xsl-list
[Top] [All Lists]

RE: [xsl] XSLT 2.0/XPath 2.0 Date arithmetic

2006-05-18 00:28:17

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/



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--