如何在 XSLT 1.0 中执行代码 table 查找

How to do a code table lookup in XSLT 1.0

如何从 XSLT 1.0 版中的代码 table 进行查找? 我试着做类似这个例子的事情:https://www.xml.com/pub/a/2002/02/06/key-lookups.html,但我认为在那种情况下数据在输入文件中而不是在 XSLT 本身中。

我创建了一个名为 "lookup" 的命名空间并尝试了以下操作,其中我在 XSLT 代码本身中查找 table,但我总是得到空值。我需要将其移至应用模板结构吗?

<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' />

下面的代码用于使用 Microsoft BizTalk 转换为 XML 的 EDI 文件。

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

基于此, I got it working. Also referenced post正确传递参数。

因为我用的是微软,所以我不得不把exsl改成msxsl。 我放弃了使用钥匙的想法。

<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)" />

我同时使用文字和 XPATH 值进行测试,直到我知道我的 XPATH 是正确的:

       <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>

模板用作查找 subroutine/function:

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

在 XML 输出中得到了我想要的结果(将 04 的代码转换为 "Update"):

ChangeStatus>Update</ChangeStatus>