【问题标题】:Parsing nested tags through XSLT通过 XSLT 解析嵌套标签
【发布时间】:2013-06-17 14:23:58
【问题描述】:

我有一个这样的 XML 文件 -

<item>
    <item>
        <tag>value</tag>
        <tag2>value</tag2>
    </item>
</item>

我想提取内部标签,让它看起来像这样 -

<item>
    <tag>value</tag>
    <tag2>value</tag2>
</item>

这可以通过 XSLT 实现吗?

如果有帮助,保证外部的&lt;item&gt;&lt;/item&gt; 是XML 文档的第一行和最后一行。

【问题讨论】:

    标签: java xml xslt xml-parsing xslt-2.0


    【解决方案1】:
    <xsl:template match="/">
      <xsl:copy-of select="//item[not(item)]"/>
    </xsl:template>
    

    提取那些不包含其他item 元素的item 元素。

    【讨论】:

      【解决方案2】:

      如果你想只提取叶元素(那些没有自己的子元素的元素),那么简单

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:template match="/*">
          <xsl:copy>
            <xsl:copy-of select="descendant::*[not(*)]" />
          </xsl:copy>
        </xsl:template>
      </xsl:stylesheet>
      

      会做的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-09
        • 2011-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-04
        • 2018-10-31
        相关资源
        最近更新 更多