How to parse string to date in xslt 2.0

痴心易碎 提交于 2019-12-17 16:45:09

问题


Is it possible to convert strings like 30042013 (30 April 2013) to a date format?

So I can use it later in functions like format-date


回答1:


fn:dateTime($arg1 as xs:date?, $arg2 as xs:time?) will convert its arguments to xs:dateTime.

Just use fn:substring() and fn:concat() to cut out the relevant parts and join them as yyyy-mm-dd before passing that to fn:dateTime.




回答2:


Like Tomalak said, you can use substring() and concat() to build a string you can cast as an xs:date() (It doesn't sound like you want a dateTime.)

Example:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="in" select="'30042013'"/>

    <xsl:template match="/">
        <xsl:variable name="date" select="xs:date(concat(
            substring($in,5,4),'-',
            substring($in,3,2),'-',
            substring($in,1,2)))"/>
        <xsl:value-of select="format-date($date,'[MNn] [D], [Y]')"/>
    </xsl:template>

</xsl:stylesheet>

produces (with any XML input)

April 30, 2013


来源:https://stackoverflow.com/questions/16851726/how-to-parse-string-to-date-in-xslt-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!