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

dateTime fun

Subject: dateTime fun
From: "James A. Robinson" <jim.robinson@xxxxxxxxxxxx>
Date: Wed, 20 Dec 2006 20:24:29 -0800
datetime with xslt
So I'm dealing with a schema which has an attribute:

  <xs:simpleType  name="time">
    <xs:union  memberTypes="
          xs:gYear xs:gYearMonth xs:date xs:dateTime"  />
  </xs:simpleType>

I had to write a function to let someone compare two of these 'time'
simple types.  It appeared as though the only way to do comparisions
was with equal times, so that mean converting everything to xs:dateTime.

What I ended up doing seems... ugly. Is there a nicer way to handle the
problem of comparing two 'time' types where we don't know ahead of time
whether we're comparing a gYear vs. a gYearMonth or an xs:date vs. and
xs:dateTime, etc?  Is there a way around doing the cast to xs:dateTime,
or a way I can better handle doing that case (the gYear and gYearMonth
bits are especially objectionable):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:f="uri.local-functions">

  <xsl:template match="/">
    <xsl:sequence select="f:a-lt-b(
      f:dateTime('2008+08:00'), f:dateTime('2009'))"/>
  </xsl:template>

  <xsl:function name="f:dateTime">
    <xsl:param name="t" as="xs:string"/>
    <xsl:sequence select="
      if ($t castable as xs:date)
      then xs:dateTime(xs:date($t))
      else if ($t castable as xs:gYearMonth)
      then (xs:dateTime(
        xs:date(concat(substring($t, 1, 7), '-01', substring($t, 8)))))
      else if ($t castable as xs:gYear)
      then (xs:dateTime(
        xs:date(concat(substring($t, 1, 4), '-01-01', substring($t, 5)))))
      else xs:dateTime($t)"/>
  </xsl:function>
 
  <xsl:function name="f:a-lt-b" as="xs:boolean">
    <xsl:param name="a" as="xs:dateTime"/>
    <xsl:param name="b" as="xs:dateTime"/>
    <xsl:sequence select="$a lt $b"/>
  </xsl:function>

</xsl:stylesheet>


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson@xxxxxxxxxxxx
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

Current Thread
  • dateTime fun
    • James A. Robinson - 21 Dec 2006 04:24:47 -0000 <=

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.