This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Indexing in XSLT
- From: Maulik Modi <MModi at qcorps dot com>
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Sun, 11 Aug 2002 12:54:08 -0500
- Subject: [xsl] Indexing in XSLT
- Reply-to: xsl-list at lists dot mulberrytech dot com
I need some help to solve this problem. Any help is appreciated.
I have an instance document that gives me a list of available dates to
choose from. I get passed another date which I have to compare against this
list and return the matched date.
If no match is found in the list, and the date passed is lower, I need to
return the next higher date from the list. For example, if the date passed
is Aug 10, 2002 and it is not in the available list, I need to return the
next available date which could be Aug 12, 2002. Also I need to check for
month end and year end. So if Aug 30 is passed, I will need to pass back Sep
2 (if available in the list).
The last requirement is if no match is found, and the date passed is higher,
I need to pass a null. For example, the date passed is Sep 30 and the last
date in the list is Aug 15, I need to pass back a null (or empty string).
I have used keys and the sample code I am pasting here is in working mode
and does not include everything I have tried. Any help is appreciated.
Instance XML:
<acquisitions>
<delivery zip="06850">
<date>07/31/2002</date>
<date>08/01/2002</date>
<date>08/02/2002</date>
<date>08/05/2002</date>
<date>08/31/2002</date>
<date>09/01/2002</date>
</delivery>
</acquisitions>
XSL (in working draft):
<xsl:key name="dateindex" match="delivery/date" use="."/>
<xsl:template match="/">
<all>
<xsl:apply-templates
select="acquisitions/delivery"/>
</all>
</xsl:template>
<xsl:template match="delivery">
<xsl:call-template name="datevar">
<xsl:with-param name="svcdate"
select="'08/03/2002'"/>
<xsl:with-param name="deldate"
select="key('dateindex',date[1])"/>
<xsl:with-param name="totaldates"
select="count(date)"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="datevar">
<xsl:param name="svcdate"/>
<xsl:param name="deldate"/>
<xsl:param name="pos" select="1"/>
<xsl:param name="totaldates"/>
<xsl:variable name="delmonth"
select="substring-before($deldate,'/')"/>
<xsl:variable name="delday"
select="substring-before(substring-after($deldate,'/'),'/')"/>
<xsl:variable name="delyear"
select="substring-after(substring-after($deldate,'/'),'/')"/>
<xsl:variable name="svcmonth"
select="substring-before($svcdate,'/')"/>
<xsl:variable name="svcday"
select="substring-before(substring-after($svcdate,'/'),'/')"/>
<xsl:variable name="svcyear"
select="substring-after(substring-after($svcdate,'/'),'/')"/>
<xsl:variable name="daysinmonth">
<xsl:choose>
<xsl:when test="$svcmonth='01' or
$svcmonth='03' or $svcmonth='05' or $svcmonth='07' or $svcmonth='08' or
$svcmonth='10' or $svcmonth='12'">
<xsl:value-of select="31"/>
</xsl:when>
<xsl:when test="$svcmonth='04' or
$svcmonth='06' or $svcmonth='09' or $svcmonth='11'">
<xsl:value-of select="30"/>
</xsl:when>
<xsl:when test="$svcmonth='02'">
<xsl:value-of select="28"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$svcday=$delday">
<xsl:choose>
<xsl:when
test="$svcmonth=$delmonth">
<xsl:choose>
<xsl:when
test="$svcyear=$delyear">
<xsl:value-of select="concat($delmonth,'/',$delday,'/',$delyear)"/>
</xsl:when>
<xsl:otherwise>
failed year
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:call-template
name="datevar">
<xsl:with-param
name="svcdate" select="$svcdate"/>
<xsl:with-param
name="deldate" select="key('dateindex',date[$pos+1])"/>
<xsl:with-param
name="pos" select="$pos+1"/>
<xsl:with-param
name="totaldates" select="$totaldates"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when
test="$pos=$totaldates">exhausted list for day match</xsl:when>
<xsl:otherwise>
<xsl:call-template
name="datevar">
<xsl:with-param
name="svcdate" select="$svcdate"/>
<xsl:with-param
name="deldate" select="key('dateindex',date[$pos+1])"/>
<xsl:with-param
name="pos" select="$pos+1"/>
<xsl:with-param
name="totaldates" select="$totaldates"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
TIA,
Maulik
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list