如何在通过XSLT转换XML时转换dateTime [英] How to convert dateTime while transforming XML via XSLT

查看:157
本文介绍了如何在通过XSLT转换XML时转换dateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我以下列格式获得GMT中的 dateTime ,请在XSLT中转换 dateTime

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

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 的值时,它应该转换并构建行

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

预期输出:

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


推荐答案

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

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>

收到:

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

这篇关于如何在通过XSLT转换XML时转换dateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