如何计算XSLT中具有各种条件的节点数

时间:2016-01-09 04:06:00

标签: xml xslt xslt-2.0

我有XML,基于此我创建了一个文本文件输出。在预告片记录中,我必须计算输出文件中的记录总数。用于从XML创建输出文件的逻辑是,如果每个报表条目的PreTaxAmtMatchAmtRothAmt的值大于零,则在输出中创建一行。如果每个报告条目的Loanamt大于零,则记录将在单独的行中输出。请建议我如何使用count函数创建XSLT中行的总计数。我正在低于我的尝试。

< Report_Entry>
    < Participant_ID>033709571</ Participant_ID>
    < Workers>
        < FULL_PART_Time_Indicator>1</ FULL_PART_Time_Indicator>
        < Last_Name>Vani</ Last_Name>
        < First_Name>Mehul</ First_Name>
        < Middle_Name>N</ Middle_Name>
        < Rehire_Date_1>2013-04-15-07:00</ Rehire_Date_1>
    </ Workers>
    < End_Date_from_Pay_Period>2016-01-03-08:00</ End_Date_from_Pay_Period>
    < PreTaxAmt>100.8</ PreTaxAmt>
    < MatchAmt>100.8</ MatchAmt>
    < RothAmt>0</ RothAmt>
    < LoanAmt>80</ LoanAmt>
</ Report_Entry>
< Report_Entry>
    < Participant_ID>037686040</ Participant_ID>
    < Workers>
        < FULL_PART_Time_Indicator>1</ FULL_PART_Time_Indicator>
        < Last_Name>Phonevilay</ Last_Name>
        < First_Name>Somsaath</ First_Name>
        < Gender_Code>1</ Gender_Code>
        < Rehire_Date_1>2012-06-18-07:00</ Rehire_Date_1>
    </ Workers>
    < End_Date_from_Pay_Period>2016-01-03-08:00</ End_Date_from_Pay_Period>
    < PreTaxAmt>61.81</ PreTaxAmt>
    < MatchAmt>61.81</ MatchAmt>
    < RothAmt>0</ RothAmt>
    < LoanAmt>76</ LoanAmt>
</ Report_Entry>
< Report_Entry>
    < Participant_ID>043781931</ Participant_ID>
    < Workers>
        < FULL_PART_Time_Indicator>1</ FULL_PART_Time_Indicator>
        < Last_Name>Ousouphan</ Last_Name>
        < First_Name>Phouangmala</ First_Name>
        < Gender_Code>2</ Gender_Code>
        < Rehire_Date_1>2001-03-20-08:00</ Rehire_Date_1>
    </ Workers>
    < End_Date_from_Pay_Period>2016-01-03-08:00</ End_Date_from_Pay_Period>
    < PreTaxAmt>84.19</ PreTaxAmt>
    < MatchAmt>0</ MatchAmt>
    < RothAmt>75</ RothAmt>
    < LoanAmt>90</ LoanAmt>
</ Report_Entry>

我使用的XSLT代码如下,它应输出6.

<xsl:if test="( PreTaxAmt != 0) or( MatchAmt != 0) or ( RothAmt != 0) ">
        <xsl:variable name="recordCount">
        <xsl:value-of select=" 
            count( Report_Data/ Report_Entry/ PreTaxAmt) 
            "/>
        </xsl:variable>
    </xsl:if>
            <xsl:if  test=" LoanAmt != 0"> 
                <xsl:variable name="recordCount1">
                    <xsl:value-of select=" 
                        count( Report_Data/ Report_Entry/ LoanAmt) 
                        "/>

                </xsl:variable>
                <xsl:variable name="var3" select="number($recordCount) + number($recordCount1)"/>
            </xsl:if>

1 个答案:

答案 0 :(得分:0)

假设您通过

修复文本对象为格式良好的XML
  • 从标记名称中删除所有空格
  • 添加Report_Data根元素

如下:

<Report_Data>
  <Report_Entry>
    <Participant_ID>033709571</Participant_ID>
    <Workers>
      <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
      <Last_Name>Vani</Last_Name>
      <First_Name>Mehul</First_Name>
      <Middle_Name>N</Middle_Name>
      <Rehire_Date_1>2013-04-15-07:00</Rehire_Date_1>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>100.8</PreTaxAmt>
    <MatchAmt>100.8</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>80</LoanAmt>
  </Report_Entry>
  <Report_Entry>
    <Participant_ID>037686040</Participant_ID>
    <Workers>
      <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
      <Last_Name>Phonevilay</Last_Name>
      <First_Name>Somsaath</First_Name>
      <Gender_Code>1</Gender_Code>
      <Rehire_Date_1>2012-06-18-07:00</Rehire_Date_1>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>61.81</PreTaxAmt>
    <MatchAmt>61.81</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>76</LoanAmt>
  </Report_Entry>
  <Report_Entry>
    <Participant_ID>043781931</Participant_ID>
    <Workers>
      <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
      <Last_Name>Ousouphan</Last_Name>
      <First_Name>Phouangmala</First_Name>
      <Gender_Code>2</Gender_Code>
      <Rehire_Date_1>2001-03-20-08:00</Rehire_Date_1>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>84.19</PreTaxAmt>
    <MatchAmt>0</MatchAmt>
    <RothAmt>75</RothAmt>
    <LoanAmt>90</LoanAmt>
  </Report_Entry>
</Report_Data>

然后是以下XSLT,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/Report_Data">
    <xsl:value-of select="count(Report_Entry[   number(PreTaxAmt) > 0
                                             or number(MatchAmt) > 0
                                             or number(RothAmt) > 0])
                          + count(Report_Entry[number(LoanAmt) > 0])"/>
  </xsl:template>

</xsl:stylesheet>

将按要求输出6