[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Hi all,
I'm trying to write a quick bit of code to loop through all the months of the year, and print a list of deadlines for each month. The grouping and printing the deadlines works, but instead of copying and pasting the <xsl:apply-templates/> block twelve times, I would like to iterate through a list of months.
Unfortunately when I do this I use document('') to pull the list of months from the XSLT code itself, but this then stops anything inside the for-each block from being able to pull any further data from the main XML code.
Does anyone know a way around this? There is some sample code demonstrating the problem below.
--- begin loop.xsl ---
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:me="http://some.url">
<xsl:template match="/items">
Use the list: (doesn't work)
<p><xsl:value-of select="$month"/> - <xsl:value-of select="."/></p>
</xsl:for-each>
Example hardcoded for May: (works)
</xsl:template>
</xsl:stylesheet>
</items>
[xsl] Difficulty with document() and for-each scope
Subject: [xsl] Difficulty with document() and for-each scope From: Adam Nielsen <adam.nielsen@xxxxxxxxx> Date: Mon, 16 Jun 2008 15:15:27 +1000 |
Hi all,
I'm trying to write a quick bit of code to loop through all the months of the year, and print a list of deadlines for each month. The grouping and printing the deadlines works, but instead of copying and pasting the <xsl:apply-templates/> block twelve times, I would like to iterate through a list of months.
Unfortunately when I do this I use document('') to pull the list of months from the XSLT code itself, but this then stops anything inside the for-each block from being able to pull any further data from the main XML code.
Does anyone know a way around this? There is some sample code demonstrating the problem below.
Many thanks, Adam.
--- begin loop.xsl ---
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:me="http://some.url">
<!-- Group items by numerical month (e.g. '02' in '2001-02-03') --> <xsl:key name="by-month" match="/items/item" use="substring(deadline, 6, 2)" />
<me:monthnames> <me:month>January</me:month> <me:month>February</me:month> <me:month>March</me:month> <me:month>April</me:month> <me:month>May</me:month> <!-- etc --> </me:monthnames>
<xsl:template match="/items">
Use the list: (doesn't work)
<xsl:for-each select="document('')//me:month"> <xsl:variable name="month" select="format-number(position(), '00')"/>
<p><xsl:value-of select="$month"/> - <xsl:value-of select="."/></p>
<ul> <!-- This template never gets applied --> <xsl:apply-templates select="key('by-month', $month)"> <xsl:sort select="deadline"/> </xsl:apply-templates> </ul>
</xsl:for-each>
Example hardcoded for May: (works)
<ul> <xsl:apply-templates select="key('by-month', '05')"> <xsl:sort select="deadline"/> </xsl:apply-templates> </ul>
</xsl:template>
<xsl:template match="item"> <li><xsl:value-of select="name"/>: <xsl:value-of select="deadline"/></li> </xsl:template>
</xsl:stylesheet>
--- begin loop.xml --- <?xml version="1.0" encoding="UTF-8" ?> <items>
<item> <name>Item One</name> <deadline>2008-01-01</deadline> </item>
<item> <name>Item Two</name> <deadline>2008-02-01</deadline> </item>
<item> <name>Item Three</name> <deadline>2008-05-03</deadline> </item>
<item> <name>Item Four</name> <deadline>2008-05-04</deadline> </item>
</items>
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] a table-of-contents for X, Ivan Shmakov | Thread | Re: [xsl] Difficulty with document(, Abel Braaksma |
Re: [xsl] a table-of-contents for X, Ivan Shmakov | Date | Re: [xsl] Difficulty with document(, Abel Braaksma |
Month |