Yatan
Yatan

Reputation: 93

Compare julian date in XSLT

I have a requirement where I have to find out the most recent datetime in julian from list of dates by using XSLT.

If I pass the below XML

Input:

<Orders>
<Order>
<OrderNumber>100</OrderNumber>
<Date>116256</Date>
</Order>
<Order>
<OrderNumber>101</OrderNumber>
<Date>116256</Date>
</Order>
<Order>
<OrderNumber>102</OrderNumber>
<Date>116276</Date>
</Order>
</Orders>

Output:

<Result>
<Date>116256</Date>
</Result>

Please help me with the XSLT.

Thanks Yatan

Upvotes: 1

Views: 267

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117003

Assuming that your "dates" are numbers, and that those numbers increase as time goes by, you only have to sort the Orders by Date, descending, and get the Date of the first one:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Orders">
    <Result>
        <xsl:for-each select="Order">
            <xsl:sort select="Date" data-type="number" order="descending"/>
            <xsl:if test="position()=1">
                <xsl:copy-of select="Date"/>
            </xsl:if>
        </xsl:for-each>
    </Result>
</xsl:template>

</xsl:stylesheet>

Applied to your example input, the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<Result>
   <Date>116276</Date>
</Result>

This is not the result you have in your question - but under the assumptions stated above, it is the correct one.

Upvotes: 3

Related Questions