我有一个关于在 XPATH 中调用“父”轴的 XSLT 性能的问题。 我可以使用“::*”调用父轴,或者使用“::”和元素名称调用它
parent::*/MVKE/item/VMSTA='Z2'
或
parent::item/MVKE/item/VMSTA='Z2'
如果我使用“*”或如果我使用节点元素的名称,这对性能有影响吗?两者都有效,但我想知道有什么区别。
最佳答案
第一个表达式匹配任何 父元素。第二个表达式仅在父元素是 item
元素时才匹配。这是唯一的区别。我无法想象任何重大的性能影响,因为两个节点测试都可以在恒定时间内执行。
请注意 XPath 1.0 spec 中的这一行:
Every node other than the root node has exactly one parent, which is either an element node or the root node.
在实践中,这意味着 parent::*
匹配除根元素之外的任何父元素。
为了演示,考虑这个简单的示例文档:
<root>
<one/>
<item>
<two/>
</item>
</root>
然后:
//parent::*
将为您提供root
和item
元素(每个父节点都是一个元素)//parent::item
将只返回item
元素(唯一的父元素是item
)//parent::node()
将为您提供root
的父节点(即根节点)以及root
和item
元素
关于XSLT 1.0 : how to go for the "parent" axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9006929/