首页 > 解决方案 > 如何从 xslt 中的多个字段中查找和格式化最新日期

问题描述

我已经搜索了有关我的特定场景的信息,但没有找到我要查找的内容。如果我错过了什么,请提前道歉。

我正在寻找有关从三个字段中查找和格式化最新日期的语法帮助。我能够草率地找到最新日期并能够格式化日期,但是将语法放在一起并让它正常工作让我望而却步。我想向这个论坛询问一个干净的代码来执行以下操作:使用 xslt 2.0 从这三个字段中查找、返回和格式化最新日期 - 格式为“20070724”:-ws:Pay_Rate_Type_Change_Effective_Date -ws:Time_Type_Change_Effective_Date -ws:Job_Code_Change_Effective_Date

非常感谢任何帮助!

谢谢,珍

<?xml version="1.0" encoding="UTF-8"?>
<ws:Worker_Sync xmlns:ws="urn:com.workday/workersync"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<ws:Worker>
    <ws:Summary>
        <ws:Employee_ID>123456</ws:Employee_ID>
    </ws:Summary>
    <ws:Additional_Information>
        <ws:Time_Type_Change_Effective_Date>2005-05-24</ws:Time_Type_Change_Effective_Date>
        <ws:Pay_Rate_Type_Change_Effective_Date>2006-06-24</ws:Pay_Rate_Type_Change_Effective_Date>
        <ws:Job_Code_Change_Effective_Date>2007-07-24</ws:Job_Code_Change_Effective_Date>
    </ws:Additional_Information>
</ws:Worker>    
</ws:Worker_Sync>


<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ws="urn:com.workday/workersync" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xtt="urn:com.workday/xtt" xmlns:etv="urn:com.workday/etv"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wd="urn:com.workday.report">
<!-- set encoding of output - need to create a properly formatted XML document that Workday's internal document transformation engine will convert to a text file -->
<xsl:output method="xml" encoding="UTF-8"/>

<xsl:template match="/">
    <File xtt:separator="&#xd;&#xa;">
        <xsl:variable name="linefeed" select="'&#xa;'"/>
        <xsl:variable name="currentDate">
            <xsl:value-of select="format-date(current-date(), '[Y0001][M01][D01]')"/>
        </xsl:variable>
        <xsl:apply-templates select="ws:Worker_Sync/ws:Worker"/>
    </File>
</xsl:template>
<xsl:template match="/ws:Worker_Sync/ws:Worker">
    <Record xtt:separator="|">
        <EmployeeAssignedID>
            <xsl:value-of select="ws:Summary/ws:Employee_ID"/>
        </EmployeeAssignedID>            
        <RecordChangeEffectiveDate/>         <!--FT/PT, Hrly/Slry, Jobcode change effdt-->
        <!--Here is where I have to add code to say:
        Find, return and format the latest date out of these three fields in xml file - format of '20070724' :
        -ws:Pay_Rate_Type_Change_Effective_Date
        -ws:Time_Type_Change_Effective_Date
        -ws:Job_Code_Change_Effective_Date    -->   
    </Record>
</xsl:template>
</xsl:stylesheet>

标签: xslt-2.0

解决方案


在 Martin、同事和我自己的反复试验的帮助下,我找到了问题的完整答案。这是我最终使用的,它工作得很好。

<RecordChangeEffectiveDate>            
 <xsl:value-of select="format-date(max((xs:date(ws:Additional_Information/ws:Pay_Rate_Type_Change_Effective_Date), xs:date(ws:Additional_Information/ws:Time_Type_Change_Effective_Date), xs:date(ws:Additional_Information/ws:Job_Code_Change_Effective_Date) )), '[Y0001][M01][D01]') "/>
</RecordChangeEffectiveDate>

推荐阅读