如何将datetime转换为ISO 8601格式

时间:2017-07-12 14:28:53

标签: xml xslt

我在XML文件的字段中有一个日期值,格式为:

<date>2017-05-01 00:00:00</date>

我想将其转换为标准的XSD格式:

2017-05-01T00:00:00.000Z

我目前正在使用3.0版。

我的样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
version="3.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
    <xsl:apply-templates select="node()" />
</xsl:template>

<xsl:variable name="theDate" select="/item/date" />

<xsl:template match="/" name="xsl:initial-template">
    <Bundle>
        <thedate>
            <xsl:attribute name="value"><xsl:value-of select="$theDate"/></xsl:attribute>
        </thedate>
    </Bundle>
</xsl:template>

XML输入

<?xml version="1.0" encoding="UTF-8"?>
<item>
    <date>2017-05-01 00:00:00</date> 
</item>

希望输出看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<Bundle xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <thedate value="2017-05-01T00:00:00.000Z"/>
</Bundle>

1 个答案:

答案 0 :(得分:0)

<xsl:value-of select="translate($theDate,' ', 'T')"/>+000Z