How to convert dateTime while transforming XML via XSLT

試著忘記壹切 提交于 2019-12-13 00:26:02

问题


How should I convert dateTime in XSLT if I get the dateTime in GMT in below format

XML input:

 <items>          
        <item>
            <lastname>Lisa</lastname>
            <firstname>Rimpell</firstname>
            <checkintime>2017-02-05T05:40:00+03:00</checkintime>
            <chekouttime>2017-02-05T10:40:00+03:00</chekouttime>
            <address></address>
        </item> 
</items>

XSLT is:

<xsl:template match="/">
    <Response>
        <Data>
            <xsl:call-template name="Buildusers" />
        </Data>
    </Response>
</xsl:template>
<xsl:template name="Buildusers">
    <Rows>
        <xsl:for-each select="//items/item">       
            <Row Action="ADD">
                <xsl:value-of select="lastname" />
                |<xsl:value-of  select="firstname" />
                |<xsl:value-of select="checkintime" />
                |<xsl:value-of select="chekouttime" />
                |<xsl:value-of select="address" />
            </Row>               
        </xsl:for-each>
    </Rows>
</xsl:template>

i need to build the rows like below with the conversion of 'dateTime. That is when I get the value of dateTime it should convert and build the row

Expected output:

Lisa|Rimpell|2017-02-05 02:40:00|2017-02-05 07:40:00||

回答1:


In XSLT 2.0, you can do:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="xs my"> 
<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8" />
<xsl:strip-space elements="*" /> 

<xsl:function name="my:adjust-dateTime">
    <xsl:param name="dateTime"/> 
    <xsl:variable name="UTC" select="adjust-dateTime-to-timezone($dateTime, xs:dayTimeDuration('PT0H'))"/>
    <xsl:sequence select="format-dateTime($UTC, '[Y0001]-[M01]-[D01] [H01]:[m01]:[s01]')" />
</xsl:function>

<xsl:template match="/items">
    <Response>
        <Data>
            <Rows>
                <xsl:for-each select="item">
                    <Row Action="ADD">
                        <xsl:value-of select="lastname, firstname, my:adjust-dateTime(checkintime), my:adjust-dateTime(chekouttime), address" separator="|" />
                    </Row>               
                </xsl:for-each>
            </Rows>
        </Data>
    </Response>
</xsl:template>

</xsl:stylesheet>

to receive:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
   <Data>
      <Rows>
         <Row Action="ADD">Lisa|Rimpell|2017-02-05 02:40:00|2017-02-05 07:40:00|</Row>
      </Rows>
   </Data>
</Response>


来源:https://stackoverflow.com/questions/42097300/how-to-convert-datetime-while-transforming-xml-via-xslt

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