tags:

views:

1443

answers:

1

I'm kind of new to XSLT, and I've gotten basic transformation done. Next I want to try out date manipulations, since my data will have timestamps. However, I can't seem to get any date functions to work, and it greatly frustrates me. I'm testing using Firefox 3.5, xsltproc 1.1.24, xalan 1.10, and XMLSpy 2009, and they all say that the functions I'm trying to use don't exist.

My xml looks like so:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="datetime.xsl"?>

<watcher>
  <event id="1" date="2009-09-04T13:49:10-0500" type="ABCD">This is a test  </event>
</watcher>
</code>

My xsl looks like so:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

<xsl:template match="event[@type='ABCD']">
<!--            Date: <xsl:value-of select="day-from-dateTime(xs:dateTime(@date))"/> -->
<!--            Date: <xsl:value-of select="day-from-dateTime(@date)"/> -->
                Date: <xsl:value-of select="fn:day-from-dateTime(@date)"/>
</xsl:template>

</xsl:stylesheet>

If I make the stylesheet version 2, XMLSpy complains that it can't cast my date: XSLT 2.0 Debugging Error: Error in XPath 2.0 expression (Cast failed, invalid lexical value - xs:dateTime '2009-09-04T13:49:10-0500')

If I leave it as version 1, it complains about a different error: XSLT 1.0 Debugging Error: Error in XPath expression (Unknown function - Name and number of arguments do not match any function signature in the static context - 'day-from-dateTime')

Anytime I try to change the XSL to use a namespace, such as fn:day-from-dateTime, it refuses to work at all, with all of my parsers saying that The function number 'http://www.w3.org/2005/02/xpath-functions%3Aday-from-dateTime' is not available and variants thereof. I know from other tests that I can use the substring() function perfectly, without needing any namespace prefix, and I believe it's in the same namespace as day-from-dateTime.

I feel like it's something incredibly easy, since all of the tutorials show functions being used, but something seems to be eluding me. Could someone show me what I'm missing?

+4  A: 

Ouch, nasty versions thing going on here. A lot of the issues you're seeing will be because the XSLT processor you're using doesn't support XPath 2.0, which is where that day-from-dateTime function comes from.

I can get what you're trying to do to work, with a Saxon processor - Saxon-B 9.1.0.6 as my processor instead of Xalan. (Xalan appears to support XPath 1.0 only, according to the documentation)

There are a few errors in your documents:

The source document should have the timezone as 05:00, not 0500

<?xml version="1.0" encoding="UTF-8"?>
<watcher>
    <event id="1" date="2009-09-04T13:49:10-05:00" type="ABCD">This is a test  </event>
</watcher>

The XSLT should cast the string 2009-09-04T13:49:10-05:00 into a xs:dateTime, which is what type the argument of day-from-dateTime needs to be.

Date: <xsl:value-of select="day-from-dateTime(xs:dateTime(@date))"/>

And then it works

<?xml version="1.0" encoding="UTF-8"?>



        Date: 4

Hope that helps,

Brabster
After changing the date format and the XSL per your suggestions, XMLSpy works. However, Firefox and my CLI utilities still complain that the function doesn't exist. xsltproc doesn't know any of the functions, though I've heard it doesn't support XSLT 2. Xalan only complains about day-from-dateTime, not xs:dateTime().How come I need the namespace for xs, but not for fn?
hufman
OK - to the best of my knowledge:Xalan only supports XPath 1.0, which means that is doesn't know about a function called day-from-dateTime.Processors that do support XPath 2.0 have day-from-dateTime built in, so you can declare a namespace prefix and URI explicitly, or just use the function name as it's built in.You need the namespace prefix for the datatypes because that isn't built in in the same way as the XPath 2.0 functions.
Brabster
Unless you use only XPath 2.0 supporting libraries, you will have problems using XPath 2.0 builtins. If you can't use exclusively XPath 2.0 libs, you can create a custom function library that re-implements the date and time handling functionality you need. I think FunctX might help here. http://www.xqueryfunctions.com/xq/fn_day-from-datetime.html
Brabster
I guess since I'm targeting Firefox, I need to stick with XPath 1.Thank you!
hufman