xml 理解 XSLT 中的 position() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28715359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
understanding position() function in XSLT
提问by user3629892
I have a similar problem as this guy:
我和这个人有类似的问题:
using position() function in xslt
But I dont need the numbering, I just want to understand the way it works:
但我不需要编号,我只想了解它的工作方式:
<?xml version="1.0" encoding="UTF-8"?>
<test>
<a>blah</a>
<a>blah</a>
<a>blah</a>
<a>blah</a>
</test>
for this input, the following stylesheet:
对于此输入,以下样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="select">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="a">
<xsl:value-of select="position()"/><br/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
outputs:
输出:
<html><body>
2<br>blah
4<br>blah
6<br>blah
8<br>blah
</body></html>
Why does it skip the uneven numbers?
为什么它跳过奇数?
采纳答案by michael.hor257k
Why does it skip the uneven numbers?
为什么它跳过奇数?
Because while you were at the /level, you said:
因为当你在那个/级别时,你说:
<xsl:apply-templates/>
which applies templates to all nodes children of the root node, and (due to the built-in template rules) to all of their descendants - including the text nodes that separate the <a>elements.
它将模板应用于根节点的所有子节点,以及(由于内置模板规则)它们的所有后代 -包括分隔<a>元素的文本节点。
You will get a different result with an input of:
输入以下内容,您将获得不同的结果:
<?xml version="1.0" encoding="UTF-8"?>
<test><a>blah</a><a>blah</a><a>blah</a><a>blah</a></test>
or if you add an instruction to your stylesheet to:
或者如果您在样式表中添加一条指令:
<xsl:strip-space elements="*"/>
or if you apply templates selectively, e.g.
或者如果您有选择地应用模板,例如
<xsl:apply-templates select="//a"/>
回答by Ian Roberts
The position()function gives you the position of the current node within the "current node list", which is whatever was select-ed by the nearest apply-templatesor for-each(XSLT 2.0 refers to "items" and "sequences" rather than nodes and node lists but the principle is the same).
该position()函数为您提供当前节点在“当前节点列表”中的位置,它是select由最近的apply-templates或for-each(XSLT 2.0 指的是“项目”和“序列”而不是节点和节点列表,但原理是相同)。
In your example the root template applies templates to its two child nodes (the text node containing the line break after the xml declaration, and the testelement). There's no explicit rule matching testso the default rule applies, which for elements means <xsl:apply-templates/>.
在您的示例中,根模板将模板应用于其两个子节点(包含 xml 声明后的换行符的文本节点和test元素)。没有明确的规则匹配,test因此默认规则适用,对于元素意味着<xsl:apply-templates/>.
Thus when the atemplate fires its "current node list" is the set of all 9 child nodes of test. This list consists of alternating text nodes (the line breaks) and element nodes (the aelements), with the text nodes at the odd positions and the element nodes at the even positions.
因此,当a模板触发时,它的“当前节点列表”是 的所有 9 个子节点的集合test。该列表由交替的文本节点(换行符)和元素节点(a元素)组成,文本节点位于奇数位置,元素节点位于偶数位置。
If you added an explicit template for testlike this:
如果您为test这样添加了显式模板:
<xsl:template match="test">
<xsl:apply-templates select="*"/>
</xsl:template>
Then this would select onlythe 4 aelement nodes as the current node list, so position()would give you 1, 2, 3 and 4.
然后这将仅选择4 个a元素节点作为当前节点列表,因此position()将为您提供 1、2、3 和 4。

