[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Difficulty with document() and for-each scope

Subject: Difficulty with document() and for-each scope
From: Adam Nielsen <adam.nielsen@xxxxxxxxx>
Date: Mon, 16 Jun 2008 15:15:27 +1000
 Difficulty with document() and for-each scope
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.