I want to test for events within a group that qualify for both city and current-date() so that I can output a header.
To find the city ($place eq //event/@city) seems to work. But I can’t figure out how to express “some eventTime/@date is less than $today”. The error message is “A sequence of more than one item is not allowed as the second operand of ‘eq'” Which is confusing because I’ve written test=”($place eq //event/@city) and (xs:date($today) lt xs:date(//eventTime/@date)).
How should I be comparing $today to the @date in my eventTime? Here’s the input.
<calendar>
<group month="2012-04-01">
<event city="paris">
<eventTime date="2012-04-02"/>
<eventText>Paris - expired April date</eventText>
</event>
<event city="london">
<eventTime date="2012-04-19"/>
<eventText>London - current April 19 date</eventText>
</event>
<event city="london">
<eventTime date="2012-04-24"/>
<eventText>London - current April date</eventText>
</event>
</group>
<group month="2012-05-01">
<event city="london">
<eventTime date="2012-05-02"/>
<eventText>London - current May date</eventText>
</event>
<event city="paris">
<eventTime date="2012-05-01"/>
<eventText>Paris - current May date</eventText>
</event>
<event city="london">
<eventTime date="2012-05-02"/>
<eventText>London - current May date</eventText>
</event>
</group>
</calendar>
Here’s the XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://johnadamturnbull.com/xslt"
exclude-result-prefixes="xs"
version="2.0" >
<xsl:output method="html" indent="yes" name="html"/>
<xsl:param name="place" as="xs:string" required="yes"></xsl:param>
<xsl:variable name="today" select="current-date()" as="xs:date"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="calendar/group"/>
</body>
</html>
</xsl:template>
<xsl:template match = "group">
<xsl:if test="($place eq //event/@city) and
(xs:date($today) ge xs:date(//eventTime/@date))">
<h4 class = "dateHeader">
<xsl:value-of select="format-date(./@month,'[MNn] [Y]')"/>
</h4>
<ul>
<xsl:apply-templates select="event"></xsl:apply-templates>
</ul>
</xsl:if>
</xsl:template>
<xsl:template match="event">
<xsl:variable name="eventTime" select="eventTime/@date" as="xs:date"/>
<xsl:choose>
<xsl:when test="($eventTime ge $today) and
(($place eq @city) or (@city eq ''))">
<li>
<xsl:apply-templates></xsl:apply-templates>
</li>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
There isn’t an example of what the HTML output is supposed to be, but I’m pretty sure I can tell what you’re trying to achieve.
I think your XSLT can be simplified by removing the
xsl:ifandxsl:chooseand adding predicates to do your testing.This XSLT 2.0 stylesheet:
applied to your example XML input produces this HTML output:
If this isn’t what you’re looking for, please add an example of what the HTML output should look like.