xsl-list
[Top] [All Lists]

Re: Understanding Identity Transformations

2005-02-12 11:28:13
Tempore 18:09:53, die 02/12/2005 AD, hinc in xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com scripsit Karl Stubsjoen <kstubs(_at_)gmail(_dot_)com>:

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

--~------------------------------------------------------------------
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>
--~--