首页 > 解决方案 > if then else 或 when-otherwise 的 XSLT 语法

问题描述

xslt 非常新(仍然)。我认为我需要使用“When-Otherwise”来执行以下操作(如果我错了,请纠正我),因此为以下逻辑寻找一些关于正确语法的指导:

当 'Contribution_Amount' <=0 时,忽略该行,不输出。
否则返回“Contribution_Amount”。

或者如果这更容易/更好,我也可以使用相反的

当 'Contribution_Amount' > 0 时,则 'Contribution_Amount' 否则不产生输出

这是xsl。任何建议或帮助将不胜感激。提前致谢!

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:out="http://www.workday.com/integration/output" 
        xmlns:etv="urn:com.workday/etv" exclude-result-prefixes="xs" version="2.0"
        xmlns:xtt="urn:com.workday/xtt"
        xmlns:wd="urn:com.workday.report/Pay_Calculation_Results_-__Deduction_Register">    

 <xsl:template match="wd:Report_Data">  
      
        <File xtt:separator="&#xd;&#xa;">
            <Header xtt:separator="|">
                <Record_Type>FH</Record_Type>
                <Administractor_Code>FTB</Administractor_Code>
                <Employer_Code>555</Employer_Code>
                <Flag></Flag>
                <Submitted_Date><xsl:value-of select="format-date(current-date(),'[M01][D01][Y0001]')"/></Submitted_Date>
                <Submitted_Time><xsl:value-of select="format-time(current-time(),'[H01][m01][s01]')"/></Submitted_Time>
                <File_Version>2.0</File_Version>               
            </Header>
            <xsl:for-each-group select="wd:Report_Entry" group-by="wd:Worker/wd:SSN">
                <xsl:for-each-group select="current-group()" group-by="wd:Deduction_Description">
                <CT xtt:separator="|">
                    <Record_Type>CT</Record_Type>
                    <Participant_File_Import_Id><xsl:value-of select="wd:Worker/wd:SSN"/></Participant_File_Import_Id>
                    <Plan_Name>HSA</Plan_Name>
                    <contribution_date><xsl:value-of select="format-date(wd:Payment_Date,'[M01][D01][Y0001]')"/></contribution_date>
                    <Contribution_desc><xsl:value-of select="wd:Deduction_Description"/></Contribution_desc>
                    <contribution_Amount><xsl:value-of select="format-number(sum(current-group()//wd:Current_Period_Result),'#0.00')"/></contribution_Amount>
                    <amount_type>Actual</amount_type>
                </CT>
                </xsl:for-each-group>
            </xsl:for-each-group>
            <Footer xtt:separator="|">
                <xsl:variable name="Total_Count" select="count(wd:Report_Entry)"/>
                <Record_Type>FF</Record_Type>
                <Record_Count><xsl:value-of select="$Total_Count + 2"/></Record_Count>
                <Administrator_Total>FTB</Administrator_Total>
                <Employer_Code>555</Employer_Code>
                <Submitted_Date><xsl:value-of select="format-date(current-date(),'[M01][D01][Y0001]')"/></Submitted_Date>
                <Submitted_Time><xsl:value-of select="format-time(current-time(),'[H01][m01][s01]')"/></Submitted_Time>
            </Footer>
        </File>
    </xsl:template>
</xsl:stylesheet>

