【问题标题】:Copy XML file contents except for root node and attribute XSLT复制除根节点和属性 XSLT 之外的 XML 文件内容
【发布时间】:2013-11-01 19:24:10
【问题描述】:

我正在处理一个小型 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 声明和不同的根节点名称。

【问题讨论】:

    标签: xml xslt root-node


    【解决方案1】:

    以下产生您想要的输出:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 2011-09-04
      • 2015-01-07
      • 2015-07-31
      • 2016-10-11
      • 2017-07-11
      相关资源
      最近更新 更多