需要帮助将值从一个对等XML组放入另一个XML组

时间:2019-06-07 17:41:05

标签: xml xslt

我在下面有输入XML结构和目标输出。每个Payroll_Result_Lines_group将具有一个输出记录“ Worker”。 我的问题是在Column5上,它应该是“ Payroll_Result_group”下的YTD工资。它应基于wd:Payroll_Result_Lines_group / wd:Deduction / @ Deduction_Code值。

下面是我到目前为止拥有的XSL。我意识到,将变量deduction_code和ytd_wage放置在Report_Entry级别时,每个工人仅对其评估一次。导致 如果我将这些变量放在wd:Payroll_Result_Lines_group中,则ytd_wage不会返回任何内容,因为组“ Payroll_Result_group”不在“ Payroll_Result_Lines_group”下。有什么建议吗?

输入XML:

<wd:Report_Entry>
    <wd:Account_ID>123456</wd:Account_ID>
    <wd:Payroll_Result_Lines_group>
        <wd:Tax_Description>W_FWFEDERAL</wd:Tax_Description>
        <wd:Deduction wd:Descriptor="Federal Withholding [USA]">
            <wd:ID wd:type="Deduction_Code">W_FW</wd:ID>
        </wd:Deduction>
        <wd:YTD_Withheld>1111</wd:YTD_Withheld>
    </wd:Payroll_Result_Lines_group>
    <wd:Payroll_Result_Lines_group>
        <wd:Tax_Description>W_MEDFEDERAL</wd:Tax_Description>
        <wd:Deduction wd:Descriptor="Medicare [USA]">
            <wd:ID wd:type="Deduction_Code">W_MED</wd:ID>
        </wd:Deduction>
        <wd:YTD_Withheld>1112</wd:YTD_Withheld>
    </wd:Payroll_Result_Lines_group>
    <wd:Payroll_Result_Lines_group>
        <wd:Tax_Description>W_OASFEDERAL</wd:Tax_Description>
        <wd:Deduction wd:Descriptor="OASDI [USA]">
            <wd:ID wd:type="Deduction_Code">W_OAS</wd:ID>
        </wd:Deduction>
        <wd:YTD_Withheld>1113</wd:YTD_Withheld>
    </wd:Payroll_Result_Lines_group>
    <wd:Payroll_Result_group>
        <wd:YTD_Supplemental_Wage>66</wd:YTD_Supplemental_Wage>
        <wd:YTD_Federal_Wage>1000</wd:YTD_Federal_Wage>
        <wd:YTD_Medicare_Wage>2000</wd:YTD_Medicare_Wage>
        <wd:YTD_OASDI_Wage>3000</wd:YTD_OASDI_Wage>
        <wd:YTD_State_Wage>4000</wd:YTD_State_Wage>
    </wd:Payroll_Result_group>
</wd:Report_Entry>

输出:

<Workers>
    <Worker>
        <Column1 maxlen="11">808452</Column1>
        <Column2 maxlen="15">W_FWFEDERAL</Column2>
        <Column3 maxlen="13">1111</Column3>
        <Column4 maxlen="13">66</Column4>
        <Column5 maxlen="13"/>1000</ Column5>
    </Worker>
    <Worker>
        <Column1 maxlen="11">808452</Column1>
        <Column2 maxlen="15">W_MEDFEDERAL</Column2>
        <Column3 maxlen="13">1112</Column3>
        <Column4 maxlen="13">66</Column4>
        <Column5 maxlen="13"/>2000</ Column5>
    </Worker>
    <Worker>
        <Column1 maxlen="11">808452</Column1>
        <Column2 maxlen="15">W_OASFEDERAL</Column2>
        <Column3 maxlen="13">1113</Column3>
        <Column4 maxlen="13">66</Column4>
        <Column5 maxlen="13"/>3000</ Column5>
    </Worker>
</Workers>

XSL:

<xsl:template match="/wd:Report_Data">
        <xsl:for-each select="wd:Report_Entry">
            <xsl:variable name="account_id" select="wd:Account_ID"/>
            <xsl:variable name="ytd_supp_wage" select="wd:Payroll_Result_group/wd:YTD_Supplemental_Wage"/>
            <xsl:variable name="deduction_code" select="wd:Payroll_Result_Lines_group/wd:Deduction/wd:ID[2]"/> 
            <xsl:variable name="ytd_wage">
                <xsl:choose>
                    <xsl:when test="$deduction_code='W_FW'"><xsl:value-of select="wd:Payroll_Result_group/wd:YTD_Federal_Wage"/></xsl:when>
                    <xsl:when test="$deduction_code = 'W_MED'"><xsl:value-of select="wd:Payroll_Result_group/wd:YTD_Medicare_Wage"/></xsl:when>
                    <xsl:when test="$deduction_code = 'W_OAS'"><xsl:value-of select="wd:Payroll_Result_group/wd:YTD_OASDI_Wage"/></xsl:when>
                    <xsl:when test="$deduction_code = 'W_SWW'"><xsl:value-of select="wd:Payroll_Result_group/wd:YTD_State_Wage"/></xsl:when>
                </xsl:choose>                   
            </xsl:variable>
            <xsl:for-each select="wd:Payroll_Result_Lines_group">
                <Worker >
                    <Column1 xtt:maxLength="11"><xsl:value-of select="$account_id"/></Column1>
                    <Column2 xtt:maxLength="15"><xsl:value-of select="wd:Tax_Description"/></Column2>   
                    <Column3 xtt:maxLength="13"><xsl:value-of select="wd:YTD_Withheld"/></Column3>
                    <Column4 xtt:maxLength="13"><xsl:value-of select="$ytd_supp_wage"/></Column4>
                    <Column5 xtt:maxLength="13"><xsl:value-of select="$ytd_wage"/></Column5>                        
                </Worker>
            </xsl:for-each> 
        </xsl:for-each>
    </Workers>      
</xsl:template>

1 个答案:

答案 0 :(得分:2)

您需要将deduction_codeytd_wage变量的声明放入xsl:for-each的{​​{1}}内,因为它们实际上取决于您正在处理的工作程序。

尝试此XSLT

wd:Payroll_Result_Lines_group