0

这是我开始的 XML:

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Sick</leaveDescription>

<leaveCodeStr>SS</leaveCodeStr>

<balanceAmount>90.22</balanceAmount>

<leaveDescription>Vacation</leaveDescription>

<leaveCodeStr>VA</leaveCodeStr>

<balanceAmount>187.11</balanceAmount>

<leaveDescription>Winter Recess</leaveDescription>

<leaveCodeStr>WR</leaveCodeStr>

<balanceAmount>30</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

我正在使用此 XSLT 将其转换为我需要的格式:

<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xtt="urn:com.workday/xtt"
    xmlns:etv="urn:com.workday/etv"
    xmlns:wd="urn:com.workday.report/CR-INT486-Kuali_Trojan_Time-Absence_Balances-Outbound" 
    exclude-result-prefixes="wd xsl"
    >

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <document>
            <xsl:for-each select="wd:Report_Data/wd:Report_Entry/wd:All_Eligible_Time_Off_Plans_for_Worker">
                <leaveBalanceTotal>
                    <employeeId><xsl:value-of select="parent::node()/wd:employeeId"/></employeeId>
                    <uscId><xsl:value-of select="parent::node()/wd:uscId"/></uscId>
                    <leaveCodeId><xsl:value-of select="wd:leaveCodeStr"/></leaveCodeId>
                    <balanceAmount><xsl:value-of select="wd:balanceAmount"/></balanceAmount>
                    <effectiveDate><xsl:value-of select="parent::node()/wd:effectiveDate"/></effectiveDate>
                    <totalDaysService><xsl:value-of select="parent::node()/wd:totalDaysService"/></totalDaysService>
                    <lastPayEndDate><xsl:value-of select="parent::node()/wd:lastPayEndDate"/></lastPayEndDate>
                </leaveBalanceTotal>
            </xsl:for-each>
        </document>
    </xsl:template>
</xsl:stylesheet>

这是我得到的输出:

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Sick</leaveDescription>

<leaveCodeStr>SS</leaveCodeStr>

<balanceAmount>90.22</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Vacation</leaveDescription>

<leaveCodeStr>VA</leaveCodeStr>

<balanceAmount>187.11</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Winter Recess</leaveDescription>

<leaveCodeStr>WR</leaveCodeStr>

<balanceAmount>30</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

但是,我需要像这样将两个日期字段转换为 UNIX 毫秒,并将日期转换为 UNIX 毫秒:

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Sick</leaveDescription>

<leaveCodeStr>SS</leaveCodeStr>

<balanceAmount>90.22</balanceAmount>

<effectiveDate>1363060799000</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>1363060799000</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Vacation</leaveDescription>

<leaveCodeStr>VA</leaveCodeStr>

<balanceAmount>187.11</balanceAmount>

<effectiveDate>1363060799000</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>1363060799000</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Winter Recess</leaveDescription>

<leaveCodeStr>WR</leaveCodeStr>

<balanceAmount>30</balanceAmount>

<effectiveDate>1363060799000</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>1363060799000</lastPayEndDate>

</leaveBalanceTotal>

基本上我希望有人能告诉我如何转换这些日期。任何帮助,将不胜感激。

4

1 回答 1

2

You can get milliseconds since the Unix epoch by dividing the duration since 1970-01-01 by one millisecond.

For example, prefix xs: being bound to namespace URI http://www.w3.org/2001/XMLSchema, the following XPath 2.0 expression

(xs:dateTime('2013-02-14T23:59:59') - xs:dateTime('1970-01-01T00:00:00')) 
div xs:dayTimeDuration('PT0.001S')

evaluates to

1360886399000


Edited to add:

For parsing your dates, you might want to look into regular expression matching in XSLT 2.0. For example, you could write a function which allows you to parse your dates into xs:dateTime values.

<!-- parses a dateTime value from a string of format "mm/dd/yyyy hh:mm:ss" -->
<xsl:function name="foo:parseDateTime" as="xs:dateTime">
  <xsl:param name="input" as="xs:string"/>
  <xsl:variable name="dateTimeLiteral">
    <xsl:analyze-string select="normalize-space($input)" 
                        regex="(\d\d)/(\d\d)/(\d\d\d\d)\s(\d\d):(\d\d):(\d\d)">
      <xsl:matching-substring>
        <xsl:number value="regex-group(3)" format="0001"/>          
        <xsl:text>-</xsl:text>
        <xsl:number value="regex-group(2)" format="01"/>          
        <xsl:text>-</xsl:text>
        <xsl:number value="regex-group(1)" format="01"/>
        <xsl:text>T</xsl:text>
        <xsl:number value="regex-group(4)" format="01"/>
        <xsl:text>:</xsl:text>
        <xsl:number value="regex-group(5)" format="01"/>
        <xsl:text>:</xsl:text>
        <xsl:number value="regex-group(6)" format="01"/>
      </xsl:matching-substring>
    </xsl:analyze-string>
  </xsl:variable>
  <xsl:value-of select="xs:dateTime($dateTimeLiteral)"/>
</xsl:function>

You could write another function which encapsulates the above example of converting into milliseconds:

<!-- converts a dateTime into milliseconds since the beginning of 1970" -->
<xsl:function name="foo:toMilliseconds" as="xs:integer">
  <xsl:param name="input" as="xs:dateTime"/>
  <xsl:value-of select="(xs:dateTime($input)-xs:dateTime('1970-01-01T00:00:00'))
                        div xs:dayTimeDuration('PT0.001S')"/>
</xsl:function>

Then you could combine these to write your dates as milliseconds, like this:

<xsl:value-of select="foo:toMilliseconds(foo:parseDateTime(lastPayEndDate))"/>
于 2013-03-11T21:16:21.173 回答