Re: [xsl] What is the most efficient XPath expression for retrieving the first child element?

Subject: Re: [xsl] What is the most efficient XPath expression for retrieving the first child element?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 12 May 2019 17:27:59 -0000
> (b) child::Total_Size

This is O(N) because all children will be examined -- and more than one
Total_Size elements may be selected.
So, this expression is wrong.

If we want to select the first child element named Total_Size, here is a
correct expression:

Total_Size[1]

The one that in theory should be more efficient than the above, is:

*[1]

Why is this more efficient? Because there is no need to get the element's
name and compare it to "Total_Size". We just select the first child
**regardless** of its name, and spend the few saved nanoseconds smiling :)

Note also that we can omit the child:: axis altogether, because it is the
"default axis" -- as per the W3C XPath 1.0 Spec:
https://www.w3.org/TR/1999/REC-xpath-19991116/#path-abbrev. So a few more
nanoseconds saved -- this time in reading and parsing.


Cheers,
Dimitre

On Sun, May 12, 2019 at 4:43 AM Costello, Roger L. costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hello XSLT experts!
>
> I want to retrieve the first child element. The children consist of a
> <Total_Size> element followed by many <String> elements (perhaps thousands
> of <String> elements).
>
> <COFF_String_Table>
>     <Total_Size>4443</Total_Size>
>     <String>.eh_frame</String>
>     <String>.debug_aranges</String>
>     <String>.debug_info</String>
>     ...
> </COFF_String_Table>
>
> Question: What is the most efficient XPath for selecting the first child
> element (the <Total_Size> element)? Assume the context node is the
> <COFF_String_Table> element. Which of these is the most efficient XPath
> expression:
>
> (a) child::*[1]
> (b) child::Total_Size
> (c) something else (what?)
>
> /Roger

Current Thread