XSL-嵌入式查找表-变量的查找值 [英] XSL - Embedded Lookup Table - Lookup Value of Variable

查看:84
本文介绍了XSL-嵌入式查找表-变量的查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StackExchange,我希望这里的人可以帮助我解决这个问题!

StackExchange, I'm hoping someone here can help me with this issue!

我正在使用 XSLT 1.0 ,试图嵌入查找表以将一些未格式化的数据转换为标准化的格式化结构.

I'm working in XSLT 1.0, trying to embed a lookup table to convert some unformatted data to a standardized, formatted structure.

我已经阅读,搜索并尝试了各种方法来实现此目的,但没有一个方法能够生成结果. (尽管我也没有任何错误.)

I have read and searched and tried various methods to accomplish this and none of them have been able to generate a result. (Though I'm not getting any errors, either.)

以下是我正在使用的XSL的示例:

Below is a sample of the XSL I'm working with:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:lookup="lookup" exclude-result-prefixes="lookup">

<xsl:key name="lookup_table" match="lookup:table/row" use="@raw"/>

<lookup:table>
    <row raw="raw1" corrected="Raw One"/>
    <row raw="raw2" corrected="Raw Two"/>
    <row raw="raw3" corrected="Raw Three"/>
    <row raw="raw4" corrected="Raw Four"/>
    <row raw="raw5" corrected="Raw Five"/>
</lookup:table>

<xsl:template match="/">

<xsl:variable name="lookup_table" select='document("")//lookup:table/row'/>
<xsl:variable name="value_to_lookup" select="'raw1'"/>

        <!-- In the actual XSL document, this variable would use an XPath to point to another attribute. -->
        <!-- In this case, the value of this variable must be changed manually. -->

<xsl:value-of select='document("")//lookup:table/row[@raw = $value_to_lookup]/@corrected'/>

<xsl:value-of select='document("")//lookup:table[@raw = $value_to_lookup]/@corrected'/>

<xsl:value-of select='$lookup_table[@raw = $value_to_lookup]/@corrected'/>

<xsl:value-of select="key('lookup_table',$value_to_lookup)/@corrected"/>

    <!-- The above lines are the various methods I've seen documented on other websites that claim these methods should allow me to what I need to. -->
    <!-- There is no need to have multiple identical results, I only have multiple attempts here to document the steps I have tried. -->

</xsl:template>
</xsl:stylesheet>

此代码的当前输出(从字面上看)为空.

The current output of this code is nothing (literally).

当变量value_to_lookup等于"raw1"时,所需的输出是:

The desired output when the variable value_to_lookup is equal to "raw1" is:

Raw One

为进一步说明,变量value_to_lookup等于"raw4"时的期望输出为:

For further clarification, the desired output when the variable value_to_lookup is equal to "raw4" is:

Raw Four

这部分代码的输出将存储在变量中,并在以后需要时调用.

The output of this bit of code will be stored in a variable and called later when needed.

再次感谢!

推荐答案

-根据问题的变化进行了编辑-

-- edited in response to changes in the question --

查看样式表中显示的4个选项:

Looking at the 4 options presented in your stylesheet:

  1. 这可以按预期工作:

  1. This works as expected:

<xsl:value-of select='document("")//lookup:table/row[@raw = $value_to_lookup]/@corrected'/>

  • 这是行不通的,因为rawrow的属性,而row 路径中不存在:

  • This cannot work, because raw is an attribute of row, and row is absent from the path:

    <xsl:value-of select='document("")//lookup:table[@raw = $value_to_lookup]/@corrected'/>
    

  • 这可以按预期工作:

  • This works as expected:

    <xsl:value-of select='$lookup_table[@raw = $value_to_lookup]/@corrected'/>
    

  • 这不起作用,因为在XSLT 1.0中,键仅在上下文中起作用 当前文档:

  • This doesn't work because in XSLT 1.0, keys only work in the context of current document:

    <xsl:value-of select="key('lookup_table',$value_to_lookup)/@corrected"/>
    

    要使其正常运行,您可以这样做:

    To make it work, you would do:

    <xsl:for-each select="document('')">
        <xsl:value-of select="key('lookup_table',$value_to_lookup)/@corrected"/>
    </xsl:for-each>
    


  • -为回应注释中的以下澄清而添加-


    -- added in response to the following clarifications in comments --

    XSL样式表是应用程序的一部分.当我想生成 新的结果文档或对现有文档进行更改,我去 通过基于Java的应用程序的菜单.我终于到了 在侧面带有小菜单和大文本输入窗口的屏幕上 在中间.该文本输入窗口是XSL编码所在的位置

    The XSL stylesheet is part of the application. When I want to generate a new resultant document or make changes to an existing one, I go through the menus of the Java-based application. I eventually arrive at a screen with a small menu on the side and large text-entry window in the middle. This text-entry window is where the XSL coding is typed.


    <xsl:value-of select="count(document(''))"/>的结果是什么?

    what do you get as the result of <xsl:value-of select="count(document(''))"/>?


    结果为"0".

    The result is "0".

    显然,您的处理环境不支持使用document()函数来引用样式表本身.这意味着您将需要使用另一种方法来执行内部查找-即,定义变量并将其转换为节点集-如MiMo的答案中已经建议的那样.

    Apparently, your processing environment does not support using the document() function to refer to the stylesheet itself. This means you will need to use another method to perform an internal lookup - namely, define a variable and convert it into a node-set - as was already suggested in the answer by MiMo.

    请注意,这与Java无关.几乎所有XSLT 1.0处理器都支持EXSLT node-set()扩展功能,但是某些Microsoft处理器仅在自己的名称空间中识别它.

    Note that this has nothing to do with Java. Practically all XSLT 1.0 processors support the EXSLT node-set() extension function, but some Microsoft processors recognize it only in their own namespace.

    为完成操作,这是使用键从变量中查找值的方法:

    For completion, here's how you would use a key to lookup a value from the variable:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:param name="value_to_lookup" select="'raw1'"/>
    
    <xsl:key name="lookup" match="row" use="@raw"/>
    
    <xsl:variable name="lookup">
        <row raw="raw1" corrected="Raw One"/>
        <row raw="raw2" corrected="Raw Two"/>
        <row raw="raw3" corrected="Raw Three"/>
        <row raw="raw4" corrected="Raw Four"/>
        <row raw="raw5" corrected="Raw Five"/>
    </xsl:variable>
    <xsl:variable name="lookup-set" select="exsl:node-set($lookup)" />
    
    <xsl:template match="/">
        <!-- change context to the lookup "document" -->
        <xsl:for-each select="$lookup-set">
            <xsl:value-of select="key('lookup', $value_to_lookup)/@corrected"/>
        </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>
    

    这篇关于XSL-嵌入式查找表-变量的查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