在 XSLT 1.0 中格式化 24 小时时间的最佳方法是什么?

发布于 2024-07-07 00:22:31 字数 685 浏览 7 评论 0原文

我很难找到采用时间格式并轻松确定其是否有效的好方法,然后使用 XSLT 1.0 生成具有某种格式的结果元素。

给定以下 xml:

<root>
    <srcTime>2300</srcTime>
</root>

生成结果 xml 会很棒:

<root>
    <dstTime>23:00</dstTime>
</root>

但是,如果源 xml 包含无效的 24 小时时间格式,则生成的 dstTime 元素应为空。

例如,当无效的源 xml 如下时:

<root>
    <srcTime>NOON</srcTime>
</root>

生成的 xml 应该是:

<root>
    <dstTime></dstTime>
</root>

问题是,可编写以产生所需结果的最佳 XSLT 1.0 片段是什么? 希望保持它非常简单,而不必每次都解析(即,如果可能的话,模式匹配会很甜蜜)。

I've had a hard time finding good ways of taking a time format and easily determining if it's valid then producing a resulting element that has some formatting using XSLT 1.0.

Given the following xml:

<root>
    <srcTime>2300</srcTime>
</root>

It would be great to produce the resulting xml:

<root>
    <dstTime>23:00</dstTime>
</root>

However, if the source xml contains an invalid 24 hour time format, the resulting dstTime element should be blank.

For example, when the invalid source xml is the following:

<root>
    <srcTime>NOON</srcTime>
</root>

The resulting xml should be:

<root>
    <dstTime></dstTime>
</root>

The question is, what's the best XSLT 1.0 fragment that could be written to produce the desired results? The hope would be to keep it quite simple and not have to parse the every piece of the time (i.e. pattern matching would be sweet if possible).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生活了然无味 2024-07-14 00:22:33

为了使列表完整,XSLT 中还有 日期/时间处理模块 部分Steve Ball 的标准库。

And to have the list complete, there is also Date/Time processing module part of XSLT Standard Library by Steve Ball.

夏日落 2024-07-14 00:22:32

即使是 exslt.org time() 函数在这里也无法为您提供帮助,因为它期望其输入采用正确的格式(xs:dateTime 或 xs:time)。

这是最好在 XSLT 之外解决的问题。 我这样说是因为我经常使用 XSLT 来做一些 XSLT 并非真正设计用途的事情,并且设法让事情正常工作。 它实际上并不是为解析字符串而设计的。

理想的解决方案是修复生成 XML 文档的任何内容,以便使用专门为此目的而方便建立的国际标准来格式化时间,使用的原则是,如果可以避免这样做,则不应保留或传输垃圾数据。

但如果这是不可能的,您应该在将数据传递到 XSLT 之前修复数据,或者在生成转换的输出之后修复它。

Even the exslt.org time() function won't help you here, because it expects its input to be in the proper format (xs:dateTime or xs:time).

This is something that is best fixed outside of XSLT. I say this as someone who routinely uses XSLT to do things it wasn't really designed for and manages to get things working. It was really not designed to parse strings.

The ideal solution is to fix whatever is producing the XML document so that it formats times using the international standard conveniently established just for that purpose, using the principle that you shouldn't persist or transmit crap data if you can avoid doing so.

But if that's not possible, you should either fix the data before passing it to XSLT or fix it after generating the transform's output.

Oo萌小芽oO 2024-07-14 00:22:31

根据实际使用的 xslt 处理器,您可以在自定义扩展函数中执行所需的操作(您必须自己创建)。

Xalan对扩展函数有很好的支持,你不仅可以用Java编写它们而且还可以使用 JavaScript 或 Apache BSF 支持的其他语言。

Microsoft 的 XSLT 引擎也支持自定义扩展,如 .NET Framework 开发人员指南中所述,扩展 XSLT 样式表

Depending on the actual xslt processor used you could be able to do desired operations in custom extension function (which you would have to make yourself).

Xalan has good support for extension functions, you can write them not only in Java but also in JavaScript or other languages supported by Apache BSF.

Microsoft's XSLT engine supports custom extensions as well, as described in .NET Framework Developer's Guide, Extending XSLT Style Sheets

