[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSLT repetition constructs

Subject: Re: XSLT repetition constructs
From: "Liam R. E. Quin liam@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 5 Mar 2019 19:42:37 -0000
Re:  XSLT repetition constructs
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.