xslt 1.0 - Accessing current node in predicate with xsl:for-each -
i'm stuck finding out how can access current node whilst iterating on collection of nodes xsl:for-each. xml:
<events> <event> <date> <year>2012</year> <month>july</month> </date> <description>...</description> </event> <!-- more events --> </events> what try achieve html-representation this:
<h2>2012</h2> <dl> <dt>july</dt> <dd>one of these every event year=2012 , month=july</dd> <dt>august</dt> <!-- ... --> </dl> <h2>2013</h2> <!-- ... --> i'm using xpath-expression distinct years , iterate on them calling template called year parameters $year , $events. getting value $year easy, i'm struggling finding right events. following won't work, because . in second predicate refers event being tested predicate. how access year in there?
<xsl:template match="events"> <xsl:for-each select="event[not(date/year=preceding-sibling::event/date/year)]/date/year"> <xsl:call-template name="year"> <xsl:with-param name="year" select="." /> <xsl:with-param name="events" select="event[date/year=.]" /> </xsl:call-template> </xsl:for-each> </xsl:template> many in advance!
ps: must work xpath , xslt 1.0.
this transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="kyear" match="year" use="."/> <xsl:key name="keventbymonthyear" match="event" use="string(date)"/> <xsl:key name="kmonthbymonthyear" match="month" use="string(..)"/> <xsl:key name="kmonthbyyear" match="month" use="../year"/> <xsl:template match="/*"> <xsl:for-each select= "*/date/year [generate-id() = generate-id(key('kyear', .)[1])] "> <h2><xsl:value-of select="."/></h2> <dl> <xsl:apply-templates select= "key('kmonthbyyear', current()) [generate-id() = generate-id(key('kmonthbymonthyear', string(..) )[1] ) ]"/> </dl> </xsl:for-each> </xsl:template> <xsl:template match="month"> <dt><xsl:value-of select="."/></dt> <xsl:for-each select= "key('keventbymonthyear', string(current()/..))"> <dd><xsl:value-of select="description"/></dd> </xsl:for-each> </xsl:template> </xsl:stylesheet> when applied on following xml document:
<events> <event> <date> <year>2012</year> <month>january</month> </date> <description>jan1</description> </event> <event> <date> <year>2012</year> <month>january</month> </date> <description>jan2</description> </event> <event> <date> <year>2012</year> <month>march</month> </date> <description>mar1</description> </event> <event> <date> <year>2012</year> <month>march</month> </date> <description>mar2</description> </event> <event> <date> <year>2012</year> <month>march</month> </date> <description>mar3</description> </event> <event> <date> <year>2012</year> <month>july</month> </date> <description>jul1</description> </event> <event> <date> <year>2012</year> <month>july</month> </date> <description>jul2</description> </event> <event> <date> <year>2012</year> <month>september</month> </date> <description>sep1</description> </event> <event> <date> <year>2012</year> <month>october</month> </date> <description>oct1</description> </event> <event> <date> <year>2012</year> <month>october</month> </date> <description>oct2</description> </event> <event> <date> <year>2012</year> <month>november</month> </date> <description>nov1</description> </event> <event> <date> <year>2012</year> <month>november</month> </date> <description>nov2</description> </event> <event> <date> <year>2012</year> <month>december</month> </date> <description>dec1</description> </event> <event> <date> <year>2012</year> <month>december</month> </date> <description>dec2</description> </event> <event> <date> <year>2012</year> <month>december</month> </date> <description>dec3</description> </event> <event> <date> <year>2013</year> <month>january</month> </date> <description>jan1</description> </event> <event> <date> <year>2013</year> <month>january</month> </date> <description>jan2</description> </event> </events> produces wanted, correct result:
<h2>2012</h2> <dl> <dt>january</dt> <dd>jan1</dd> <dd>jan2</dd> <dt>march</dt> <dd>mar1</dd> <dd>mar2</dd> <dd>mar3</dd> <dt>july</dt> <dd>jul1</dd> <dd>jul2</dd> <dt>september</dt> <dd>sep1</dd> <dt>october</dt> <dd>oct1</dd> <dd>oct2</dd> <dt>november</dt> <dd>nov1</dd> <dd>nov2</dd> <dt>december</dt> <dd>dec1</dd> <dd>dec2</dd> <dd>dec3</dd> </dl> <h2>2013</h2> <dl> <dt>january</dt> <dd>jan1</dd> <dd>jan2</dd> </dl> explanation:
proper use of muenchian method grouping -- composite grouping keys.
Comments
Post a Comment