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

标签 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 等于“raw1”时所需的输出是:

Raw One

为了进一步说明,当变量 value_to_lookup 等于“raw4”时所需的输出是:

Raw Four

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

再次感谢!

最佳答案

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

查看样式表中显示的 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>
    

-- 添加以回应评论中的以下说明 --

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.


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


The result is "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>

关于xml - XSL - 嵌入式查找表 - 变量的查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327614/

相关文章:

Python pandas 两个表匹配以查找最新日期

mongodb - Mongodb查找条件: not exist

spring - 如何从 Spring 中对 Websphere 应用服务器中定义的 MQ 连接工厂进行 jndi 查找

xml - XSL 根据属性对元素进行排序,特定元素除外

mysql - SQL多对多关系解决方案: intermediate table or xml query?

xml - xslt 使用命名空间遍历属性

c++ - XML 解析 : Checking for strings within string C++

java - 使用 XSL FO 文件使用 iText 布局 PDF

xml - 使用 XSLT 将多个可选的 XML 元素包装到一个新的包装器 XML 元素中

Android:res/strings.xml,包括另一个文件夹中的另一个字符串文件