Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting dates using XPath

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.

like image 338
Remotec Avatar asked Feb 24 '23 04:02

Remotec


2 Answers

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>
like image 140
Emiliano Poggi Avatar answered Feb 25 '23 19:02

Emiliano Poggi


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.

like image 44
Michael Kay Avatar answered Feb 25 '23 18:02

Michael Kay