如何在通过XSLT转换XML时转换dateTime

时间:2017-02-07 18:29:07

标签: datetime xslt transform

如果我在GMT中获得格式为dateTime的格式,我该如何在XSLT中转换dateTime

XML输入:

 <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是:

<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>

我需要使用“dateTime的转换来构建如下所示的行。那是当我得到dateTime的值时,它应该转换并构建行

预期产出:

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

1 个答案:

答案 0 :(得分:0)

在XSLT 2.0中,您可以执行以下操作:

brew doctor

接收:

<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>