I am trying to take a basic XML file and turn it into a table. This table
should be two columns wide and as many rows long as required.
Following the instructions at:
http://www.dpawson.co.uk/xsl/sect2/N7450.html#d7256e205
With four hit's (my XML uses pet's) I get the following result:
Hit One Hit Two
Hit Two Hit Three
Hit Four is never seen.. and as you can see hit two duplicates. I am using
XMLSPY with the internal XSLT engine to work on this.
Is the FAQ in need of updating? Is XMLSPY not behaving properly? Did I copy
it wrong?
Here is my modified XSL:
<?xml version="1.0"?>
<?xmlspysamplexml C:\Documents and Settings\Hal
Levy\Desktop\MCSPCA\dogs.xml?>
<?xml-stylesheet type="text/xsl" href="C:\Documents and Settings\Hal
Levy\Desktop\MCSPCA\dogs.xsl"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Monmouth County SPCA Dogs for Adoption</TITLE>
</HEAD>
<BODY>
<table border="1">
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="pets">
<xsl:variable name="cols" select="2"/>
<xsl:variable name="all_pets" select="pet"/>
<xsl:for-each select="$all_pets[position() mod $cols = 1]">
<xsl:variable name="this_pet_pos" select="position()"/>
<xsl:variable name="current_row_pets"
select="$all_pets[position() >=
$this_pet_pos and position() < $this_pet_pos + $cols]"/>
<!-- go generate the table rows for this one data row
-->
<xsl:call-template name="make_table_rows">
<xsl:with-param name="cols" select="$cols"/>
<xsl:with-param name="current_row_pets"
select="$current_row_pets"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="make_table_rows">
<xsl:param name="cols" select="1"/>
<xsl:param name="current_row_pets" select="/.."/>
<!-- selects above are defaults in case nothing was passed in -->
<xsl:if test="$current_row_pets">
<xsl:variable name="num_empty_cols" select="$cols -
$current_row_pets"/>
<tr>
<xsl:for-each select="$current_row_pets">
<td valign="top">
<img src="" alt="" align="left">
<xsl:attribute
name="src"><xsl:value-of
select="thumbnail-url"/></xsl:attribute>
<xsl:attribute
name="alt"><xsl:value-of
select="description"/></xsl:attribute>
<xsl:value-of
select="description"/>
</img>
</td>
</xsl:for-each>
<xsl:if test="$num_empty_cols">
<!-- true if not zero -->
<xsl:call-template name="make_empty_cells">
<xsl:with-param name="num"
select="$num_empty_cols"/>
</xsl:call-template>
</xsl:if>
</tr>
</xsl:if>
</xsl:template>
<xsl:template name="make_empty_cells">
<xsl:with-param name="num" select="0"/>
<xsl:if test="$num">
<td> </td>
<xsl:call-template name="make_empty_cells">
<xsl:with-param name="num - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Here is my XML:
<?xml version="1.0"?>
<!-- edited with XMLSPY v5 rel. 2 U (http://www.xmlspy.com) by Hal Levy
(None) -->
<?xml-stylesheet href="dogs.xsl" type="text/xsl"?>
<pets>
<pet>
<thumbnail-url>images/dogs/dog-buttercup.jpg</thumbnail-url>
<picture-url/>
<description>dog 1</description>
</pet>
<pet>
<thumbnail-url>images/dogs/dog-milo2.jpg</thumbnail-url>
<picture-url/>
<description>dog 2</description>
</pet>
<pet>
<thumbnail-url>images/dogs/dog-Sandy2.jpg</thumbnail-url>
<picture-url/>
<description>dog 3</description>
</pet>
<pet>
<thumbnail-url>images/dogs/dog-trooper4.jpg</thumbnail-url>
<picture-url/>
<description>dog 4</description>
</pet>
</pets>
Hal
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list