问题描述
我定义了一个 XSLT 密钥.我需要从 for-each 循环中访问键,该循环正在处理一个超出键定义范围的节点集.
I have an XSLT key defined. I need to access the key from within a for-each loop, where that loop is processing a node-set that is outside the scope of where the key was defined.
代码片段,我在其中标记了两行,一行有效,另一行无效:
Snippet, where I've marked two lines, one which works and one which does not:
<xsl:value-of select="key('name', 'use')"/> <!-- works -->
<xsl:for-each select="$outOfScopeNodeSet">
<xsl:value-of select="key('name', 'use')"/> <!-- does not work -->
</xsl:for-each>
有没有办法从 for-each 循环中访问密钥?
Is there a way to access the key from within the for-each loop?
XSLT 1.0,msxsl 引擎.
XSLT 1.0, msxsl engine.
(我想不出一个合理的方法来为此提供一个完整的工作示例.我也不确定正确的术语,例如范围"-也许如果我知道正确的术语,我就可以已经找到我的答案.如果问题不够清楚,请告诉我,我会尝试将其编辑成更好的形状.)
(I could not think of a reasonable way to provide a full working example for this. I'm also not sure of the correct terminology, such as "scope" - perhaps if I knew the correct terminology I'd be able to find my answer already. If the question is not clear enough please let me know and I'll try to edit it into better shape.)
推荐答案
在 XSLT 1.0 中,键不能跨文档工作.您的 $outOfScopeNodeSet
似乎包含一个节点集,其根节点与正在处理的 XML 文档的根节点不同(可能由 exsl:node-set()代码>函数?) - 而键应该从处理过的 XML 文档中获取一个值.
In XSLT 1.0, keys do not work across documents. It seems that your $outOfScopeNodeSet
contains a node-set whose root node is different from the root node of the XML document being processed (probably created by the exsl:node-set()
function?) - while the key is supposed to fetch a value from the processed XML document.
要解决这个问题,需要在调用key()
函数之前将上下文返回给处理后的XML文档,例如:
To resolve this problem, you need to return the context back to the processed XML document before calling the key()
function, for example:
<xsl:variable name="root" select="/" />
<xsl:for-each select="$outOfScopeNodeSet">
<xsl:variable name="use" select="some-value" />
<xsl:for-each select="$root">
<xsl:value-of select="key('name', $use)"/>
</xsl:for-each>
</xsl:for-each>
这篇关于XSLT“key"的范围(根节点、上下文)元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!