Formatting dates using XPath

半城伤御伤魂 提交于 2020-01-03 19:41:52

问题


I have the following xpath expressions...

//ns:response[1]/ns:return[1]/legs[1]/startDate[1] (Value 01/01/2011)
//ns:response[1]/ns:return[1]/legs[1]/startTime[1] (Value 12:13)

I need to format and concat these values into something like this

2011-08-25T17:35:00

Is this possible to do using xpath functions? An example would be appreciated.

The date format in the input data is dd/mm/yyyy.


回答1:


As by @Michael Key suggestion (+1), three substring() and one concat() is all what you need. Check at this XSLT example using the XPath you are searching for (use of variables to make expression readable):

<xsl:template match="/">
    <xsl:variable name="sD" select="'01/01/2011'"/>
    <xsl:variable name="sT" select="'12:13'"/>
    <xsl:value-of select="concat(
        substring($sD,7),'-',
        substring($sD,4,2),'-',
        substring($sD,1,2),'T',
        $sT,':00')"/>
</xsl:template>



回答2:


It would help to know whether your 01/01/2011 is d/m/y or m/d/y. Either way, it's just a question of calling substring() three times to extract the parts of the data, and then concat() to build up the result.




回答3:


Look at XPath 1.0 functions: concat, substring, substring-before, substring-after.



来源:https://stackoverflow.com/questions/7187564/formatting-dates-using-xpath

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