XML XPath:

Key Features of XPath:

  • Enables the option of displaying a node or a set of nodes by navigating through to a particular path of the document.
  • Provides various functions and operators to manipulate and retrieve data.
  • Can be used with other XML technologies like XSLT and XQuery.

Example:

Given the following XML document:

                    
<bookstore>
  <book id="001">
    <title>XML Fundamentals</title>
    <author>John Smith</author>
    <price>29.99</price>
  </book>
  <book id="002">
    <title>Advanced XML</title>
    <author>Jane Doe</author>
    <price>39.99</price>
  </book>
</bookstore>

                    
                  

XPath can be used to validate some of this document and get information on books, authors, price, and many other things.

Basic XPath Syntax and Expressions:

XPath Expressions:

  • Absolute Path: Starts from the root element and follows the hierarchy
    • Example: /bookstore/book/title
  • Relative Path: Starts from the current context node.
    • Example: book/title
  • Axis: Defines a relationship to the current node.
    • Example: child::book
  • Predicates: Used to filter nodes based on conditions.
    • Example: /bookstore/book[price > 30.00]

Common XPath Syntax:

  • /: Root node or path separator.
  • //: Selects nodes in the document from the current node that match the selection no matter where they are.
  • .: Current node.
  • ..: Parent of the current node.
  • @: Selects attributes.

Examples:

  • /bookstore/book: Selects all <book> elements under the root <bookstore>.
  • //author: Selects all <author> elements in the document.
  • /bookstore/book[@id='001']: Selects the <book> element with an id attribute of '001'.

Using XPath to Select Nodes and Extract Data:

Selecting Nodes:

XPath expressions can be used to select specific nodes in an XML document.

Example:

To select the titles of all books

                    
/bookstore/book/title

                    
                  

This expression chooses all <title> elements that are descended from <book> elements but not vice-versa, which are contained in <bookstore>.


Extracting Data:

XPath can be used to extract data from selected nodes.

Example:

To get the price of the book with id='002'.

                    
                    /bookstore/book[@id='002']/price
                    
                  

This expression selects the <price> element of the <book> element with id='002'.


Combining Conditions::

XPath allows combining multiple conditions to refine the selection.

Example:

To select books with a price greater than 30:

                    
                    /bookstore/book[price > 30.00]
                    
                  

This expression finds all <book> elements such that the value of the <price> element in a selected node is greater than 30. 00.


Summary

  • XPath: An XML query, a language designed for retrieval of data stored in XML files and offering variability and versatility for selecting the data.
  • Basic XPath Syntax:
    • Absolute and Relative Paths: Head through the different levels of the document structure.
    • Axis: Specify dependencies on the current note.
    • Predicates: A service that allows end-users to filter nodes depending on conditions that are set.
  • Using XPath:
    • Selecting Nodes: Include expressions in references to give more precise identification of individual items and characters.
    • Extracting Data: The selected nodes’ data are to be retrieved with precision.

Because XPath is primarily used for querying and processing of XML documents, it is an inseparable utility in working with XML in various applications.