Reputation: 3
I have the repeating structure of XML and I want to use the max() and min() function present in the xslt 2.0 to fetch the maximum and minimum dates.
I am using like max(Detail/Startdate) and it is returning me NaN as the result. Could anyone help me out here?
<Info dataSource="source">
<Detail>
<StartDate>20121211</StartDate>
<EndDate>20130112</EndDate>
</Detail>
<Detail>
<StartDate>20121211</StartDate>
<EndDate>20130112</EndDate>
</Detail>
<Detail>
<StartDate>20121211</StartDate>
<EndDate>20130112</EndDate>
</Detail>
<Detail>
<StartDate>20121218</StartDate>
<EndDate>20130114</EndDate>
</Detail>
</Info>
Upvotes: 0
Views: 5329
Reputation: 167716
For the sample you posted and the XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:template match="Info">
<xsl:value-of select="max(Detail/StartDate), max(Detail/EndDate), min(Detail/StartDate), min(Detail/EndDate)"/>
</xsl:template>
</xsl:stylesheet>
Saxon 9.5 gives me 2.0121218E7 2.0130114E7 2.0121211E7 2.0130112E7
and not NaN
. Obviously treating those date values as doubles is not the ideal approach so I would suggest to use something alike
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf">
<xsl:function name="mf:date" as="xs:date">
<xsl:param name="date" as="xs:string"/>
<xsl:sequence select="xs:date(concat(substring($date, 1, 4), '-', substring($date, 5, 2), '-', substring($date, 7)))"/>
</xsl:function>
<xsl:template match="Info">
<xsl:value-of select="max(Detail/StartDate/mf:date(.)), max(Detail/EndDate/mf:date(.)), min(Detail/StartDate/mf:date(.)), min(Detail/EndDate/mf:date(.))"/>
</xsl:template>
</xsl:stylesheet>
to convert to xs:date
first and then to compute a max or min date and not a max or min number.
If you still have problems then post enough information for us to reproduce the problem.
Upvotes: 2