我正在尝试从转换后的 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>
最佳答案
只是改变
<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>
关于xml - 命名空间声明问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22495305/