问题描述
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:
-
这可以按预期工作:
This works as expected:
<xsl:value-of select='document("")//lookup:table/row[@raw = $value_to_lookup]/@corrected'/>
这是行不通的,因为raw
是row
的属性,而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 contextof 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 --
显然,您的处理环境不支持使用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-嵌入式查找表-变量的查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!