2017-04-11 100 views
0

的ISO 8601的格式和更新值我有一个XML象下面这样:转换日期为节点XSLT

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<properties> 
<entry key="date">11-15-2017 22:45:59</entry> 
</properties> 

我要更改日期格式为ISO 8601,并使用XSLT更新日期变量的值。 输出XML应该是这样的

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<properties> 
<entry key="date">11-15-2017T22:45:59Z</entry> 
</properties> 

我已经定义与转换日期值的变量。我正在使用这个xslt,但没有得到所需的输出。

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

<xsl:variable name="dateparam" select="properties/entry[@key='date']/@value"></xsl:variable> 
<xsl:template match="entry[@key='date']"> 
<xsl:copy> 
<xsl:apply-templates select="@*"/> 
<xsl:value-of select="concat(translate(normalize-space($dateparam), ' ', 'T'), 'Z')"/> 
</xsl:copy> 
</xsl:template> 

有人可以帮助我,因为我是XSLT的新手。

+0

'“属性/进入[@名称= '日期']/@ value =“'=>'”properties/entry [@key ='date']/@ value“',和''=>'',应该解析y我们的问题。 – AntonH

+1

我看不到''做了想要的转换,它merlely选择一个输入属性节点。由于符号' {$ dateparam}'只会在XSLT 3.0处理器中使用XSLT 2.0处理器在XSLT'version =“3.0”'和expand-text =“yes”'中输出变量,您可以使用' '。 –

+0

我在这里使用变量来转换格式 我没有得到dataparam的价值。 –

回答

1

要得到你要求的输出,你只需要改变你的xsl:variable到:

<xsl:variable name="dateparam" select="/properties/entry[@key='date']"/> 

然而,要求输出(11-15-2017T22:45:59Z)是不是一个有效的DateTime。

这是我会做什么:

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:variable name="dateparam" select="/properties/entry[@key='date']"/> 

    <xsl:template match="entry[@key='date']"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <xsl:value-of select="replace(normalize-space($dateparam), 
     '^(\d{2})-(\d{2})-(\d{4})\s+(.*)','$3-$1-$2T$4Z')"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

输出

<properties> 
    <entry key="date">2017-11-15T22:45:59Z</entry> 
</properties> 
+0

它很棒。感谢丹尼尔的解决方案。 –

0

尝试这样:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="entry[@key='date']"> 
    <xsl:variable name="dateparam" select="."/> 
    <xsl:variable name="dd" select="xs:date(substring-before(., ' '))"/> 
    <xsl:variable name="tt" select="xs:time(substring-after(., ' '))"/> 
    <xsl:variable name="dt" select="dateTime($dd, $tt)"/> 
    <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <xsl:value-of select="format-dateTime($dt, 
     '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]Z')"/> 
    </xsl:copy> 
    </xsl:template> 

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

顺便说一句:<xsl:text>{$dateparam}</xsl:text>将无法​​正常工作。

改为使用<xsl:value-of select="$dateparam"/>

+0

感谢您的回答。如果我想将日期09-20-2016 20:57:27更改为ISO标准。请向我建议更改。 –

+0

相应地更新了问题。 修改了您对此要求所建议的代码。