xsl-list
[Top] [All Lists]

Re: [xsl] copy nodes based on the criteria

2007-12-07 12:10:41
Hi Mukul,
Thanks. Thats works fine, I guess.

So, first copy all the elements which has attributes, then drop elements which
don't have the attribute Apply=1. Also dont drop the ancestor and descendant of
elements with Apply=1

Thanks again
Senthil


On Dec 7, 2007 10:39 PM, Mukul Gandhi 
<gandhi(_dot_)mukul(_at_)gmail(_dot_)com> wrote:

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::*[(_at_)Apply =
'1'] or descendant::*[(_at_)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(_at_)gmail(_dot_)com > 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="//*[(_at_)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="//*[(_at_)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

--~------------------------------------------------------------------
XSL-List info and archive:   http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto: 
xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>