Subject: [xsl] Re: alternating node type in a loop From: Dimitre Novatchev <dnovatchev@xxxxxxxxx> Date: Fri, 12 Jul 2002 13:25:08 -0700 (PDT) |
--- "Tyler Queen" <xmllist at telocity dot com> wrote: > > I want to loop through all the article nodes within a > section element. The problem is I want to alternate the > way they are looped by "type" going "1,2,1,2,1,2" and > then when there aren't anymore to alternate through > finish looping through the remaining nodes. > Any Ideas? > > This is the xml > > <articles> > <section> > <article type="1">Article</article> > <article type="2">Article</article> > <article type="2">Article</article> > <article type="1">Article</article> > <article type="2">Article</article> > <article type="1">Article</article> > <article type="1">Article</article> > <article type="1">Article</article> > </section> > <articles> > Hi Tyler, Here is a solution. This transformation: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="section"> <xsl:variable name="vcnt1" select="count(article[@type=1])"/> <xsl:variable name="vcnt2" select="count(article[@type=2])"/> <xsl:variable name="vcntPairs"> <xsl:choose> <xsl:when test="$vcnt1 <= $vcnt2"> <xsl:value-of select="$vcnt1"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$vcnt2"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:for-each select="article [@type=1] [position() <= $vcntPairs]"> <xsl:variable name="vPos1" select="position()"/> <xsl:apply-templates select="."/> <xsl:apply-templates select="../article[@type=2] [$vPos1]"/> </xsl:for-each> <xsl:apply-templates select="article [@type=1] [position() > $vcntPairs] | article [@type=2] [position() > $vcntPairs]"/> </xsl:template> <xsl:template match="article" > <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> when applied to your source xml: <articles> <section> <article type="1">Article</article> <article type="2">Article</article> <article type="2">Article</article> <article type="1">Article</article> <article type="2">Article</article> <article type="1">Article</article> <article type="1">Article</article> <article type="1">Article</article> </section> </articles> Produces: <article type="1">Article</article> <article type="2">Article</article> <article type="1">Article</article> <article type="2">Article</article> <article type="1">Article</article> <article type="2">Article</article> <article type="1">Article</article> <article type="1">Article</article> Hope this helped. ===== Cheers, Dimitre Novatchev. http://fxsl.sourceforge.net/ -- the home of FXSL __________________________________________________ Do You Yahoo!? Sign up for SBC Yahoo! Dial - First Month Free http://sbc.yahoo.com XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] XSLTC key() with for-each, Michael Kay | Thread | [xsl] RE: alternating node type in , Dimitre Novatchev |
RE: [xsl] alternating node type in , McNally, David | Date | RE: [xsl] default attribute values?, Michael |
Month |