user5458829
user5458829

Reputation: 163

Removing tags from XML using XSLT

I have one XML where i want to remove some tags and need to keep the elements within it below is the sample. sample XML:

<aggRes>
   <services>
      <serviceIdentifier>vStatus</serviceIdentifier>
      <body>
         <vStatusResponse>
            <Access>Y</Access>
         </vStatusResponse>
      </body>
   </services>
   <services>
      <serviceIdentifier>vRules</serviceIdentifier>
      <body>
         <vRulesResponse>
            <Access>Y</Access>
         </vRulesResponse>
      </body>
   </services>
   <services>
      <serviceIdentifier>vAppAccess</serviceIdentifier>
      <body>
         <vAppAccessResponse>
            <Access>Y</Access>
         </vAppAccessResponse>
      </body>
   </services>
</aggRes>

Required Output:

<aggRes>
   <services>
      <serviceIdentifier>vStatus</serviceIdentifier>
      <Access>Y</Access>
   </services>
   <services>
      <serviceIdentifier>vRules</serviceIdentifier>
      <Access>Y</Access>
   </services>
   <services>
      <serviceIdentifier>vAppAccess</serviceIdentifier>
      <Access>Y</Access>
   </services>
</aggRes>

I tried using this XSLT code: XSLT code:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*" />
         <xsl:apply-templates />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="body">
      <xsl:copy-of select="node()" />
   </xsl:template>
   <xsl:template match="vStatusResponse">
      <xsl:copy-of select="node()" />
   </xsl:template>
</xsl:stylesheet>

Its not working as expected . can someone help how to get the required XML.

Upvotes: 0

Views: 56

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116959

You have more exceptions to the rule than nodes that should conform to it. Why don't you approach this from the opposite direction? Then all you need to to do is:

XSLT 1.0

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

<xsl:template match="aggRes | services | serviceIdentifier | Access">
    <xsl:copy>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Tomalak
Tomalak

Reputation: 338108

You want <xsl:apply-templates>, not <xsl:copy-of>

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

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

A less explicit (and therefore potentially more flexible) alternative would be

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

Output in either case:

<aggRes>
   <services>
      <serviceIdentifier>vStatus</serviceIdentifier>
      <Access>Y</Access>
   </services>
   <services>
      <serviceIdentifier>vRules</serviceIdentifier>
      <Access>Y</Access>
   </services>
   <services>
      <serviceIdentifier>vAppAccess</serviceIdentifier>
      <Access>Y</Access>
   </services>
</aggRes>

Upvotes: 1

Related Questions