user151419
user151419

Reputation:

XSL Transformation help

I currently have the following XML file:

<Regions>
  <Region>
     <Code>AU</Code>
     <Name>Austria</Name>
  </Region>
</Regions>
<Channels>
    <Channel>
      <Code>00</Code>
      <Name>Im a channel</Name>
    </Channel>
    ...
</Channels>
<Programs>
   <Program>
      <Code>00</Code>
      <Name>Program</Name>          
   </Program>
</Programs>

I would only like to keep the Channels path so that the output would look like this using an XSLT:

<Channels>
    <Channel>
      <Code>00</Code>
      <Name>Im a channel</Name>
    </Channel>
    ...
</Channels>

Upvotes: 2

Views: 113

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

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="/*">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="/t/*[not(self::Channels)]"/>
</xsl:stylesheet>

when applied on the provided XML document (fixed to be made well-formed):

<t>
    <Regions>
        <Region>
            <Code>AU</Code>
            <Name>Austria</Name>
        </Region>
    </Regions>
    <Channels>
        <Channel>
            <Code>00</Code>
            <Name>Im a channel</Name>
        </Channel>    
    </Channels>
    <Programs>
        <Program>
            <Code>00</Code>
            <Name>Program</Name>
        </Program>
    </Programs>
</t>

produces the wanted, correct result:

<Channels>
   <Channel>
      <Code>00</Code>
      <Name>Im a channel</Name>
   </Channel>
</Channels>

Explanation:

Use of the identity rule, overriden for the top element (pass-through) and for any non-Channels children of the top element (delete).

Upvotes: 1

Related Questions