Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT calculating if date > a specified number of years

Status
Not open for further replies.

rarararaha

Technical User
Apr 23, 2009
2
BE
Hi,

I am very new to XML programming, so am looking for a quick fix. Basically, we have a ton of xml info submitted, and its handled by an xslt file. One of the fields has a product manufacture date in the form of 'YYYYMMDD' (which is then converted into 'YYYY-MM-DD' if that is relevent). What I really want to do though, is reject any submissions where the product age is greater than 5 years, with something like this as the error:

<Age_value broken-rule="Age_value element is more than 5 years old"/>

But what do I have as an 'if' statement?? I guess something that looks at <todays_date> and subtracts 5 years to see if it is more or less than the date submitted?

Any advice appreciated.
 
Dian's suggestion is spot on if you have XSLT 2 available.

If you are constrained to XSLT 1, then consider XSLT Cookbook published by O'Reilly. There you will find a full chapter of date routines, with downloadable examples. The basic approach is compute the number of days since 'day 0' (aka the start of the epoch) for both today's date and the product manufacture date, and see if the difference is larger than 1827 (which allows two leap years - to be generous). Not too difficult if you know where to look...

Tom Morrison
 
thanks dian - was going to print off the link you suggested and have a read but it was looking like 206 pages!

as for xslt1 or 2 - wish i knew.

(EDIT: it might be xslt1 if this is important: <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
i am now expected to pick up some xml issues at work, but with zero training! all i know is that the date arrives in this format:

<Manufacture_Date>20050425</Manufacture_Date>

it is then altered thus by the xslt (but i don't think this matters):

<xsl:template match="Manufacture_Date">
<!--Date expected yyyymmdd-->
<!--Transforms to yyyy-mm-dd-->
<xsl:variable name="manDate" select="."/>
<xsl:variable name="yyyymmdd">
<xsl:value-of select="substring($manDate,1,4)"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring($manDate,5,2)"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring($manDate,7,2)"/>
</xsl:variable>
<Registration_Date>
<xsl:value-of select="$yyyymmdd"/>
</Manufacture_Date>
</xsl:template>


and i would like it to be rejected when over 5 years old, returning an error similar to this one which is for another item:

<xsl:if test="$New_Used = 0">
<xsl:attribute name="broken-rule">Invalid New_Used type</xsl:attribute>
</xsl:if>
<xsl:value-of select="$New_Used"/>
</New_Used>
</xsl:template>


can anyone help???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top