问题
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 theroot
anditem
elements (every parent node that is an element)//parent::item
will return only theitem
element (the only parent element that is anitem
)//parent::node()
will get you the parent ofroot
(i.e. the root node) as well as theroot
anditem
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