复制XML文件内容,除根节点和属性XSLT之外

3

我正在处理一个小的XSLT文件,以复制XML文件的内容并剥离声明和根节点。根节点有一个命名空间属性。

目前我已经做到了,除了现在命名空间属性被复制到直接子节点。

这是我的XSLT文件,没有什么大问题或复杂的地方:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

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

我的输入文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<Report_Data xmlns="examplenamespace">
    <Report_Entry>
        <Report_Date>
        </Report_Date>
    </Report_Entry>
    <Report_Entry>
        <Report_Date>
        </Report_Date>
    </Report_Entry>
    <Report_Entry>
        <Report_Date>
        </Report_Date>
    </Report_Entry>
</Report_Data>

XSLT之后的输出如下所示:
<Report_Entry xmlns="examplenamespace">
    <Report_Date>
    </Report_Date>
</Report_Entry>
<Report_Entry xmlns="examplenamespace">
    <Report_Date>
    </Report_Date>
</Report_Entry>
<Report_Entry xmlns="examplenamespace">
    <Report_Date>
    </Report_Date>
</Report_Entry>

问题在于,每个Report_Entry标签都会获取我删除的根节点中的xml命名空间属性。
如果你想知道,我知道XSLT的输出格式不正确。在XSLT转换后,我将添加XML声明和另一个根节点名称。
1个回答

5
以下代码会输出您想要的结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8"/>

  <!-- For each element, create a new element with the same local-name (no namespace) -->
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- Skip the root element, just process its children. -->
  <xsl:template match="/*">
    <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接