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

Re: alternating node type in a loop

Subject: Re: alternating node type in a loop
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 12 Jul 2002 13:25:08 -0700 (PDT)
vcnt1
--- "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 &lt;= $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() &lt;= $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

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.