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 个选项:
这按预期工作:
<xsl:value-of select='document("")//lookup:table/row[@raw = $value_to_lookup]/@corrected'/>
这行不通,因为
raw
是row
的属性, 和row
路径中不存在:<xsl:value-of select='document("")//lookup:table[@raw = $value_to_lookup]/@corrected'/>
这按预期工作:
<xsl:value-of select='$lookup_table[@raw = $value_to_lookup]/@corrected'/>
这不起作用,因为在 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/