XSLT将字符串更改为日期

时间:2018-10-19 11:04:25

标签: xml xslt

我正在尝试获取属性@start和@stop并使用XSLT将其更改为日期。

这是一种非常奇怪的日期格式,我无法更改(学校作业)。

<programme start="20181011154000 +0200" stop="20181011172000 +0200" channel="1.bluemovie.de" clumpidx="0/1">

我走了这么远

<xsl:variable name="start" select="@start"/>
                                        <xsl:variable name="stop" select="@stop"/>
                                        <xsl:value-of select="format-dateTime($start, '')"/>

在将第二个参数提供给format-dateTime函数时遇到问题。

有什么想法可以格式化吗?

1 个答案:

答案 0 :(得分:2)

因此,您有一个格式为20181011154000 +0200的输入字符串,要将XSLT / XPath 2.0 xs:datexs:dateTime转换为该字符串吗?我假设进入2018-10-11T15:40:00+02:00,如果您使用的格式一致,则可以使用replace函数为xs:dateTime构建正确的格式,例如

xs:dateTime(replace('20181011154000 +0200', '([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})\s([+-])([0-9]{2})([0-9]{2})', '$1-$2-$3T$4:$5:$6$7$8:$9'))

因此您可以编写一个函数

  <xsl:function name="mf:date-time-string-to-dateTime" as="xs:dateTime">
      <xsl:param name="input" as="xs:string"/>
      <xsl:sequence select="xs:dateTime(replace($input, '([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})\s([+-])([0-9]{2})([0-9]{2})', '$1-$2-$3T$4:$5:$6$7$8:$9'))"/>
  </xsl:function>

并使用它:

<?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:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:function name="mf:date-time-string-to-dateTime" as="xs:dateTime">
      <xsl:param name="input" as="xs:string"/>
      <xsl:sequence select="xs:dateTime(replace($input, '([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})\s([+-])([0-9]{2})([0-9]{2})', '$1-$2-$3T$4:$5:$6$7$8:$9'))"/>
  </xsl:function>

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="programme">
      <p>Start {mf:date-time-string-to-dateTime(@start)}, end {mf:date-time-string-to-dateTime(@stop)}</p>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/eiZQaG4