繁体   English   中英

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

[英]XSLT 2.0 : Addition of Time and on-on match

首先,我将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>

希望能提供快速帮助。

要添加时间值,我建议

<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声明。

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

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

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

  <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>

做到这一点。

在线示例位于http://xsltransform.hikmatu.com/nc4NzPS

请注意,原始输入样本的空Hours元素,要处理它们,需要一些额外的规范,以了解如何将它们转换为时间或持续时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM