XSLT 2.0:添加时间和即时匹配

时间:2018-10-06 09:05:03

标签: xml xslt

首先,我将XSLT 2.0与SAXON-HE 9.5.1.5一起使用。

  1. 以下命令是否可以替代?

    <xsl:mode on-no-match="shallow-copy"/>
    
  2. 在我的输入XML中,有一个时间字段将以HH:MM格式显示。我想添加它,结果格式也将仅是HH:MM格式。

输入XML

    <Root>
     <Detail>
      <Time>24:00</Time>
     <Detail>
     <Detail>
      <Time>59:10</Time>
     <Detail>
     <Detail>
      <Time>4:59</Time>
     <Detail>
     <Detail>
      <Time></Time>
     <Detail>
     <Detail>
     <Detail>
    <Root>

感谢您的快速帮助。

2 个答案:

答案 0 :(得分:2)

要添加时间值,我建议

<xsl:variable name="totalTime"
   select="sum(Detail/Time ! 
                xs:dayTimeDuration(replace(., '(\d+):(\d+)', 'PT$1H$2M')))"/>

<xsl:value-of select="hours-from-duration($totalTime), 
                      format-number(minutes-from-duration($totalTime), '00')"
              separator=":"/>

将时间转换为持续时间的另一种方法是添加“:00”,转换为xs:time,然后减去xs:time('00:00:00')

关于xsl:mode,如果升级到最新版本(9.8或9.9),则可以在Saxon-HE中使用XSLT 3.0 xsl:mode声明。

答案 1 :(得分:1)

<xsl:mode on-no-match="shallow-copy"/>是在https://www.w3.org/TR/xslt-30/#built-in-templates-shallow-copy中定义的,基本上,对于单个未命名模式,您可以在XSLT 2或1中替换它,而无需使用身份转换进行流传输(另请参见{ {3}})模板:

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

我认为,关于格式化根据XSLT 2中转换为xs:dayTimeDuration s的时间值之和计算出的持续时间,

  <xsl:function name="mf:format-duration" as="xs:string">
      <xsl:param name="duration" as="xs:dayTimeDuration"/>
      <xsl:sequence select="concat(format-number(xs:integer(floor($duration div xs:dayTimeDuration('PT1H'))), '00'), ':', format-number(minutes-from-duration($duration), '00'))"/>
  </xsl:function>

做到了。

https://www.w3.org/TR/xslt20/#shallow-copy上的在线示例。

请注意,您的原始输入样本中有空的Hours元素,要对它们进行一些其他规范,以了解如何将它们转换为时间或持续时间。

相关问题