Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT 1.0: how to go for the "parent" axis

Tags:

xslt

xpath

parent

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.

like image 676
Peter Avatar asked Nov 03 '25 12:11

Peter


2 Answers

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

like image 65
Wayne Avatar answered Nov 06 '25 02:11

Wayne


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 ..

like image 37
Lucero Avatar answered Nov 06 '25 03:11

Lucero