How to do a code table lookup in XSLT 1.0

ε祈祈猫儿з 提交于 2019-12-24 19:19:07

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/59415629/how-to-do-a-code-table-lookup-in-xslt-1-0

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