Re: [xsl] Understanding Identity Transformations

Subject: Re: [xsl] Understanding Identity Transformations
From: Karl Stubsjoen <kstubs@xxxxxxxxx>
Date: Sat, 12 Feb 2005 12:20:59 -0700
In the below sample, i'm interested in the "BLUE" categories.  I still
want to bring back the original XML source but omit everything not
"BLUE".  See the FOO cat="GREEN", it contains a cat="BLUE"... of
course I would not expect to return this element since the parent
element cat is "GREEN".

SAMPLE DATA
<DATA>
  <FOO cat="BLUE">
  </FOO>
  <FOO cat="RED">
  </FOO>
  <FOO cat="BLUE">
    <BAR cat="YELLOW" />
    <BAR cat="BLUE">
  </FOO>
  <FOO cat="GREEN">
    <BAR cat="BLUE" />
  </FOO>
</DATA>

EXPECTED RESULT:
<DATA>
  <FOO cat="BLUE">
  </FOO>
  <FOO cat="BLUE">
    <BAR cat="BLUE">
  </FOO>
</DATA>


On Sat, 12 Feb 2005 19:28:13 +0100, Joris Gillis <roac@xxxxxxxxxx> wrote:
> Tempore 18:09:53, die 02/12/2005 AD, hinc in
> xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Karl Stubsjoen <kstubs@xxxxxxxxx>:
> 
> > Is the nature of identifty transformations recursive?  How does the
> > engine know that eventually the recursive call is going to run out?
> Recursive algorithms are not a priori infinite. The copying stops when all
> nodes have been copied.
> 
> > I have a large document, I am interested in transforming it.  I want
> > to reproduce the xml source but restricted for a specific value match.
> >  I totally understand how to write the appropriate select in an
> > apply-template rule to get the desired results, but do not understand
> > how to mix this call up with the recursive template identity
> > tranformation.
> > So, starting with SAMPLE_002, where would I stub out and add the
> > qualifying select?  What confuses me, is that in the sample we are
> > saying (basically) grab everything from the top to the bottom and at
> > every level.  I'm affraid that my select on an element who's value is
> > '1234' is going to mess up the results.  I hope this makes sense and I
> > understand the concept of changing a single element, or single
> > attribute by setting up the match, but in this case, I need the parent
> > node, and all of the child nodes.
> 
> Maybe you could learn be studying an example:
> 
> Consider this fictional sample:
> 
> XML:
> <root>
>        <part1>
>                <!--I will be copied-->
>                <node copy="yes">So do <b>I</b>.</node>
>        </part1>
>        <part2>
>                <node copy="no">I <b>don't</b> feel like being copied today</node>
>                <data>
>                        <value>1</value>
>                        <value>2</value>
>                        <value>3</value>
>                </data>
>        </part2>
> </root>
> 
> * You want an identity transform for 'part1', with the execption of 'b'
> which should become 'bold'
> * 'part2' must be processed without any identity transform, but with
> templates and algorithms you define.
> 
> OUTPUT:
> <result>
>        <part1>         <!--I will be copied-->
> 
>                <node copy="yes">So do <bold>I</bold>.</node>
>        </part1>
>        <p>I don't feel like being copied today</p>
>        <p>Sum= 6</p>
> </result>
> 
> How can this be achieved, while we don't have to be afraid of templates
> messing up each-other? By using modes:
> 
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.0">
> 
> <xsl:output method="xml" indent="yes"/>
> 
> <xsl:template match="root">
>        <result>
>                <xsl:apply-templates select="part1" mode="IdTr"/>
>                <xsl:apply-templates select="part2"/>
>        </result>
> </xsl:template>
> 
> <xsl:template match="node">
>        <p><xsl:apply-templates/></p>
> </xsl:template>
> 
> <xsl:template match="data">
>        <p>Sum= <xsl:value-of select="sum(value)"/></p>
> </xsl:template>
> 
> <xsl:template match="@* | node()" mode="IdTr">
>    <xsl:copy>
>      <xsl:apply-templates select="@* | node()" mode="IdTr"/>
>    </xsl:copy>
> </xsl:template>
> 
> <xsl:template match="b" mode="IdTr">
>        <bold><xsl:apply-templates/></bold>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> regards,
> --
> Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
> Gaudiam omnibus traderat W3C, nec vana fides

Current Thread