2011-05-29 120 views
1

我使用xslt將兩個xml文件一起添加,然後該生成的xml文件應符合架構。我可以將兩個xml文件一起添加,但是結果應該在根元素中包含noNamespaceSchemaLocation =「file:///osba.xsd」。XSLT,將屬性添加到生成的xml文件的根節點

這裏是我的.xsl文件:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" /> 
<xsl:variable name="with" select="'reception.xml'" /> 

<xsl:template match="@*|node()" > 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="bd"> 
<xsl:copy> 
    <xsl:apply-templates select="@*|node()" /> 
    <xsl:variable name="info" select="document($with)/reception/bd[title=current()/title]/." /> 
    <xsl:for-each select="$info/*"> 
    <xsl:if test="name()!='title'"> 
     <xsl:copy-of select="." /> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:copy> 
</xsl:template> 
</xsl:transform> 

我想補充一點,如:

<xsl:template match="/*" > 
    <xsl:attribute name="noNamespaceSchemaLocation"><![CDATA[file:///I:/IMY%20Project/XML%20Files/osba.xsd]]></xsl:attribute> 
<xsl:apply-templates/>       

我還沒有發現任何方式來匹配,如果當前元素是根,我也在考慮在第一個xsl:模板中添加一個xsl:if test =「root」類型的交易。

感謝所有幫助

回答

1

事情是這樣的:

 <xsl:template match="/*"> 
     <xsl:element name="{local-name(.)}" namespace="{namespace-uri(.)}"> 
      <xsl:copy-of select="@*"/> 
      <xsl:attribute name="noNamespaceSchemaLocation" 
       namespace="http://www.w3.org/2001/XMLSchema-instance">your-value</xsl:attribute> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template>
+0

哈哈非常感謝!像魅力一樣工作 – 2011-05-29 11:21:52

相關問題