需要帮助在XSLT中进行有条件的应用模板排序

问题描述 投票:0回答:1

我正在尝试获取符合条件并在wd:Completed_DateTime上按desc排序的第一行。可能有多个符合条件的行。例如,下面的XML中的wd:Instance_Set_Reference / wd:ID [@wd:type ='Customer_Category_ID'和text()='CRDRG']将返回两行,但我想要wd:Instance_Set_Reference / wd:ID [@ wd:type ='Customer_Category_ID'和text()='CRDRG']在wd:Completed_DateTime上由desc排序。

示例XML-

<?xml version='1.0' encoding='UTF-8'?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wd="urn:com.workday/bsvc">
    <env:Body>
        <wd:Get_Integration_Events_Response
            xmlns:wd="urn:com.workday/bsvc"
            wd:version="v34.0">
            <wd:Request_Criteria>
                <wd:Integration_System_Reference
                    wd:Descriptor="INTXXXX REV FINRA Connect Customer Invoice PDF Outbound Studio">
                    <wd:ID wd:type="WID">0ef644a34de5012532e30ad25003dcf1</wd:ID>
                    <wd:ID wd:type="Integration_System_ID">INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:ID>
                </wd:Integration_System_Reference>
                <wd:Integration_Event_Status_Reference wd:Descriptor="Completed">
                    <wd:ID wd:type="WID">d8b0bcd8446c11de98360015c5e6daf6</wd:ID>
                    <wd:ID wd:type="Background_Process_Instance_Status_ID">Completed</wd:ID>
                </wd:Integration_Event_Status_Reference>
                <wd:Integration_Event_Status_Reference wd:Descriptor="Completed with Warnings">
                    <wd:ID wd:type="WID">d8b0c34a446c11de98360015c5e6daf6</wd:ID>
                    <wd:ID wd:type="Background_Process_Instance_Status_ID">CompletedWithWarnings</wd:ID>
                </wd:Integration_Event_Status_Reference>
            </wd:Request_Criteria>
            <wd:Response_Filter>
                <wd:Page>1</wd:Page>
                <wd:Count>50</wd:Count>
            </wd:Response_Filter>
            <wd:Response_Results>
                <wd:Total_Results>2</wd:Total_Results>
                <wd:Total_Pages>1</wd:Total_Pages>
                <wd:Page_Results>2</wd:Page_Results>
                <wd:Page>1</wd:Page>
            </wd:Response_Results>
            <wd:Response_Data>
                <wd:Integration_Event>
                    <wd:Integration_Event_Data>                        
                        <wd:Completed_DateTime>2020-03-12T08:13:56.457-07:00</wd:Completed_DateTime>
                        <wd:Percent_Complete>1</wd:Percent_Complete>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="InvoicePrintStartDateTime (Required)">DateTime</wd:Parameter_Name>
                            <wd:Date>2019-03-12T08:13:15.000-07:00</wd:Date>
                        </wd:Integration_Runtime_Parameter_Data>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="InvoicePrintEndDateTime (Required)">DateTime</wd:Parameter_Name>
                            <wd:Date>2020-03-12T08:13:42.138-07:00</wd:Date>
                        </wd:Integration_Runtime_Parameter_Data>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="Revenue Stream">Customer Category of Billable Entity</wd:Parameter_Name>
                            <wd:Instance_Set_Reference
                                wd:Descriptor="(TRXXX)">
                                <wd:ID wd:type="WID">df63f2db7473018e586236fcdc00fa06</wd:ID>
                                <wd:ID wd:type="Customer_Category_ID">TRXXX</wd:ID>
                            </wd:Instance_Set_Reference>
                        </wd:Integration_Runtime_Parameter_Data>
                    </wd:Integration_Event_Data>                    
                </wd:Integration_Event>
                <wd:Integration_Event>                   
                    <wd:Integration_Event_Data>                        
                        <wd:Completed_DateTime>2019-11-20T11:27:33.111-08:00</wd:Completed_DateTime>
                        <wd:Percent_Complete>1</wd:Percent_Complete>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="InvoicePrintStartDateTime (Required)">DateTime</wd:Parameter_Name>
                            <wd:Date>2015-11-20T11:19:57.000-08:00</wd:Date>
                        </wd:Integration_Runtime_Parameter_Data>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="InvoicePrintEndDateTime (Required)">DateTime</wd:Parameter_Name>
                            <wd:Date>2019-11-20T11:27:22.179-08:00</wd:Date>
                        </wd:Integration_Runtime_Parameter_Data>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="Revenue Stream">Customer Category of Billable Entity</wd:Parameter_Name>
                            <wd:Instance_Set_Reference
                                wd:Descriptor="(CRXXX)">
                                <wd:ID wd:type="WID">df63f2db7473018e586236fcdc00fa06</wd:ID>
                                <wd:ID wd:type="Customer_Category_ID">CRXXX</wd:ID>
                            </wd:Instance_Set_Reference>
                        </wd:Integration_Runtime_Parameter_Data>
                    </wd:Integration_Event_Data>                    
                </wd:Integration_Event>
                <wd:Integration_Event>                    
                    <wd:Integration_Event_Data>                        
                        <wd:Completed_DateTime>2020-02-10T11:27:33.111-08:00</wd:Completed_DateTime>
                        <wd:Percent_Complete>1</wd:Percent_Complete>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="InvoicePrintStartDateTime (Required)">DateTime</wd:Parameter_Name>
                            <wd:Date>2015-11-20T11:19:57.000-08:00</wd:Date>
                        </wd:Integration_Runtime_Parameter_Data>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="InvoicePrintEndDateTime (Required)">DateTime</wd:Parameter_Name>
                            <wd:Date>2019-11-20T11:27:22.179-08:00</wd:Date>
                        </wd:Integration_Runtime_Parameter_Data>
                        <wd:Integration_Runtime_Parameter_Data>
                            <wd:Launch_Configurable_Name>INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio_Collection/INTXXXX_REV_FINRA_Connect_Customer_Invoice_PDF_Outbound_Studio/StartHere</wd:Launch_Configurable_Name>
                            <wd:Parameter_Name wd:Label="Revenue Stream">Customer Category of Billable Entity</wd:Parameter_Name>
                            <wd:Instance_Set_Reference
                                wd:Descriptor="(CRXXX)">
                                <wd:ID wd:type="WID">df63f2db7473018e586236fcdc00fa06</wd:ID>
                                <wd:ID wd:type="Customer_Category_ID">CRXXX</wd:ID>
                            </wd:Instance_Set_Reference>
                        </wd:Integration_Runtime_Parameter_Data>
                    </wd:Integration_Event_Data>                    
                </wd:Integration_Event>
            </wd:Response_Data>
        </wd:Get_Integration_Events_Response>
    </env:Body>
