简体   繁体   中英

Parsing nested tags through XSLT

I have a XML file like this -

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

I'd like to extract out the inner tag so it looks like this -

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

Is this possible through XSLT?

The outer <item> and </item> are guaranteed to be the first and last lines of the XML document if that helps.

If you want to extract just leaf elements (those that have no child elements of their own) then something as simple as

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

will do the job.

<xsl:template match="/">
  <xsl:copy-of select="//item[not(item)]"/>
</xsl:template>

extracts those item elements that do not contain other item elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM