Xml 命名空间声明问题
我试图从转换后的xml中删除名称空间声明 请到这里: 输入xml:Xml 命名空间声明问题,xml,xslt,namespaces,xslt-1.0,Xml,Xslt,Namespaces,Xslt 1.0,我试图从转换后的xml中删除名称空间声明 请到这里: 输入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 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>
换衣服
<xsl:template match="d:Product">
<DONE>
<xsl:apply-templates select="@* | node()"/>
</DONE>
</xsl:template>
到
如果要在默认命名空间中创建元素,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 -->
正如keshlam所指出的,这样做之所以有效,是因为您将其放入与文档其余部分相同的名称空间。请注意,这并不是“删除名称空间”——实际上是将
DONE
元素放入正确的名称空间,以便它从其父元素继承默认名称空间。它所做的是避免取消继承的默认值。这是样本输出中要求的,但不是原始海报认为他们要求的。如果你在文章底部阅读,OP列出了他的要求。可能OP想说“我正试图从转换后的xml中的一些节点中删除不需要的名称空间声明”。同意,@JoelM.Lamsen——我的目的是澄清,而不是批评。如果被认为是后者,我会道歉。
<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>
<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 -->
<xsl:template match="d:Product">
<DONE>
<xsl:apply-templates select="@* | node()"/>
</DONE>
</xsl:template>