</env:Envelope>

样本XSLT-

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wd="urn:com.workday/bsvc" exclude-result-prefixes="xs env wd">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:for-each select="/env:Envelope/env:Body/wd:Get_Integration_Events_Response/wd:Response_Data/wd:Integration_Event/wd:Integration_Event_Data[wd:Integration_Runtime_Parameter_Data/wd:Instance_Set_Reference/wd:ID[@wd:type = 'Customer_Category_ID']/text()= 'CRXXX']">
            <xsl:sort select="wd:Completed_DateTime" order="descending"/>
            <xsl:if test="position()=1">
                <Revenue_Stream>
                    <xsl:value-of select="wd:Integration_Runtime_Parameter_Data/wd:Instance_Set_Reference/wd:ID[@wd:type = 'Customer_Category_ID']/text()"/>
                </Revenue_Stream>
                <Completion_date>
                    <xsl:value-of select="wd:Completed_DateTime"/>
                </Completion_date>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

预期输出-

<Root>
   <Revenue_Stream>CRXXX</Revenue_Stream>
    <Completion_date>2020-02-10T11:27:33.111-08:00</Completion_date>
</Root>

我将需要从具有很多行的XML中选择此值。因此,我不确定我使用的方法是否有效。一些指导对此将不胜感激。

xslt xslt-3.0
1个回答
1
投票

[另一种方法(以降序排序并获取第一项)将是使用xs:dateTime函数选择最大值max,然后选择具有该最大日期的(第一项):

<xsl:template match="/">
    <Root>
        <xsl:variable
          name="max"
          select="let $elements := /env:Envelope/env:Body/wd:Get_Integration_Events_Response/wd:Response_Data/wd:Integration_Event/wd:Integration_Event_Data[wd:Integration_Runtime_Parameter_Data/wd:Instance_Set_Reference/wd:ID[@wd:type = 'Customer_Category_ID'] = 'CRXXX'],
                  $max-dateTime := max($elements/wd:Completed_DateTime/xs:dateTime(.))
                  return head($elements[xs:dateTime(wd:Completed_DateTime) eq $max-dateTime])"/>
        <Revenue_Stream>
            <xsl:value-of select="$max/wd:Integration_Runtime_Parameter_Data/wd:Instance_Set_Reference/wd:ID[@wd:type = 'Customer_Category_ID']"/>
        </Revenue_Stream>
        <Completion_date>
            <xsl:value-of select="$max/wd:Completed_DateTime"/>
        </Completion_date>                
    </Root>
</xsl:template>

与其他任何与性能相关的问题一样,唯一的了解方法是在环境中使用处理器和样本数据进行测量。

© www.soinside.com 2019 - 2024. All rights reserved.