Re: [xsl] XSLT repetition constructs

Subject: Re: [xsl] XSLT repetition constructs
From: "Liam R. E. Quin liam@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 5 Mar 2019 19:42:37 -0000
On Tue, 2019-03-05 at 09:57 +0000, Mukul Gandhi gandhi.mukul@xxxxxxxxx
wrote:
> Hi all,
>     I've written a document explaining different XSLT repetition
> constructs. Its available at,
> http://gandhimukul.tripod.com/xslt/xslt_repetition_constructs.pdf.
> 
> Any comments, corrections to this document are welcome.
> 

i prefer to suggest to people, when introducing them to XSLT, that an
XSLT transformation, or stylesheet, represents a mapping from one set
of trees to another. A construct like for-each is not actually a loop
in the imperative programming sense; rather, it specifies how to map a
sequence into another sequence. For example, in general, for-each could
be evaluated in any order or even in parallel.

Some people prefer to gloss over this and consider it an advanced
topic.

The statement that it's easier touse iterate than to construct an XPath
expression surprised me a little, but i suppose it's very subjective.

Here's a short XSLT 1 example that also works: (more comments follow
it)

<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="val[not(. = 0) and not(preceding-sibling::val =
0)]" />
        </result>
    </xsl:template>

    <xsl:template match="val">
        <xsl:copy-of select="." />
    </xsl:template>
</xsl:stylesheet>

You could also use the XPath expression as the match expression for
val, and have an empty template for val to transform unwanted val
elements into the empty sequence and make them vanish :)

I added xsl:strip-space to make the intent clearer, too: (even more
comments follow!)

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

    <xsl:output indent="yes" />
    <xsl:strip-space elements="root val" />

    <xsl:template match="root">
        <result>
            <xsl:apply-templates />
        </result>
    </xsl:template>

    <xsl:template
        match="val[not(. = 0) and not(preceding-sibling::val = 0)]" >
        <xsl:copy-of select="." />
    </xsl:template>

    <xsl:template match="val"></xsl:template>
</xsl:stylesheet>

Now, your goal was to illustrate xsl:iterate, but is the reason you
think of a recursive template before you think of using the preceding-
sibling axis that you think of for-each as a loop construct? If so,
xsl:iterate will probably enforce that way of thinking and lead you
further astray.

Starting with apply-templates and match patterns often makes for
something easier to extend and maintain as the stylesheet gets larger,
rather than recursive named tmplates. I donbt have enough experience
with using xs:iterate in larger stylesheets, but suspect it comes in
use most productively when youbre processing something and donbt want
to (or canbt) change other templates.

Liam


-- 
Liam Quin, https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Web slave for vintage clipart http://www.fromoldbooks.org/

Current Thread