XSL - 嵌入式查找表 - 变量的查找值

时间:2016-02-10 22:55:09

标签: xml xslt lookup lookup-tables

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

我正在 XSLT 1.0 中工作,试图嵌入一个查找表,将一些未格式化的数据转换为标准化的格式化结构。

我已经阅读并搜索并尝试了各种方法来完成此任务,但没有一个方法能够生成结果。 (虽然我也没有收到任何错误。)

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

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

此代码的当前输出无关紧要(字面意思)。

变量value_to_lookup等于&#34; raw1&#34;时的所需输出是:

Raw One

为了进一步说明,当变量value_to_lookup等于&#34; raw4&#34;时,所需的输出是:

Raw Four

这段代码的输出将存储在一个变量中,并在需要时稍后调用。

再次感谢!

2 个答案:

答案 0 :(得分:2)

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

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

  1. 这符合预期:

    <xsl:value-of select='document("")//lookup:table/row[@raw = $value_to_lookup]/@corrected'/>
    
  2. 这不起作用,因为rawrow的属性,row 不在路上:

    <xsl:value-of select='document("")//lookup:table[@raw = $value_to_lookup]/@corrected'/>
    
  3. 这符合预期:

    <xsl:value-of select='$lookup_table[@raw = $value_to_lookup]/@corrected'/>
    
  4. 这不起作用,因为在XSLT 1.0中,键仅在上下文中起作用 当前文件:

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

    为了使其有效,你可以这样做:

    <xsl:for-each select="document('')">
        <xsl:value-of select="key('lookup_table',$value_to_lookup)/@corrected"/>
    </xsl:for-each>
    
  5. - 在回复评论中的以下说明时添加了

      

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

      

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

      

    结果为“0”。

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

    请注意,这与Java无关。实际上,所有XSLT 1.0处理器都支持EXSLT node-set()扩展功能,但是一些Microsoft处理器只在它们自己的命名空间中识别它。

    为了完成,以下是使用键从变量中查找值的方法:

    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>
    

答案 1 :(得分:0)

问题是<xsl:variable name="value_to_lookup" select='raw1'/>value_to_lookup设置为(不存在的)raw1元素的值 - 因此它是空的。

您想要的是<xsl:variable name="value_to_lookup" select="'raw1'"/>,然后是<xsl:value-of select='document("")//lookup:table/row[@raw = $value_to_lookup]/@corrected'/>。完成XSLT:

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

<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="value_to_lookup" select="'raw1'"/>
    <xsl:value-of select='document("")//lookup:table/row[@raw = $value_to_lookup]/@corrected'/>
</xsl:template>

您可以在不使用document()执行此类操作的情况下定义查找表:

<xsl:variable name="lookup_table_fragment">
    <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_table" select="msxsl:node-set($lookup_table_fragment)" />

然后:

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

msxsl:node-set是特定于Microsoft的,但所有处理器都具有将片段转换为节点集的功能)

适用于Java XSLT处理器的最新版本为:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
        xmlns:lookup="lookup" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="lookup exsl">

    <xsl:variable name="lookup_table_fragment">
        <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_table" select="exsl:node-set($lookup_table_fragment)" />

    <xsl:template match="/">
        <xsl:variable name="value_to_lookup" select="'raw1'"/>
        <xsl:value-of select='$lookup_table/row[@raw = $value_to_lookup]/@corrected'/>
   </xsl:template>

</xsl:stylesheet>

可以在http://www.utilities-online.info/xsltransformation

进行测试
相关问题