This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Interleaving 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@informatik.hu-berlin.de             |
|  op|qo        WWW:    http://www.informatik.hu-berlin.de/~obecker |
\-------------------------------------------------------------------/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]