How to use for loop in XSLT and get node values based on the iteration

倖福魔咒の 提交于 2020-02-04 05:57:41

问题


How do we use for loop in XSLT?

I have this requirement where I want to convert the below shown xml into a comma separated file. Number of rows in CSV file would be equal to count of COBRA_Records_within_Range nodes for an employee's report entry. All values in 3 rows will be same except the child element values for COBRA_Records_within_Range nodes. I am able to create 3 rows but not able to retrieve the value for child elements of COBRA_Records_within_Range. I want to run the for loop on a count of a particular node and then retrieve its child elements based on the iteration. In the example below, there are 3 COBRA_Records_within_Range nodes. So loop should run for count(COBRA_Records_within_Range)`and then in every iteration i need the value from its child nodes. For examples, if it's 2nd iteration, then Eligibility_Reason should appear as 'Dependent Children - Loss of dependent child status under the plan rules' in the CSV output.

Could someone please help me with this?

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <wd:Report_Entry xmlns:wd="urn:com.workday/bsvc">
        <wd:Employee_ID>111111</wd:Employee_ID>
        <wd:Worker>John Smith</wd:Worker>
        <wd:Employee_Last_Name>Smith</wd:Employee_Last_Name>
        <wd:Employee_First_Name>John</wd:Employee_First_Name>
        <wd:COBRA_Records_within_Range>
            <wd:Qualifying_Event_Date>2015-10-04</wd:Qualifying_Event_Date>
            <wd:Eligibility_Reason>Dependent Children - Loss of dependent child status under the
                plan rules</wd:Eligibility_Reason>
            <wd:Benefit_Plan1>Dental-US - Delta Dental PPO BREG</wd:Benefit_Plan1>
        </wd:COBRA_Records_within_Range>
        <wd:COBRA_Records_within_Range>
            <wd:Qualifying_Event_Date>2015-10-05</wd:Qualifying_Event_Date>
            <wd:Eligibility_Reason>Dependent Children - Loss of dependent child status under the
                plan rules</wd:Eligibility_Reason>
            <wd:Benefit_Plan1>Healthcare FSA - Tri-Ad FSA Residential US</wd:Benefit_Plan1>
        </wd:COBRA_Records_within_Range>
        <wd:COBRA_Records_within_Range>
            <wd:Qualifying_Event_Date>2015-10-05</wd:Qualifying_Event_Date>
            <wd:Eligibility_Reason>Spouse - Divorce or legal separation of the covered
                employee</wd:Eligibility_Reason>
            <wd:Test>0</wd:Test>
            <wd:Benefit_Plan1>Medical/Vision-US - Empire Blue Cross &amp; Blue Shield
                EPO</wd:Benefit_Plan1>
        </wd:COBRA_Records_within_Range>
    </wd:Report_Entry>
</root>

Here's the expected output -

111111, John Smith, Smith, John, 2015-10-04, Dependent Children - Loss of dependent child status under the plan rules, Dental-US - Delta Dental PPO BREG
111111, John Smith, Smith, John, 2015-10-05, Dependent Children - Loss of dependent child status under the plan rules, Healthcare FSA - Tri-Ad FSA Residential US
111111, John Smith, Smith, John, 2015-10-05, Spouse - Divorce or legal separation of the covered employee, Medical/Vision-US - Empire Blue Cross &amp; Blue Shield EPO

Here's the XSLT I have created -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:op="http://www.w3.org/2005/xpath-functions"
    xmlns:wd="urn:com.workday/bsvc"
    exclude-result-prefixes="xsd op wd"    
    version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">      
        <File>            
            <Header separator=",">
                <xsl:call-template name="printHeader"/>
            </Header>            

            <xsl:for-each select="root/wd:Report_Entry">
                <xsl:variable name="cobrarecordcount" as="xsd:integer" select="count(wd:COBRA_Records_within_Range)"/>
                <xsl:variable name="record" select="."/>

                <xsl:for-each select="1 to $cobrarecordcount"> 
                    <xsl:call-template name="printRecord">
                        <xsl:with-param name="record" select="$record"></xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>                                                         
            </xsl:for-each>             
        </File>         

    </xsl:template>

    <xsl:template name="printHeader">   
        <xsd:element><xsl:text>Employee ID</xsl:text></xsd:element>
        <xsd:element><xsl:text>Employee Last Name</xsl:text></xsd:element>
        <xsd:element><xsl:text>Employee First Name </xsl:text></xsd:element>        
        <xsd:element><xsl:text>Qualifying Event Date</xsl:text></xsd:element>
        <xsd:element><xsl:text>Qualifying Event Type</xsl:text></xsd:element>        
        <xsd:element><xsl:text>Benefit Plan 1</xsl:text></xsd:element>
    </xsl:template> 

    <xsl:template name="printRecord">
        <xsl:param name="record"/>

                <Line separator=","  quoteStyle="double" quoteWhenMatches=".*[a-zA-Z].*">
                    <!--Employee Id-->
                    <xsd:element>
                        <xsl:value-of select="$record/wd:Employee_ID"/>
                    </xsd:element>     
                    <!--Employee Last Name-->
                    <xsd:element>
                        <xsl:value-of select="$record/wd:Employee_Last_Name"/>
                    </xsd:element>      
                    <!--Employee First Name-->
                    <xsd:element>
                        <xsl:value-of select="$record/wd:Employee_First_Name"/>
                    </xsd:element>  
                    <!--Qualifying Event Date-->
                    <xsd:element>
                        <xsl:value-of select="($record/wd:COBRA_Records_within_Range[position()]/wd:Qualifying_Event_Date)"/>
                    </xsd:element> 
                    <!--Qualifying Event Type-->
                    <xsd:element>
                        <xsl:value-of select="($record/wd:COBRA_Records_within_Range[position()]/wd:Eligibility_Reason)"/>
                    </xsd:element> 
                    <!--Benefit-->
                    <xsd:element>
                        <xsl:value-of select="$record/wd:COBRA_Records_within_Range[position()]/wd:Benefit_Plan1"/>
                    </xsd:element> 
                </Line>
    </xsl:template>    
