Re: [xsl] copy nodes based on the criteria

Subject: Re: [xsl] copy nodes based on the criteria
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Fri, 7 Dec 2007 22:39:24 +0530
Hi Senthil,
  Please try this ...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                         version="1.0">

<xsl:output indent="yes" />

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

<xsl:template match="*[not(@Apply = '1')][not(ancestor::*[@Apply =
'1'] or descendant::*[@Apply = '1'])]" />

</xsl:stylesheet>

This stylesheet uses the identity template, plus a template for
excluding the desired elements.

On 12/7/07, Senthil Nathan <rsennat@xxxxxxxxx> wrote:
> Hi,
> Long time back I posted this and got a solution too. But I have some
> issues with that. I have also given that XSL below, which I got from this
> forum earlier.
>
> Suppose orig.xml is as below,
> <Root>
>   <Parent>
>        <Child1 Apply="1">
>             <Value>100</Value>
>        </Child1>
>    </Parent>
>        <sub1>
>               <Parent>
>                       <a>
>                               <value>5000</value>
>                       </a>
>                       <a Apply="1">
>                               <value>3000</value>
>                       </a>
>                       <a Apply="7">
>                               <value>7000</value>
>                       </a>
>               </Parent>
>        </sub1>
>        <noParent Apply="1">
>                <value>1234</value>
>        </noParent>
>        <noParent Apply="10">
>                <value>1234</value>
>        </noParent>
> </Root>
>
>
> -->new.xml should be as,
> <Root>
>   <Parent>
>        <Child1 Apply="1">
>             <Value>100</Value>
>        </Child1>
>    </Parent>
>        <sub1>
>               <Parent>
>                       <a Apply="1">
>                               <value>3000</value>
>                       </a>
>
>               </Parent>
>        </sub1>
>        <noParent Apply="1">
>                <value>1234</value>
>        </noParent>
> </Root>
>
> XSL----->
>  <?xml version="1.0" encoding="UTF-8" ?>
>  <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:x="http://www.w3.org/1999/xhtml";
> xmlns:exslt="http://exslt.org/common";
> xmlns:str="http://exslt.org/strings";
> xmlns="http://www.w3.org/1999/xhtml"; exclude-result-prefixes="x">
>
>  <xsl:template match="/">
>  <xsl:comment>Matched root node template</xsl:comment>
>  <xsl:apply-templates />
>  </xsl:template>
>
>  <xsl:template match="//*[not(@Apply)]">
>  <xsl:comment>Matched "element without Apply attribute" template</xsl:comment>
>  <xsl:copy>
>  <xsl:copy-of select="@*" />
>  <xsl:apply-templates />
>  </xsl:copy>
>  </xsl:template>
>
>  <xsl:template priority="2.0" match="//*[@Apply='1']">
>  <xsl:comment>Matched "element with Apply attribute = 1"
> template</xsl:comment>
>  <xsl:copy>
>  <xsl:copy-of select="@*" />
>  <xsl:apply-templates />
>  </xsl:copy>
>  </xsl:template>
>
>  <xsl:template match="//*[@Apply]">
>  <xsl:comment>Matched "element with Apply attribute that has some
> other value" template</xsl:comment>
>  </xsl:template>
>
>  </xsl:stylesheet>
>
> How can I select only the nodes with "Apply=1", drop everything else
> like, apply=0 or any value and even if apply attribute is not there at
> all. I need to still maintain the hierarchy. eg. Parent, sub1 etc did
> not have apply attribute at all. But that has to be preserved, because
> thats the hierarchy.
>
> Thanks
> Senthil


-- 
Regards,
Mukul Gandhi

Current Thread