时光是把杀猪刀 2024-07-14 00:22:31

看一下:
http://www.exslt.org/ 特别是“日期和时间”部分。
我还没有深入研究它,但看起来它可能就是您所寻找的。

Have a look at:
http://www.exslt.org/ specifically the "dates and times" section.
I haven't dug deep into it but it looks like it may be what your looking for.

陌伤浅笑 2024-07-14 00:22:31

XSLT 1.0 中没有任何正则表达式,因此恐怕无法进行模式匹配。

我不清楚 23:00 是否合法? 如果是,请尝试:

<dstTime>
  <xsl:if test="string-length(srcTime) = 4 or
                string-length(srcTime) = 5">
    <xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
    <xsl:if test="$hour >= 0 and 24 > $hour">
      <xsl:variable name="minute">
        <xsl:choose>
          <xsl:when test="string-length(srcTime) = 5 and
                          substring(srcTime, 3, 1) = ':'">
            <xsl:value-of select="substring(srcTime, 4, 2)" />
          </xsl:when>
          <xsl:when test="string-length(srcTime) = 4">
            <xsl:value-of select="substring(srcTime, 3, 2)" />
          </xsl:when>
        </xsl:choose>
      </xsl:variable>
      <xsl:if test="$minute >= 0 and 60 > $minute">
        <xsl:value-of select="concat($hour, ':', $minute)" />
      </xsl:if>
    </xsl:if>
  </xsl:if>
</dstTime>

如果不是,并且四位数字是唯一合法的,那么:

<dstTime>
  <xsl:if test="string-length(srcTime) = 4">
    <xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
    <xsl:if test="$hour >= 0 and 24 > $hour">
      <xsl:variable name="minute" select="substring(srcTime, 3, 2)" />
      <xsl:if test="$minute >= 0 and 60 > $minute">
        <xsl:value-of select="concat($hour, ':', $minute)" />
      </xsl:if>
    </xsl:if>
  </xsl:if>
</dstTime>

There aren't any regular expressions in XSLT 1.0, so I'm afraid that pattern matching isn't going to be possible.

I'm not clear if <srcTime>23:00</srcTime> is supposed to be legal or not? If it is, try:

<dstTime>
  <xsl:if test="string-length(srcTime) = 4 or
                string-length(srcTime) = 5">
    <xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
    <xsl:if test="$hour >= 0 and 24 > $hour">
      <xsl:variable name="minute">
        <xsl:choose>
          <xsl:when test="string-length(srcTime) = 5 and
                          substring(srcTime, 3, 1) = ':'">
            <xsl:value-of select="substring(srcTime, 4, 2)" />
          </xsl:when>
          <xsl:when test="string-length(srcTime) = 4">
            <xsl:value-of select="substring(srcTime, 3, 2)" />
          </xsl:when>
        </xsl:choose>
      </xsl:variable>
      <xsl:if test="$minute >= 0 and 60 > $minute">
        <xsl:value-of select="concat($hour, ':', $minute)" />
      </xsl:if>
    </xsl:if>
  </xsl:if>
</dstTime>

If it isn't, and four digits is the only thing that's legal then:

<dstTime>
  <xsl:if test="string-length(srcTime) = 4">
    <xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
    <xsl:if test="$hour >= 0 and 24 > $hour">
      <xsl:variable name="minute" select="substring(srcTime, 3, 2)" />
      <xsl:if test="$minute >= 0 and 60 > $minute">
        <xsl:value-of select="concat($hour, ':', $minute)" />
      </xsl:if>
    </xsl:if>
  </xsl:if>
</dstTime>
世界如花海般美丽 2024-07-14 00:22:31

XSLT 1.0 没有任何对日期/时间操作的标准支持。

您必须编写一个简单的解析和格式化函数。 这不会很简单,也不会很美好。

XSLT 确实是为树转换而设计的。 这种文本节点操作最好在 XSLT 之外完成。

XSLT 1.0 does not have any standard support for date/time manipulation.

You must write a simple parsing and formatting function. That's not going to be simple, and that's not going to be pretty.

XSLT is really designed for tree transformations. This sort of text node manipulations are best done outside of XSLT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文