XSL-T 3.0 中的 XPath 3.1 如何像 java.text.SimpleDateFormat 一样将自由格式字符串解析为 DateTime? [英] XPath 3.1 in XSL-T 3.0 How to parse free format String to DateTime as in java.text.SimpleDateFormat?

查看:52
本文介绍了XSL-T 3.0 中的 XPath 3.1 如何像 java.text.SimpleDateFormat 一样将自由格式字符串解析为 DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 XSL-T 3.0 中将几乎免费的格式字符串解析为 DateTime,因为可以使用 java.text.SimpleDateFormat 在 Java 中完成.是否可以?

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?

我正在使用最新的用于 Java 的 Saxon HE 9.7.0.1,并通过 W3C CR 3.1XPath 和 XQuery 函数和运算符 3.1"咨询它.W3C CR 3.1 中有函数fn:parse-ietf-date",但它看起来不能像6.1.94 7:29"那样解析字符串 - 异常:

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:

在 tr.xsl 的第 20 行第 47 列 xsl:value-of/@select 中的字符 17 处出错:FORG0010:无效的 IETF 日期值 6.1.94 7:29(缺少日期分隔符)在内置模板规则中无效的 IETF 日期值 6.1.94 7:29(缺少日期分隔符)

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)

我是否在 XSL-T 中犯了任何错误,或者fn:parse-ietf-date"不支持将更多格式的字符串解析为 DateTime?

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?

如果问题不在我这边,是否可以将 fn:parseTime 之类的函数添加到 W3C CR 3.1 作为 Java 8 java.text.SimpleDateFormat 类的副本",并支持其所有 Date 和时间模式?它可以解决 String 到 DateTime 的解析(我希望永远).它也已经在 J​​ava 中发明并被广泛使用.我非常感谢在 Saxon-HE 中实现它,尽管我知道,在我的 XSL-T 脚本中调用 Saxon-PE 或 Saxon-EE 中的 java.text.SimpleDateFormat 应该很容易.

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.

问候,斯捷潘

我的用例:

外壳:

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>

推荐答案

函数 parse-ietf-date 专门用于解析各种 IETF 标准中使用的日期,这些标准以美国为中心(例如,它们总是表示月份作为英文月份名称,并使用 (month, day, year) 顺序.它不是为任意格式的日期设计的通用解决方案.

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.

您的选择基本上是 (a) 通过扩展函数调用 Java 中的通用日期解析器,或者 (b) 编写 XSLT 代码来解析您想要处理的特定日期格式.

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.

多年来在 EXSLT 日期处理库中定义了一个 parse-date() 函数:参见 http://exslt.org/date/functions/parse-date/index.html - 但它没有被广泛实施,也没有在撒克逊人中实施.

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.

这篇关于XSL-T 3.0 中的 XPath 3.1 如何像 java.text.SimpleDateFormat 一样将自由格式字符串解析为 DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