XSLT Row Replication within Tables

Here should go questions about transforming XML with XSLT and FOP.
jsuen
Posts: 1
Joined: Mon Aug 07, 2006 7:16 am

XSLT Row Replication within Tables

Post by jsuen »

Hi,

I wanted to generate a table with a new row (tr) replicated every 4 or so elements (td's). I have found a good example using a calendar month as a data source, and would like the same design principle in my XSLT.

i.e. for:

Code: Select all

<Month>
<day date="1"/>
<day date="2"/>
<day date="3"/>
<day date="4"/>
<day date="5"/>
<day date="6"/>
<day date="7"/>
<day date="8"/>
<day date="9"/>
</Month>

Code: Select all

    <xsl:template match="day[position() mod 4 = 1]">
<tr>
<xsl:for-each select=". | following-sibling::day[position() < 4]">
<td><xsl:value-of select="@date"/></td>
</xsl:for-each>
</tr>
</xsl:template>

<xsl:template match="day"/> <!-- ignore the other days -->
The only problem in my application is that I want to fetch the datasource from a http: XML document (i.e. via localhost). Is it possible to do this in the code example above?

I cannot reference the $variable in the <xsl:template match=""> tag, but normally use a variable to assign the external XML datasource to be used.

Any help would be greatly appreciated, thanks

Jon
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

Assuming you have the data in some variable then you can match on the document root element and apply templates on that variable content. You can also use the document function to access the data and again match on the document root and apply templates on the nodes in the document, like:

Code: Select all


<xsl:template match="/">
<xsl:apply-templates select="document('http://localhost/myData.xml')/*"/>
</xsl:template>
Best Regards,
George
Post Reply