I apologize for the crude explanation, I'm fairly new to xml and xslt. Thanks for your patience.
Here's the challange, I'm trying to build a transformation to csv that creates a new line for each instance of an employee's benefit enrollments. So, if they're enrolled in two plans, a line gets created for each plan. The same is being done for thier dependents.
With my xml, how do I loop through different nodes and pick the right data?
While trying to make sure that I don't make lines for dependents that aren't enrolled in a benefit, I came across this error:
Description: XPTY0004: A sequence of more than one item is not allowed as the first argument of contains() ("System_ID", "Dependent_ID", ...)
I see that there is more than one ID type in a Dependent element, but I don't want to specifiy which one to use because I need to scan both of them.
Here's my xml:
<Report>
<Employee_ID>111111</Employee_ID>
<Last_Name>Allen</Last_Name>
<First_Name>Amy</First_Name>
<Benefit_Elections>
<Benefit_Type>Accident</Benefit_Type>
<Coverage>Employee + Family</Coverage>
<Enrolled_Worker Descriptor="Amy Allen (111111)">
<ID type="System_ID">aaaaaa</ID>
<ID type="Employee_ID">111111</ID>
</Enrolled_Worker>
<Benefit_Plan Descriptor="Accident">
<ID type="System_ID">121212</ID>
<ID type="Health_Care_ID">hcp01</ID>
</Benefit_Plan>
<Covered_Dependents Descriptor="Sally Allen">
<ID type="System_ID">bbbbbb</ID>
<ID type="Dependent_ID">22222</ID>
</Covered_Dependents>
<Covered_Dependents Descriptor="Bob Allen">
<ID type="System_ID">ffffff</ID>
<ID type="Dependent_ID">44444</ID>
</Covered_Dependents>
</Benefit_Elections>
<Benefit_Elections>
<Benefit_Type>Critical Illness</Benefit_Type>
<Coverage>$10,000</Coverage>
<Calculated_Coverage>$10,000</Calculated_Coverage>
<Enrolled_Worker Descriptor="Amy Allen (111111)">
<ID type="System_ID">aaaaaa</ID>
<ID type="Employee_ID">111111</ID>
</Enrolled_Worker>
<Benefit_Plan
Descriptor="Critical Illness (Child)">
<ID type="System_ID">ssssss</ID>
<ID type="Insurance_Coverage_Plan_ID">icpchild</ID>
</Benefit_Plan>
<Covered_Dependents Descriptor="Sally Allen">
<ID type="System_ID">bbbbbb</ID>
<ID type="Dependent_ID">22222</ID>
</Covered_Dependents>
</Benefit_Elections>
<Benefit_Elections>
<Benefit_Type>Critical Illness</Benefit_Type>
<Coverage>$15,000</Coverage>
<Calculated_Coverage>$15,000</Calculated_Coverage>
<Enrolled_Worker Descriptor="Amy Allen (111111)">
<ID type="System_ID">aaaaaa</ID>
<ID type="Employee_ID">111111</ID>
</Enrolled_Worker>
<Benefit_Plan
Descriptor="Critical Illness (Spouse)">
<ID type="System_ID">tttttt</ID>
<ID type="Insurance_Coverage_Plan_ID">icpspouse</ID>
</Benefit_Plan>
<Covered_Dependents Descriptor="Bob Allen">
<ID type="System_ID">ffffff</ID>
<ID type="Dependent_ID">44444</ID>
</Covered_Dependents>
</Benefit_Elections>
<Benefit_Elections>
<Benefit_Type>Critical Illness</Benefit_Type>
<Coverage>$30,000</Coverage>
<Calculated_Coverage>$30,000</Calculated_Coverage>
<Enrolled_Worker Descriptor="Amy Allen (111111)">
<ID type="System_ID">aaaaaa</ID>
<ID type="Employee_ID">111111</ID>
</Enrolled_Worker>
<Benefit_Plan
Descriptor="Critical Illness (Employee)">
<ID type="System_ID">uuuuuu</ID>
<ID type="Insurance_Coverage_Plan_ID">icpemployee</ID>
</Benefit_Plan>
</Benefit_Elections>
<Dependents>
<First_Name>Sally</First_Name>
<Last_Name>Allen</Last_Name>
<Relationship Descriptor="Child">
<ID type="Related_Person_Relationship_ID">Child</ID>
</Relationship>
<Dependent_ID Descriptor="Sally Allen">
<ID type="System_ID">bbbbbb</ID>
<ID type="Dependent_ID">22222</ID>
</Dependent_ID>
</Dependents>
<Dependents>
<First_Name>Bob</First_Name>
<Last_Name>Allen</Last_Name>
<Relationship Descriptor="Spouse">
<ID type="Related_Person_Relationship_ID">Spouse</ID>
</Relationship>
<Uses_Tobacco>0</Uses_Tobacco>
<Dependent_ID Descriptor="Bob Allen">
<ID type="System_ID">ffffff</ID>
<ID type="Dependent_ID">44444</ID>
</Dependent_ID>
</Dependents>
</Report>
Here's my xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<xsl:for-each select="Report">
<Record>
<xsl:call-template name="EmployeeData"/>
<xsl:for-each select="Dependents">
<xsl:call-template name="Print_Dependents">
<xsl:with-param name="BenefitsExtractEmployeeNode" select=".."/>
</xsl:call-template>
</xsl:for-each>
</Record>
</xsl:for-each>
</xsl:template>
<xsl:template name="EmployeeData">
<First_Name><xsl:value-of select="First_Name"/></First_Name>
<Last_Name><xsl:value-of select="Last_Name"/></Last_Name>
<Benefit_Type><xsl:value-of select="Benefit_Elections/Benefit_Type"/></Benefit_Type>
<Coverage><xsl:value-of select="Benefit_Elections/Coverage"/></Coverage>
<Calculated_Coverage><xsl:value-of select="Benefit_Elections/Calculated_Coverage"/></Calculated_Coverage>
</xsl:template>
<xsl:template name="Print_Dependents">
<xsl:param name="BenefitsExtractEmployeeNode"/>
<xsl:if test="contains($BenefitsExtractEmployeeNode/Benefit_Elections/Covered_Dependents[ID/@type ='Dependent_ID']/ID/@type,
Dependents/Dependent_ID[ID/@type='Dependent_ID']/ID/@type)">
<Dependent>
<First_Name><xsl:value-of select="Dependents/First_Name"/></First_Name>
<Last_Name><xsl:value-of select="Dependents/Last_Name"/></Last_Name>
<Benefit_Type><xsl:value-of select="$BenefitsExtractEmployeeNode/Benefit_Elections/Benefit_Type"/></Benefit_Type>
<Coverage><xsl:value-of select="$BenefitsExtractEmployeeNode/Benefit_Elections/Coverage"/></Coverage>
<Calculated_Coverage><xsl:value-of select="$BenefitsExtractEmployeeNode/Benefit_Elections/Calculated_Coverage"/></Calculated_Coverage>
</Dependent>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
It's hard to tell what you're trying to do exactly (you mention transforming to CSV, but your XSLT is outputting XML) without an example of your desired output.
However, you should be able to move the contains() to the ID element in your xsl:if test...
<xsl:if test="$BenefitsExtractEmployeeNode/Benefit_Elections/Covered_Dependents[
ID/@type ='Dependent_ID']/ID[contains(@type,
Dependents/Dependent_ID[ID/@type='Dependent_ID']/ID/@type)]">
...
</xsl:if>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With