Xml XSLT将字符串更改为日期
我试图获取属性@start和@stop,并使用XSLT将其更改为日期 这是一个非常奇怪的日期格式,我无法更改(学校作业)Xml XSLT将字符串更改为日期,xml,xslt,Xml,Xslt,我试图获取属性@start和@stop,并使用XSLT将其更改为日期 这是一个非常奇怪的日期格式,我无法更改(学校作业) 我已经走了这么远 <xsl:variable name="start" select="@start"/> <xsl:variable name="stop" select="@stop"/>
我已经走了这么远
<xsl:variable name="start" select="@start"/>
<xsl:variable name="stop" select="@stop"/>
<xsl:value-of select="format-dateTime($start, '')"/>
我在向format dateTime函数提供第二个参数时遇到了一个问题
你知道如何格式化吗?所以你有一个格式为
20181011154000+0200
的输入字符串,XSLT/XPath 2.0xs:date
或xs: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>
Start{mf:date-time字符串到dateTime(@Start)},end{mf:date-time字符串到dateTime(@stop)}
因此您有一个格式为
20181011154000+0200
的输入字符串,XSLT/XPath 2.0xs:date
或xs: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>
Start{mf:date-time字符串到dateTime(@Start)},end{mf:date-time字符串到dateTime(@stop)}