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

Re: Interleaving two lists

Subject: Re: Interleaving two lists
From: Oliver Becker <obecker@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 28 Jul 2000 15:04:10 +0200 (MET DST)
xsl count two lists
> I have an interesting and curious one. I think it hasn't show in the 
> list before. If so, sorry for reiterate....
> 
> Suppose the following XML snip:
> <data>
> 	<imagelist>
> 		<image href="img1.jpg"/>
> 		<image href="img2.jpg"/>
> 		......
> 	</imagelist>
> 	<paralist>
> 		<para>Blah, blah....</para>
> 		<para>Burli, burli...</para>
> 		....
> 	</paralist>
> </data>
> 
> and I want to transform it in something like this:
> <table>
> 	<tr>
> 		<td>
> 			<img src="img1.jpg"/>
> 		</td>
> 		<td>
> 			<p>Blah, blah....</p>
> 		</td>
> 	</tr>
> 	<tr>
> 		<td>
> 			<p>Burli, burli....</p>
> 		</td>
> 		<td>
> 			<img src="img2.jpg"/>
> 		</td>
> 	</tr>
> 	.......
> </table>

Another one for the loop compiler [1]:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:loop="http://informatik.hu-berlin.de/loop"
                exclude-result-prefixes="loop">

<xsl:output method="html" />

<xsl:template match="data">
   <xsl:variable name="images" select="imagelist/image" />
   <xsl:variable name="paras" select="paralist/para" />
   <xsl:variable name="ci" select="count($images)" />
   <xsl:variable name="cp" select="count($paras)" />
   <xsl:variable name="max">
      <xsl:choose>
         <xsl:when test="$ci > $cp"><xsl:value-of select="$ci" />
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$cp" />
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>
   <table>
      <loop:for name="i" from="1" to="$max">
         <tr>
            <td>
               <xsl:if test="$images[$i]">
                  <img src="{$images[$i]/@href}" />
               </xsl:if>
            </td>
            <td><p><xsl:value-of select="$paras[$i]" /></p></td>
         </tr>
      </loop:for>
   </table>
</xsl:template>

</xsl:stylesheet>

Cheers,
Oliver


[1] http://www.informatik.hu-berlin.de/~obecker/XSLT/#loop-compiler


PS: It just came to my mind that there's another solution using
    <xsl:for-each> without recursion:
    Determine the longer list (<xsl:choose>), use <xsl:for-each> for 
    this list, access elements of the other list via $otherlist[position()]


/-------------------------------------------------------------------\
|  ob|do        Dipl.Inf. Oliver Becker                             |
|  --+--        E-Mail: obecker@xxxxxxxxxxxxxxxxxxxxxxx             |
|  op|qo        WWW:    http://www.informatik.hu-berlin.de/~obecker |
\-------------------------------------------------------------------/


 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.