1
votes

I would like to parse almost free format String to DateTime in XSL-T 3.0 as one can do it in Java using java.text.SimpleDateFormat. Is it possible?

I am using the latest Saxon HE 9.7.0.1 for Java and consulting it with the W3C CR 3.1 "XPath and XQuery Functions and Operators 3.1". There is the function "fn:parse-ietf-date" in W3C CR 3.1, but it does not look like it can parse String like "6.1.94 7:29" - the Exception:

Error at char 17 in xsl:value-of/@select on line 20 column 47 of tr.xsl: FORG0010: Invalid IETF date value 6.1.94 7:29 (Date separator missing) in built-in template rule Invalid IETF date value 6.1.94 7:29 (Date separator missing)

Do I make any error in my XSL-T or "fn:parse-ietf-date" does not support more formats of String to be parsed to DateTime?

If the problem is not on my side, could it be possible to add function like fn:parseTime to the W3C CR 3.1 as the "copy" of Java 8 java.text.SimpleDateFormat class with the support of all its Date and Time Patterns? It could solve the parsing of String to DateTime (I hope forever). Also it is already invented in Java and used widely. Implementing it in Saxon-HE would be very appreciated by me, although I know, it should be quite easy to call java.text.SimpleDateFormat in Saxon-PE or Saxon-EE in my XSL-T script.

Regards, Stepan

My use-case:

shell:

java -jar .\saxon9he.jar -t -s:.\in.xml -xsl:.\tr.xsl -o:.\out.xml

in.xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>6.1.94 7:29</root>

tr.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:math="http://www.w3.org/2005/xpath-functions/math"
  exclude-result-prefixes="xs math"
  version="3.0">
  <xsl:template
    match="/root">
    <xsl:element
      name="root">
      <xsl:element
        name="originalValue">
        <xsl:value-of
          select="./text()"/>
      </xsl:element>
      <xsl:element
        name="newValue">
        <xsl:value-of
          select="parse-ietf-date(./text())"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
1

1 Answers

3
votes

The function parse-ietf-date is specifically for parsing dates used in various IETF standards, which are rather US-centric (for example, they always express the month as an English month name, and use (month, day, year) order. It was not designed as a general-purpose solution for dates in arbitrary formats.

You choice is basically either (a) to invoke a general-purpose date parser in Java, via extension functions, or (b) to write XSLT code to parse the specific date formats you want to handle.

A parse-date() function has been defined in the EXSLT date handling library for years: see http://exslt.org/date/functions/parse-date/index.html - but it has not been widely implemented, and is not implemented in Saxon.