Search code examples
xsltxpathparent

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


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.


Solution

  • 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