如何在 XSLT 中使用 for 循环并根据迭代获取节点值 [英] How to use for loop in XSLT and get node values based on the iteration
问题描述
我们如何在 XSLT 中使用 for 循环?
How do we use for loop in XSLT?
我有这个要求,我想将下面显示的 xml 转换为逗号分隔的文件.CSV 文件中的行数等于员工报告条目的 COBRA_Records_within_Range
节点数.除了 COBRA_Records_within_Range
节点的子元素值之外,3 行中的所有值都将相同.我能够创建 3 行,但无法检索 COBRA_Records_within_Range
的子元素的值.我想对特定节点的计数运行 for 循环,然后根据迭代检索其子元素.在下面的示例中,有 3 个 COBRA_Records_within_Range 节点
.所以循环应该运行 count(COBRA_Records_within_Range)`然后在每次迭代中我需要来自其子节点的值.例如,如果是第 2 次迭代,则 Eligibility_Reason 应在 CSV 输出中显示为受抚养子女 - 根据计划规则失去受抚养子女身份".
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 & Blue Shield
EPO</wd:Benefit_Plan1>
</wd:COBRA_Records_within_Range>
</wd:Report_Entry>
</root>
这是预期的输出 -
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 & Blue Shield EPO
这是我创建的 XSLT -
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>
推荐答案
我在您尝试的 XSLT 中找不到任何押韵或原因.可以通过以下方式非常简单地实现预期结果:
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> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这篇关于如何在 XSLT 中使用 for 循环并根据迭代获取节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!