xml - 命名空间声明问题

标签 xml xslt namespaces xslt-1.0

我正在尝试从转换后的 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/

相关文章:

xslt - 在 XSL 中如何防止重复?

visual-studio - 类型 Task<> 存在于 Unity.Tasks 和 mscorlib(Unity 问题)

Java DOM,命名空间/版本问题

c# - 在 UTF16 列中存储 UTF8 数据

android - 从 Manifest.xml 中读取应用程序标签和名称

c++ - 哪些脚本语言可用于 msxml4.dll xsl 转换?

xslt - 使用 XSL 呈现 RDF。更新

c# - 奇怪的 C# 命名空间问题

java - 单击按钮后无法切换到另一个 Activity

javascript - UI5 添加/删除/更改响应式拆分器拆分 Pane