</xsl:stylesheet>

回答1:


I cannot find any rhyme or reason in your attempted XSLT. The expected result can be achieved very simply by:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wd="urn:com.workday/bsvc">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="wd:Report_Entry">
    <xsl:variable name="common">
        <xsl:value-of select="wd:Employee_ID" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="wd:Worker" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="wd:Employee_Last_Name" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="wd:Employee_First_Name" />
        <xsl:text>, </xsl:text>
    </xsl:variable> 
    <xsl:for-each select="wd:COBRA_Records_within_Range">
        <xsl:copy-of select="$common"/>
        <xsl:value-of select="wd:Qualifying_Event_Date" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="wd:Eligibility_Reason" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="wd:Benefit_Plan1" />
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>



回答2:


In the predicate for the wd:COBRA_Records_within_Range elements, you are specifying position(), but that is not giving you the current value from your sequence in the xsl:for-each. The position() is the position of the context node.

You want to filter only for those who's position() is equal to the current number from your xsl:for-each.

In order to do that, add another parameter to your template and pass the current value when you call it:

In the example below, I created a parameter called rangeIndex and used the shorthand predicate [$rangeIndex], but could have also used [position() = $rangeIndex]:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:op="http://www.w3.org/2005/xpath-functions"
    xmlns:wd="urn:com.workday/bsvc"
    exclude-result-prefixes="xsd op wd"    
    version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">      
        <File>            
            <Header separator=",">
                <xsl:call-template name="printHeader"/>
            </Header>            

            <xsl:for-each select="root/wd:Report_Entry">
                <xsl:variable name="cobrarecordcount" as="xsd:integer" select="count(wd:COBRA_Records_within_Range)"/>
                <xsl:variable name="record" select="."/>

                <xsl:for-each select="1 to $cobrarecordcount"> 
                    <xsl:call-template name="printRecord">
                        <xsl:with-param name="record" select="$record"></xsl:with-param>
                        <xsl:with-param name="rangeIndex" select="."/>
                    </xsl:call-template>
                </xsl:for-each>                                                         
            </xsl:for-each>             
        </File>         

    </xsl:template>

    <xsl:template name="printHeader">   
        <xsd:element><xsl:text>Employee ID</xsl:text></xsd:element>
        <xsd:element><xsl:text>Employee Last Name</xsl:text></xsd:element>
        <xsd:element><xsl:text>Employee First Name </xsl:text></xsd:element>        
        <xsd:element><xsl:text>Qualifying Event Date</xsl:text></xsd:element>
        <xsd:element><xsl:text>Qualifying Event Type</xsl:text></xsd:element>        
        <xsd:element><xsl:text>Benefit Plan 1</xsl:text></xsd:element>
    </xsl:template> 

    <xsl:template name="printRecord">
        <xsl:param name="record"/>
        <xsl:param name="rangeIndex"/>
        <Line separator=","  quoteStyle="double" quoteWhenMatches=".*[a-zA-Z].*">
            <!--Employee Id-->
            <xsd:element>
                <xsl:value-of select="$record/wd:Employee_ID"/>
            </xsd:element>     
            <!--Employee Last Name-->
            <xsd:element>
                <xsl:value-of select="$record/wd:Employee_Last_Name"/>
            </xsd:element>      
            <!--Employee First Name-->
            <xsd:element>
                <xsl:value-of select="$record/wd:Employee_First_Name"/>
            </xsd:element>  
            <!--Qualifying Event Date-->
            <xsd:element>
                <xsl:value-of select="($record/wd:COBRA_Records_within_Range[$rangeIndex]/wd:Qualifying_Event_Date)"/>
            </xsd:element> 
            <!--Qualifying Event Type-->
            <xsd:element>
                <xsl:value-of select="($record/wd:COBRA_Records_within_Range[$rangeIndex]/wd:Eligibility_Reason)"/>
            </xsd:element> 
            <!--Benefit-->
            <xsd:element>
                <xsl:value-of select="$record/wd:COBRA_Records_within_Range[$rangeIndex]/wd:Benefit_Plan1"/>
            </xsd:element> 
        </Line>
    </xsl:template>    
</xsl:stylesheet>



回答3:


Here is a much simpler and shorter solution that does not use <xsl:for-each> at all:

I. XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:wd="urn:com.workday/bsvc">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vHeader" 
      select="string-join(/*/*/*[not(self::wd:COBRA_Records_within_Range)], ', ')"/>

  <xsl:template match="wd:COBRA_Records_within_Range">
    <xsl:value-of select="string-join(($vHeader, *), ', '), '&#xA;'"/>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>

II. XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:wd="urn:com.workday/bsvc">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vHeader">
   <xsl:apply-templates 
        select="/*/*/*[not(self::wd:COBRA_Records_within_Range)]" mode="header"/>
 </xsl:variable>

  <xsl:template match="wd:COBRA_Records_within_Range">
    <xsl:value-of select="$vHeader"/><xsl:text>, </xsl:text>
    <xsl:apply-templates mode="header"/>
    <xsl:value-of select="'&#xA;'"/>
  </xsl:template>

  <xsl:template match="*[text()]" mode="header">
    <xsl:if test="not(position() = 1)">, </xsl:if>
    <xsl:value-of select="."/>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>


来源:https://stackoverflow.com/questions/34205648/how-to-use-for-loop-in-xslt-and-get-node-values-based-on-the-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!