XSLT:函数内的日期计算[format-dateTime()]

时间:2012-05-31 12:38:58

标签: xslt xslt-2.0

我想在format-dateTime()函数中进行日期计算并进行XSL转换。这是我的XSL文件

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://www.abc.com/ws/integration/toolkit/2005/07" xmlns:fct="http://www.taleo.com/xsl_functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="ns:field[@name = 'EndDate']">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:value-of select="fct:format-considering-timezone(xs:dateTime(.))"/>
    </xsl:copy>
</xsl:template>
<xsl:function name="fct:format-considering-timezone">
    <xsl:param name="dateTime" as="xs:dateTime"/>
    <xsl:variable name="dst">
        <xsl:value-of select="fct:get-timezone($dateTime)"/>
    </xsl:variable>
    <xsl:variable name="date-format">
        <xsl:choose>
            <xsl:when test="$dst = 'PT11H'">[Y,4-4]-[M,2-2]-[D,2-2]02:00:00T-05:00</xsl:when>
            <xsl:otherwise>[Y,4-4]-[M,2-2]-[D,2-2]00:00:00T-05:00</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>               
    <xsl:variable name="Duration">
        <xsl:choose>
            <xsl:when test="ns:field[@name='CareerSiteNo3'] = 'xyz'">P2D</xsl:when>
            <xsl:otherwise>P14D</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="AUTime">
        <xsl:value-of select="adjust-dateTime-to-timezone($dateTime + xs:dayTimeDuration($Duration), $dst)"/>
    </xsl:variable>
    <xsl:value-of select="format-dateTime((xs:datetime($AUTime) + xs:dayTimeDuration($Duration)), $date-format)"/>
</xsl:function>
<xsl:function name="fct:get-timezone" as="xs:dayTimeDuration">
    <xsl:param name="dateTime" as="xs:dateTime"/>
    <xsl:variable name="timezone">
        <xsl:choose>
            <xsl:when test="$dateTime gt xs:dateTime('2030-10-06T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2030-04-07T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2029-10-07T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2029-04-01T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2028-10-01T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2028-04-02T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2027-10-03T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2027-04-04T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2026-10-04T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2026-04-05T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2025-10-05T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2025-04-06T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2024-10-06T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2024-04-07T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2023-10-01T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2023-04-02T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2022-10-02T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2022-04-03T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2021-10-03T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2021-04-04T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2020-10-04T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2020-04-05T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2019-10-06T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2019-04-07T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2018-10-07T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2018-04-01T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2017-10-01T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2017-04-02T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2016-10-02T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2016-04-03T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2015-10-04T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2015-04-05T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2014-10-05T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2014-04-06T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2013-10-06T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2013-04-07T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2012-10-07T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2012-04-01T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2011-10-02T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2011-04-03T03:00:00+10:00')">PT10H</xsl:when>
            <xsl:when test="$dateTime gt xs:dateTime('2010-10-03T02:00:00+11:00')">PT11H</xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(' Error - dateTime : ', $dateTime, ' out of range. Need to extend the list of DST datetimes.')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="xs:dayTimeDuration($timezone)"/>
</xsl:function>

此代码导致错误 “TransformerConfigurationException:无法编译样式表。检测到1个错误。”。

以下是我想要转换的输出:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Body>
    <getDocumentByKeyResponse xmlns="http://www.abc.com/ws/integration/toolkit/2005/07" xmlns:ns1="http://www.abc.com/ws/integration/toolkit/2005/07" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <Document>
            <Attributes>
                <Attribute name="duration">0:00:00.086</Attribute>
                <Attribute name="count">2</Attribute>
                <Attribute name="entity">SourcingRequest</Attribute>
                <Attribute name="mode">XML</Attribute>
                <Attribute name="version">http://www.taleo.com/ws/tee800/2009/01</Attribute>
            </Attributes>
            <Content>
                <ExportXML>
                    <record>
                        <field name="Identifier">00000abc</field>
                        <field name="ContestNumber">00000abc</field>
                        <field name="CareerSiteNo3">xyz</field>
                        <field name="OpeningDate">2012-02-15</field>
                        <field name="EndDate">2012-02-1602:00:00T-05:00</field>
                    </record>
            </Content>
        </Document>
    </getDocumentByKeyResponse>
</Body>

当我改变部分时 像此<xsl:value-of select="format-dateTime((xs:datetime($AUTime) + xs:dayTimeDuration($Duration)), $date-format)"/> <xsl:value-of select="format-dateTime($AUTime, $date-format)"/>,它不包含数据计算,转换过程成功。

是否无法在format-dateTime()>内进行计算?

非常感谢

1 个答案:

答案 0 :(得分:0)

可以进行计算以提供函数的参数,但是,xpath的'+'运算符不能像你期望的那样在日期时间上运行。

查看http://www.w3.org/TR/2004/WD-xpath-functions-20041029/#dateTime-arithmetic