[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] dateTime fun


Subject: [xsl] dateTime fun
From: "James A. Robinson" <jim.robinson@xxxxxxxxxxxx>
Date: Wed, 20 Dec 2006 20:24:29 -0800

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
Keywords