'XSLT xsl:param looping - Return value if found within a text string
Objective: Evaluate each <Report_Entry> element. Return the <Internal_ID> value if the <External_ID> value is contained anywhere within the <Addenda> text.
My current XSLT returns the desired output but I am looking for a more dynamic/scalable solution. My XSLT requires that I create 2 xsl:param statements for each <Integration_Map_Value>:
<xsl:param name="INT-MAP_External_ID_1" select="//Integration_Map_Value[1]/External_ID"/>
<xsl:param name="INT-MAP_Internal_ID_1" select="//Integration_Map_Value[1]/Internal_ID"/>
plus an <xsl:when> statement:
<xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_1)">
<Internal_ID>
<xsl:value-of select="$INT-MAP_Internal_ID_1"/>
</Internal_ID>
</xsl:when>
Issue: Theoretically there could be an unlimited number of <Integration_Map_Value> instances so I am looking for a way of dynamically looping through all the <External_ID> values and comparing them against the <Addenda> field without having to create an endless amount of parameters, hardcoded instance numbers (i.e. [1]. [2], [3], etc.), and <xsl:when> statements.
Source XML:
<root>
<Integration_Map_Data>
<Integration_Map_Value>
<External_ID>MICROSOFT 1234</External_ID>
<Internal_ID>XYZ-001</Internal_ID>
</Integration_Map_Value>
<Integration_Map_Value>
<External_ID>MEGACORP</External_ID>
<Internal_ID>C10025</Internal_ID>
</Integration_Map_Value>
<Integration_Map_Value>
<External_ID>WIDGET TOOLS, INC ABC</External_ID>
<Internal_ID>C-000338</Internal_ID>
</Integration_Map_Value>
</Integration_Map_Data>
<Report_Data>
<Report_Entry>
<Addenda>abc microsoft 1234567</Addenda>
<OrderNo>4444</OrderNo>
</Report_Entry>
<Report_Entry>
<Addenda>fubar MEGACORP LLC</Addenda>
<OrderNo>5555</OrderNo>
</Report_Entry>
<Report_Entry>
<Addenda>no expected match here</Addenda>
<OrderNo>6666</OrderNo>
</Report_Entry>
</Report_Data>
</root>
Current XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<!-- External ID Parameters -->
<xsl:param name="INT-MAP_External_ID_1" select="//Integration_Map_Value[1]/External_ID"/>
<xsl:param name="INT-MAP_External_ID_2" select="//Integration_Map_Value[2]/External_ID"/>
<xsl:param name="INT-MAP_External_ID_3" select="//Integration_Map_Value[3]/External_ID"/>
<!-- Internal ID Parameters -->
<xsl:param name="INT-MAP_Internal_ID_1" select="//Integration_Map_Value[1]/Internal_ID"/>
<xsl:param name="INT-MAP_Internal_ID_2" select="//Integration_Map_Value[2]/Internal_ID"/>
<xsl:param name="INT-MAP_Internal_ID_3" select="//Integration_Map_Value[3]/Internal_ID"/>
<xsl:template match="/">
<root>
<xsl:for-each select="root/Report_Data/Report_Entry">
<record>
<xsl:copy-of select="Addenda"/>
<xsl:copy-of select="OrderNo"/>
<xsl:choose>
<xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_1)">
<Internal_ID>
<xsl:value-of select="$INT-MAP_Internal_ID_1"/>
</Internal_ID>
</xsl:when>
<xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_2)">
<Internal_ID>
<xsl:value-of select="$INT-MAP_Internal_ID_2"/>
</Internal_ID>
</xsl:when>
<xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_3)">
<Internal_ID>
<xsl:value-of select="$INT-MAP_Internal_ID_3"/>
</Internal_ID>
</xsl:when>
</xsl:choose>
</record>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
XML Output:
<?xml version="1.0" encoding="utf-8"?>
<root>
<record>
<Addenda>abc microsoft 1234567</Addenda>
<OrderNo>4444</OrderNo>
<Internal_ID>XYZ-001</Internal_ID>
</record>
<record>
<Addenda>fubar MEGACORP LLC</Addenda>
<OrderNo>5555</OrderNo>
<Internal_ID>C10025</Internal_ID>
</record>
<record>
<Addenda>no expected match here</Addenda>
<OrderNo>6666</OrderNo>
</record>
</root>
I’ve exhausted my beginner XSLT skills and can’t seem to figure out a more elegant solution to my problem. Any help is greatly appreciated. I am open to using XSLT 3.0 but that is very new to me.
Apologies for the title of this post... struggled to describe my issue.
Solution 1:[1]
Use a simple reference to the particular matching element e.g.
<xsl:template match="Report_Entry">
<record>
<xsl:apply-templates select="node(), //Integration_Map_Value[contains(upper-case(current()/Addenda), External_ID)]/Internal_ID"/>
</record>
</xsl:template>
plus the identity transformation template plus
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="Report_Data/Report_Entry"/>
</xsl:copy>
</xsl:template>
The identity transformation can be declared in XSLT 3 through <xsl:mode on-no-match="shallow-copy"/>, in earlier versions you have to spell it out as
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
