具有条件的XSLT sum()-返回“不允许将多个项目的序列作为第一个参数”

时间:2019-07-01 14:41:43

标签: xml xslt xpath

我有一组XML,如果wd:Group_Deduction_Code =“ SIDE FUND”,我试图对wd:Results_Line_Amount求和,并且此人也扣除了“可变组通用生命”。我可以总结wd:_Group_Deduction_Code很好,但是当我尝试添加任何遍历wd:All_Deductions的条件时,出现以下错误:

“不允许将一个以上项目的序列作为fn:substring()的第一个参数”

如何将wd:Results_Line_Amount与这些条件相加?

我尝试将for-each语句嵌套在变量声明中,但它的合计值不合适。

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/test">
<wd:Report_Entry>
    <wd:Deduction wd:Descriptor="Side Fund">
        <wd:ID wd:type="WID">9e7339f76b5c010ca628fca91701ea24</wd:ID>
        <wd:ID wd:type="Deduction_Code">SIDE</wd:ID>
    </wd:Deduction>
    <wd:Group_Deduction>SIDE FUND</wd:Group_Deduction>
    <wd:Result_Line_Amount>50</wd:Result_Line_Amount>
    <wd:Payroll_Result_Group>
        <wd:All_Deductions
            wd:Descriptor="Variable Group Universal Life: 9 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e01e7381928fc7209cc1e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="Side Fund: 50 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e0190b40928fc7209cb1e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="Spouse Critical Illness: 50 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e01fd9ad827fc7209ca1e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="Side Fund Spouse: 50 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e01ffc2c827fc7209c91e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="VGUL Spouse: 5 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e015d93b827fc7209c81e</wd:ID>
        </wd:All_Deductions>
    </wd:Payroll_Result_Group>
</wd:Report_Entry>
<wd:Report_Entry>
    <wd:Deduction wd:Descriptor="Side Fund Spouse">
        <wd:ID wd:type="WID">9e7339f76b5c012615df0baa1701f024</wd:ID>
        <wd:ID wd:type="Deduction_Code">SIDESP</wd:ID>
    </wd:Deduction>
    <wd:Group_Deduction>SIDE FUND</wd:Group_Deduction>
    <wd:Result_Line_Amount>50</wd:Result_Line_Amount>
    <wd:Payroll_Result_Group>
        <wd:All_Deductions
            wd:Descriptor="Variable Group Universal Life: 9 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e01e7381928fc7209cc1e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="Side Fund: 50 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e0190b40928fc7209cb1e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="Spouse Critical Illness: 50 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e01fd9ad827fc7209ca1e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="Side Fund Spouse: 50 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e01ffc2c827fc7209c91e</wd:ID>
        </wd:All_Deductions>
        <wd:All_Deductions
            wd:Descriptor="VGUL Spouse: 5 :05/04/2019 - 05/17/2019  (ABC Bi-weekly):John Doe: 05/17/2019 (Regular) - Complete">
            <wd:ID wd:type="WID">0edb826acc5e015d93b827fc7209c81e</wd:ID>
        </wd:All_Deductions>
    </wd:Payroll_Result_Group>
</wd:Report_Entry>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wd="urn:com.workday.report/test">
<xsl:output method="text"/>
<xsl:variable name="linefeed" select="'&#xd;&#xa;'"/>
<xsl:template match="/">
<xsl:variable name="sumSIDE" select="sum(wd:Report_Data/wd:Report_Entry[wd:Group_Deduction = 'SIDE FUND'][substring(wd:Payroll_Result_Group/wd:All_Deductions/@wd:Descriptor,1,29) = 'Variable Group Universal Life']/wd:Result_Line_Amount)"/>
</xsl:template>
</xsl:stylesheet>       

我期望SUM返回50,相反它返回一条错误消息“不允许将多个项的序列作为fn:substring()的第一个参数。我得到了错误。它没有像这样,有多个“ wd:_All_Deduction”标签。

1 个答案:

答案 0 :(得分:0)

您当前的状态是...

[substring(wd:Payroll_Result_Group/wd:All_Deductions/@wd:Descriptor,1,29) = 'Variable Group Universal Life']

但是正如您提到的,每个wd:Payroll_Result_Group/wd:All_Deductions元素有多个wd:Report_Entry元素。在XSLT 2.0中,当您将序列作为参数传递给字符串函数时,仅当序列由不超过1个条目组成时,该序列才有效。 (在这种情况下,它将使用单个条目的字符串值。)

您要的条件是这个。...

[wd:Payroll_Result_Group/wd:All_Deductions[substring(@wd:Descriptor,1,29) = 'Variable Group Universal Life']]

http://xsltfiddle.liberty-development.net/bFN1ya2此处查看其运行情况