[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XSLT 2.0/XPath 2.0 Date arithmetic

Subject: RE: XSLT 2.0/XPath 2.0 Date arithmetic
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 18 May 2006 08:27:54 +0100
date xsl
> Given a parameter, let's call it "today" in the form of this 
> string "20060517", how do I create a variable, let's call it 
> "tMinus1" such that it represents a day earlier than 
> "20060517", that would be "20060516". So long as "$today" 
> isn't the first day of a month, a simple subtraction and 
> followed by a type cast that I don't grasp would do the trick.
> 
> What I'm looking for is guidance on date arithmetic.

You'd be much better off working with the xs:date type, which uses the
format 2006-05-17.

So, two functions to convert between your non-standard dates and standard
xs:date objects:

<xsl:import-schema>
  <xs:schema target-namespace="http://my-date">
    <xs:simpleType name="yyyymmdd-date">
      <xs:restriction base="xs:string">
        <xs:pattern value="[0-9]{4}[0-1][0-9][0-3][0-9]"/>
      </
    </
  </
</
      

<xsl:function name="f:to-iso-date" as="xs:date">
  <xsl:param name="in" as="my:yyyymmdd-date"/>
  <xsl:sequence select="xs:date(replace($in, '(\d{4})(\d{2})(\d{2})',
'$1-$2-$3'))"/>
</xsl:function>

<xsl:function name="f:to-yyyymmdd-date" as="my:yyyymmdd-date">
  <xsl:param name="in" as="xs:date"/>
  <xsl:sequence select="my:yyyymmdd-date(translate(string($in, '-', ''))"/>
</xsl:function>

then:
   select="f:to-yyyymmdd-date(f:to-iso-date($input-date) -
xs:dayTimeDuration('PT1D'))"

If you're not schema-aware, then use xs:string in place of my:yyyymmdd-date
- all you lose is type-checking.

Michael Kay
http://www.saxonica.com/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.