XPath: efficiency in xpath expressions

T

Tjerk Wolterink

I'm wondering what is faster in a big document
something like:

//element

or this absolute path:

/parent/parent2/parent3/element

?

What is more efficient?
I came to this question when reading this:
http://dom4j.org/benchmarks/xpath/index.html
Especially this sentence:

The syntax of Xpath statements must be chosen carefully. Contrary to
some belief, and of the topology of our XML format, using /*/* or // was
most efficient compared to the absolute path /ItemResultSet/Item
 
R

Richard Tobin

Tjerk Wolterink said:
I'm wondering what is faster in a big document
something like:

//element

or this absolute path:

/parent/parent2/parent3/element

It depends on the implementation strategy, but in a straightforward
implementation, //element will have to examine every element in the
document, while /parent/parent2/parent3/element will ignore the
subtrees under, for example, /parent/other and /parent/parent2/other.

In the (fairly unusual) case where there aren't any such irrelevant
subtrees, the cost of checking the names of the parents will make
//element faster.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top