2014-03-19 93 views
1

我试图从转换的XML删除空间中声明空间声明的问题

请到这里:http://xslttest.appspot.com/

输入XML:

<Products xmlns="http://api.company.com.au/Data" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<Product> 
<Code>BM54</Code> 
</Product> 
</Products> 

XSLT模板

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:d="http://api.company.com.au/Data" exclude-result-prefixes="d"> 
    <xsl:output method="xml" omit-xml-declaration="yes"/> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="d:Product"> 
     <DONE> 
      <xsl:apply-templates select="@* | node()"/> 
     </DONE> 
    </xsl:template> 
    <xsl:template match="@Product"> 
     <xsl:attribute name="DONE"> 
      <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

结果是:

<Products xmlns="http://api.company.com.au/Data" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<DONE xmlns=""> 
<Code xmlns="http://api.company.com.au/Data">BM54</Code> 
</DONE> 
</Products> 

我希望它是:

<Products xmlns="http://api.company.com.au/Data" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<DONE> 
<Code>BM54</Code> 
</DONE> 
</Products> 

回答

2

只是改变

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

<xsl:template match="d:Product"> 
    <xsl:element name="DONE" namespace="http://api.company.com.au/Data"> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:element> 
</xsl:template> 
+3

请注意,这并不是“删除名称空间” - 实际上,您将“DONE”元素放入正确的名称空间中,以便从父级继承默认名称空间。它所做的是避免取消继承的默认值。这是样本输出中要求的内容,但不是原始海报认为他们要求的内容。 – keshlam

+0

如果您在帖子的底部阅读,OP会列出他的要求。可能是OP想说“我试图从转换后的xml中的一些节点中删除不需要的名称空间声明”。 –

+0

非常感谢这么多:) – Paul

1

的alterate解决方案,如果你想创建的元素到默认的命名空间是申报前期:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:d="http://api.example.com.au/Data" 
    xmlns="http://api.example.com.au/Data" 
    exclude-result-prefixes="d"> 
<!-- rest of the document --> 

这使得d速记和空命名空间都对应于http://api.company.com.au/Data这就是你打算,即使它是不是你问什么。

然后你可以使用原始代码:

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

由于keshlam指出,这个工程监守你把它变成了相同的命名空间文档的其余部分。