Search code examples
xslt-1.0lookup-tables

How to do a code table lookup in XSLT 1.0


How can I do a lookup from a code table in XSLT version 1.0? I tried to do something like this example: https://www.xml.com/pub/a/2002/02/06/key-lookups.html, but I think in that case the data was in the input file and not in the XSLT itself.

I created a namespace called "lookup" and tried the following, where my lookup table in the XSLT code itself, but I always get empty values. Do I need to move this to an apply-template structure?

<lookup:TenderActionType>
    <string id='00'>Add</string>
    <string id='01'>Cancel</string>
    <string id='04'>Update</string>
    <string id='05'>Update</string>
    <string id='56'>Cancel</string>
    <string id='06'>Add</string>
    <string id='46'>Cancel</string>
</lookup:TenderActionType>
<xsl:key name='tenderActionType' match='string' use='@id' />
<!-- I tried this as well --> 
<xsl:key name='tenderActionType2' match='lookup:TenderActionTypestring' use='@id' />

Code below is in a working for an EDI file converted to XML with Microsoft BizTalk.

      <ChangeStatus>
        <xsl:value-of select="key('tenderActionType', s0:B2A/B2A01/text())" />
      </ChangeStatus>
      <ChangeStatusTest>
        <xsl:value-of select="key('tenderActionType', '04')"/>            
      </ChangeStatusTest>

Solution

  • Based on this post, I got it working. Also referenced post to pass the parameter correctly.

    As I was using Microsoft, I had to change exsl to msxsl. I dropped the idea of using the key.

    <xsl:variable name="LookupTenderActionType">
        <string id='00'>Add</string>
        <string id='01'>Cancel</string>
        <string id='04'>Update</string>
        <string id='05'>Update</string>
        <string id='56'>Cancel</string>
        <string id='06'>Add</string>
        <string id='46'>Cancel</string>
    </xsl:variable>
    <xsl:variable name="lookupSet" select="msxsl:node-set($LookupTenderActionType)" />
    

    I was testing both with a literal and an XPATH value, until I knew my XPATH was correct:

           <Process> 
              <ChangeStatus>
                <xsl:call-template name="PerformLookupTenderActionType">
                   <xsl:with-param name="lookupNumericCode" select="s0:B2A/B2A01/text()"/>
                </xsl:call-template>              
              </ChangeStatus>
              <ChangeStatusTest>
                <xsl:call-template name="PerformLookupTenderActionType">
                   <xsl:with-param name="lookupNumericCode" select="04"/>
                </xsl:call-template>              
              </ChangeStatusTest>
    

    The template acts as the lookup subroutine/function:

    <xsl:template name="PerformLookupTenderActionType">
        <xsl:param name="lookupNumericCode"/>
        <xsl:value-of select="$lookupSet/string[@id = $lookupNumericCode]"/>
    </xsl:template>
    

    Got my desired result in XML output (converted code of 04 to "Update"):

    ChangeStatus>Update</ChangeStatus>