Cover Image for XPath Axes
129 views

XPath Axes

In XPath, axes are used to define the relationships between nodes in an XML or HTML document. Axes allow you to navigate the document’s structure in different directions to select nodes based on their relationships with other nodes. There are several axes available in XPath, each serving a specific purpose. The most commonly used axes are:

  1. child: Selects all child elements of the current node.
    Example: //book/author selects all <author> elements that are children of <book> elements.
  2. descendant: Selects all descendant elements (children, grandchildren, etc.) of the current node.
    Example: //book/descendant::title selects all <title> elements that are descendants of <book> elements.
  3. parent: Selects the parent element of the current node.
    Example: //title/parent::book selects the <book> element that is the parent of the <title> element.
  4. ancestor: Selects all ancestor elements (parent, grandparent, etc.) of the current node.
    Example: //title/ancestor::bookstore selects the <bookstore> element that is the ancestor of the <title> element.
  5. following-sibling: Selects all sibling elements that come after the current node.
    Example: //author/following-sibling::title selects all <title> elements that are siblings of <author> elements and come after them.
  6. preceding-sibling: Selects all sibling elements that come before the current node.
    Example: //author/preceding-sibling::title selects all <title> elements that are siblings of <author> elements and come before them.
  7. following: Selects all elements that come after the current node in the document order.
    Example: //title/following::author selects all <author> elements that come after <title> elements.
  8. preceding: Selects all elements that come before the current node in the document order.
    Example: //author/preceding::title selects all <title> elements that come before <author> elements.
  9. self: Selects the current node itself.
    Example: //title/self::title selects the <title> element itself.

XPath axes provide a powerful way to navigate and select nodes based on their relative positions in the document. By combining axes with element names, attributes, and other criteria, you can build complex XPath expressions to precisely retrieve the data you need from XML or HTML documents.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS