/phonebook/person[starts-with(phone-number, '323') and last-name = 'Lee']Imagine you are running this query on a hypothetical XML document that represents a whole phone book. The query retrieves all the persons from Hollywood (area code 323) with the last name Lee. How will the XPath engine execute this query? Let's see a few ways in which this could happen:
- It can go through the list of persons and start by checking the first condition first. If the first 3 digits of the phone number are 323 then it checks if the last name is "Lee".
- A more advanced engine might figure that the first test on the phone number is more expensive than the straight comparison with "Lee". So it might decide that it is more efficient to do the second comparison first, and only check the first 3 digits of the area code if it already knows that the last name is "Lee".
- An even more advanced engine might maintain an index of the persons based on their last name. Based on this index it can quickly locate the persons with last name "Lee". A standalone XPath engine typically wouldn't index XML documents, but this can be certainly expected from an engine running in a database.
- Instead of
/phonebook/personmight be more efficient: With
//personsome engines will traverse every element of the document, while they would only need to go through child elements of the root element with
- But more importantly
/phonebook/personstates more clearly your intension and makes your code more readable.
- In large XPath expressions, avoid duplicating part of the expression. For instance:
(count(/company/department[name = 'HR']/employee), avg(/company/department[name = 'HR']/employee/salary))This expression returns a sequence with the number of employees in the HR departments, and their average salary. Instead you can write it:
for $hr in /company/department[name = 'HR'] return (count($hr/employee), avg($hr/employee/salary))Unlike XQuery, XPath doesn't have a
letconstruct for you to declare variables. In some cases however, you can get around this by using the