XML interview question · Most asked

Explain XPath axes and give examples of predicates.

Create a free account to save this page, take notes and track your progress. Sign in

Summary

An axis sets the direction of a step: child (default), attribute (@), descendant (//), parent (..), ancestor, following-sibling, preceding-sibling, following, preceding, self. Predicates filter: //product[price > 10], //item[@sku='A1'], //product[last()], //p[contains(name, 'tea')].

Longer Answer

Watch for //product[1] (first product of each parent) vs (//product)[1] (first in the document), and node-set comparisons that are true if any node matches.

Key Points

  • Axes set direction.
  • Predicates filter.
  • Positions start at 1.
  • (//x)[1] vs //x[1].

Deep dive: XML Tutorial: XPath Axes and Functions

Explanation

What interviewers look for

Correct axes and the position pitfall.

More most-asked XML questions

← All XML questions

Was this page helpful?