user3388999
user3388999

Reputation: 15

get earliest date

Need to get the closest date to the general list of dates, the current date is displayed in the tag current_date

xslt version 1.0

<current_date>04.04.2014 13:00:00</current_date>
<property_value id="8">
    <property_id>96</property_id>
    <entity_id>237</entity_id>
    <property_dir_id>0</property_dir_id>
    <tag_name>event_date</tag_name>
    <value>19.04.2014 18:00:00</value>
</property_value>
<property_value id="9">
    <property_id>96</property_id>
    <entity_id>237</entity_id>
    <property_dir_id>0</property_dir_id>
    <tag_name>event_date</tag_name>
    <value>05.05.2014 22:00:00</value>
</property_value>
<property_value id="10">
    <property_id>96</property_id>
    <entity_id>237</entity_id>
    <property_dir_id>0</property_dir_id>
    <tag_name>event_date</tag_name>
    <value>07.06.2014 17:00:00</value>
</property_value>

Upvotes: 0

Views: 163

Answers (1)

Thomas W
Thomas W

Reputation: 15371

I'm not sure what you mean by "earliest" and "closest". However, you can sort the dates using the following technique:

<xsl:variable name="sortedDateCsvList">
  <xsl:for-each select="property_value/value | current_date">
    <xsl:sort select="substring(.,7,4)"/><!-- year -->
    <xsl:sort select="substring(.,4,2)"/><!-- month -->
    <xsl:sort select="substring(.,1,2)"/><!-- day -->
    <xsl:sort select="substring(.,12)"/><!-- time -->
    <xsl:value-of select="concat(., ',')"/>
  </xsl:for-each>
</xsl:variable>

You can then use the variable to do several things:

  • extract the oldest date:

    substring-before($sortedDateCsvList, ',')

  • extract the closest date after current_date:

    substring-before(substring-after($sortedDateCsvList, current_date), ',')

Or, you can get the youngest date and the closest date before current_date if you add the attribute order="descending" to all <xsl:sort> elements.

Upvotes: 1

Related Questions