XSLT 1.0: how to go for the “parent” axis

时光总嘲笑我的痴心妄想 提交于 2019-12-07 16:31:25

问题


I have a question concerning the performance of an XSLT calling the "parent" axis in an XPATH. I can call the parent axis using "::*" or I call it using "::" and the name of the element

parent::*/MVKE/item/VMSTA='Z2'

or

parent::item/MVKE/item/VMSTA='Z2'

Does it matter performance wise if I use the "*" or if I use the name of the node element? Both work but I was wondering what the difference is. Can please someone explain that to me?

Thank you very much for your help and best regards, Peter


回答1:


The first expression matches any element parent. The second expression only matches when the parent is an item element. That's the only difference. I can't imagine any significant performance impact, since both node tests can be performed in constant time.

Note this line from the 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.

In practice this means that parent::* matches any parent except that of the root element.

To demonstrate, consider this simple example document:

<root>
    <one/>
    <item>
        <two/>
    </item>
</root>

Then:

  • //parent::* will get you the root and item elements (every parent node that is an element)

  • //parent::item will return only the item element (the only parent element that is an item)

  • //parent::node() will get you the parent of root (i.e. the root node) as well as the root and item elements




回答2:


It's not the same, but I doubt that there will be a significant performance difference.

Using the *accepts any parent element, using the name will require that the parent has this name or you'll get an empty node-set. Therefore, if one were faster it would likely be the * one.

Another option would be to use parent::node(), or its short form ..




回答3:


What about simply:

<xsl:value-of select="../node()"/>

Gives you the immediate parent. Are there,as was the original question performance benefits in one method V another?



来源:https://stackoverflow.com/questions/9006929/xslt-1-0-how-to-go-for-the-parent-axis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!