xsl-list
[Top] [All Lists]

[xsl] Difficulty with document() and for-each scope

2008-06-15 22:15:57
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>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>