XSLT: Getting the latest date

Figured it out, wasn't as hard as I thought it will be:

        <xsl:variable name="latest">
          <xsl:for-each select="Info">
            <xsl:sort select="Date" order="descending" />
            <xsl:if test="position() = 1">
              <xsl:value-of select="Date"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:variable>
      <xsl:value-of select="$latest"/>

In XSLT 2.0 or later, you shouldn't need to sort at all; you can use max()...

<xsl:value-of select="max(//Date/xs:date(.))"/>

XSLT 2.0+: <xsl:perform-sort> is used when we want to sort elements without processing the elements individually. <xsl:sort> is used to process elements in sorted order. Since you just want the last date in this case, you do not need to process each <Info> element. Use <xsl:perform-sort>:

<xsl:variable name="sorted_dates">
  <xsl:perform-sort select="Info/Date">
     <xsl:sort select="."/>
  </xsl:perform-sort>
</xsl:variable>

<xsl:value-of select="$sorted_dates/Date[last()]"/>

Tags:

Xml

Sorting

Xslt