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

Re: simulating for with foreach

Subject: Re: simulating for with foreach
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 5 Jul 2006 11:54:56 +0100
foreach xsl
> Do I have to use <xsl:if test="position() mod 4 = 1", or there is
> a more flexible way?

It depends what you mean by flexible. Using that method you can use any
sequence that may be expressed by an Xpath formula, which is pretty
flexible, but if your sequence isn't expressed by formula, or has
repeated numbers and is perhaps from some empirical sampled data and you
need something like
1 32 2 5 7 2 4 7

just list them in some document

list.xml
<list>
<n>1</n> <n>32</n> <n>2</n> <n>5</n> <n>7</n> <n>2</n> <n>4</n> <n>7</n
</list>

and then do

<xsl:variable name="nodes" select="node"/>
<xsl:for-each select="document('list.xml')/list/n">
<xsl:for-each select="$nodes[position()=current()]">
.
</xsl:for-each>
</xsl:for-each>

If you have an extension function like saxon:tokenize you can avoid the
explict document

<xsl:variable name="nodes" select="node"/>
<xsl:for-each select="saxon:tokenize('1 32 2 5 7 2 4 7')">
<xsl:for-each select="$nodes[position()=current()]">
.
</xsl:for-each>
</xsl:for-each>

If you have XSLT2 you can avoid the extension function

<xsl:variable name="nodes" select="node"/>
<xsl:for-each select="1,32,2,5,7,2,4,7">
<xsl:for-each select="$nodes[position()=current()]">
.
</xsl:for-each>
</xsl:for-each>

David

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.