这是xml输入。我将其标记为 1.0,因为这是我们最初的开发人员创建的。我不够精通,无法真正了解其中的区别或何时切换,抱歉。

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/Pay_Calculation_Results_-__Deduction_Register">
    <wd:Report_Entry>
        <wd:Payroll_Result wd:Descriptor="Daffy Duck | Daffy Duck (12345): 03/08/2020 (Regular) - Complete">
            <wd:ID wd:type="WID">1</wd:ID>
        </wd:Payroll_Result>
        <wd:Worker wd:Descriptor="Daffy Duck | Daffy Duck (12345)">
            <wd:ID wd:type="WID">6</wd:ID>
            <wd:ID wd:type="Employee_ID">12345</wd:ID>
        </wd:Worker>
        <wd:Worker>
            <wd:SSN>123456789</wd:SSN>
        </wd:Worker>
        <wd:Employee_ID>12345</wd:Employee_ID>
        <wd:Company wd:Descriptor="ACME Corp">
            <wd:ID wd:type="WID">2</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">ACME</wd:ID>
            <wd:ID wd:type="Company_Reference_ID">ACME</wd:ID>
        </wd:Company>
        <wd:Organizations wd:Descriptor="Cost Center: Management Team 90">
            <wd:ID wd:type="WID">3</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">10090</wd:ID>
            <wd:ID wd:type="Cost_Center_Reference_ID">10090</wd:ID>
        </wd:Organizations>
        <wd:Organizations wd:Descriptor="Location: Location 1">
            <wd:ID wd:type="WID">4</wd:ID>
            <wd:ID wd:type="Location_ID">LOC01</wd:ID>
        </wd:Organizations>
        <wd:Period wd:Descriptor="02/24/2020 - 03/08/2020  (Bi-weekly (Mon-Sun))">
            <wd:ID wd:type="WID">5</wd:ID>
            <wd:ID wd:type="Period_ID">19</wd:ID>
        </wd:Period>
        <wd:Deduction>HSA Employer Contribution</wd:Deduction>
        <wd:Deduction_Description>Employer Contribution</wd:Deduction_Description>
        <wd:Payment_Date>2020-03-13-07:00</wd:Payment_Date>
        <wd:Deduction_Code>HSA ER CON</wd:Deduction_Code>
        <wd:Current_Period_Result>41.66</wd:Current_Period_Result>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Payroll_Result wd:Descriptor="Daffy Duck | Daffy Duck (12345): 03/08/2020 (Regular) - Complete">
            <wd:ID wd:type="WID">1</wd:ID>
        </wd:Payroll_Result>
        <wd:Worker wd:Descriptor="Daffy Duck | Daffy Duck (12345)">
            <wd:ID wd:type="WID">6</wd:ID>
            <wd:ID wd:type="Employee_ID">12345</wd:ID>
        </wd:Worker>
        <wd:Worker>
            <wd:SSN>123456789</wd:SSN>
        </wd:Worker>
        <wd:Employee_ID>12345</wd:Employee_ID>
        <wd:Company wd:Descriptor="ACME Corp">
            <wd:ID wd:type="WID">2</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">ACME</wd:ID>
            <wd:ID wd:type="Company_Reference_ID">ACME</wd:ID>
        </wd:Company>
        <wd:Organizations wd:Descriptor="Cost Center: Management Team 90">
            <wd:ID wd:type="WID">3</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">10090</wd:ID>
            <wd:ID wd:type="Cost_Center_Reference_ID">10090</wd:ID>
        </wd:Organizations>
        <wd:Organizations wd:Descriptor="Location: Location 1">
            <wd:ID wd:type="WID">4</wd:ID>
            <wd:ID wd:type="Location_ID">LOC01</wd:ID>
        </wd:Organizations>
        <wd:Period wd:Descriptor="02/24/2020 - 03/08/2020  (Bi-weekly (Mon-Sun))">
            <wd:ID wd:type="WID">5</wd:ID>
            <wd:ID wd:type="Period_ID">19</wd:ID>
        </wd:Period>
        <wd:Deduction>HSA - Contribution</wd:Deduction>
        <wd:Deduction_Description>Payroll Deduction</wd:Deduction_Description>
        <wd:Payment_Date>2020-03-13-07:00</wd:Payment_Date>
        <wd:Deduction_Code>HSA</wd:Deduction_Code>
        <wd:Current_Period_Result>254.17</wd:Current_Period_Result>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Payroll_Result wd:Descriptor="Daffy Duck | Daffy Duck (12345): 03/08/2020 (Regular) - Complete">
            <wd:ID wd:type="WID">1</wd:ID>
        </wd:Payroll_Result>
        <wd:Worker wd:Descriptor="Daffy Duck | Daffy Duck (12345)">
            <wd:ID wd:type="WID">6</wd:ID>
            <wd:ID wd:type="Employee_ID">12345</wd:ID>
        </wd:Worker>
        <wd:Worker>
            <wd:SSN>123456789</wd:SSN>
        </wd:Worker>
        <wd:Employee_ID>12345</wd:Employee_ID>
        <wd:Company wd:Descriptor="ACME Corp">
            <wd:ID wd:type="WID">2</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">ACME</wd:ID>
            <wd:ID wd:type="Company_Reference_ID">ACME</wd:ID>
        </wd:Company>
        <wd:Organizations wd:Descriptor="Cost Center: Management Team 90">
            <wd:ID wd:type="WID">3</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">10090</wd:ID>
            <wd:ID wd:type="Cost_Center_Reference_ID">10090</wd:ID>
        </wd:Organizations>
        <wd:Organizations wd:Descriptor="Location: Location 1">
            <wd:ID wd:type="WID">4</wd:ID>
            <wd:ID wd:type="Location_ID">LOC01</wd:ID>
        </wd:Organizations>
        <wd:Period wd:Descriptor="02/24/2020 - 03/08/2020  (Bi-weekly (Mon-Sun))">
            <wd:ID wd:type="WID">5</wd:ID>
            <wd:ID wd:type="Period_ID">19</wd:ID>
        </wd:Period>
        <wd:Deduction>HSA - Contribution</wd:Deduction>
        <wd:Deduction_Description>Payroll Deduction</wd:Deduction_Description>
        <wd:Payment_Date>2020-03-13-07:00</wd:Payment_Date>
        <wd:Deduction_Code>HSA</wd:Deduction_Code>
        <wd:Current_Period_Result>-250</wd:Current_Period_Result>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Payroll_Result wd:Descriptor="Daffy Duck | Daffy Duck (12345): 03/08/2020 (Regular) - Complete">
            <wd:ID wd:type="WID">1</wd:ID>
        </wd:Payroll_Result>
        <wd:Worker wd:Descriptor="Daffy Duck | Daffy Duck (12345)">
            <wd:ID wd:type="WID">6</wd:ID>
            <wd:ID wd:type="Employee_ID">12345</wd:ID>
        </wd:Worker>
        <wd:Worker>
            <wd:SSN>123456789</wd:SSN>
        </wd:Worker>
        <wd:Employee_ID>12345</wd:Employee_ID>
        <wd:Company wd:Descriptor="ACME Corp">
            <wd:ID wd:type="WID">2</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">ACME</wd:ID>
            <wd:ID wd:type="Company_Reference_ID">ACME</wd:ID>
        </wd:Company>
        <wd:Organizations wd:Descriptor="Cost Center: Management Team 90">
            <wd:ID wd:type="WID">3</wd:ID>
            <wd:ID wd:type="Organization_Reference_ID">10090</wd:ID>
            <wd:ID wd:type="Cost_Center_Reference_ID">10090</wd:ID>
        </wd:Organizations>
        <wd:Organizations wd:Descriptor="Location: Location 1">
            <wd:ID wd:type="WID">4</wd:ID>
            <wd:ID wd:type="Location_ID">LOC01</wd:ID>
        </wd:Organizations>
        <wd:Period wd:Descriptor="02/24/2020 - 03/08/2020  (Bi-weekly (Mon-Sun))">
            <wd:ID wd:type="WID">5</wd:ID>
            <wd:ID wd:type="Period_ID">19</wd:ID>
        </wd:Period>
        <wd:Deduction>HSA - Contribution</wd:Deduction>
        <wd:Deduction_Description>Payroll Deduction</wd:Deduction_Description>
        <wd:Payment_Date>2020-03-13-07:00</wd:Payment_Date>
        <wd:Deduction_Code>HSA</wd:Deduction_Code>
        <wd:Current_Period_Result>250</wd:Current_Period_Result>
    </wd:Report_Entry>
</wd:Report_Data>

标签: xsltxslt-1.0

解决方案


我会将其简化为:

<xsl:variable name="amt" select="sum(current-group()//wd:Current_Period_Result)" />
<xsl:if test="$amt">
    <contribution_Amount>
        <xsl:value-of select="format-number($amt, '0.00')"/>
    </contribution_Amount>
</xsl:if> 

(应该是注释,但是代码太多了)。


推荐阅读