Rahul
Rahul

Reputation: 137

Extract node from xml

I am struggling to achieve something that should be very simple using XSLT1.0, so please bear with me.

This is my original XML:

<adapter-response>
<status>success</status>
<data>
<inventory>
<servers>
....
....
</servers>
<routers>
....
....
</routers>
...
...
</inventory>
</adapter-response>

Its a huge XML with lots of data. I just want to strip out the adapter related tags and keep the inventory data with the original tags. So the final XML would be:

<inventory>
<servers>
....
....
</servers>
<routers>
....
....
</routers>
...
...
</inventory>

Please help!

Regards, Rahul

Upvotes: 1

Views: 157

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

The provided "sketch" of an XML document is not well-formed, so my reconstruction of the document maynot be the one that was intended in the question:

<adapter-response>
    <status>success</status>
    <data>
        <inventory>
            <servers>
....
....
            </servers>
            <routers>
....
....
            </routers>
...
...
        </inventory>
    </data>
</adapter-response>

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="*[contains(name(), 'adapter')]">
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>

copies to the output only elements, whose name doesn't contain the string "adapter". THe result for the above document is:

<status>success</status>
<data>
   <inventory>
      <servers>
....
....
            </servers>
      <routers>
....
....
            </routers>
...
...
        </inventory>
</data>

Upvotes: 1

Related Questions