Welcome to The Coding College! This tutorial will guide you through the XPath Syntax, which is the foundation for writing queries to navigate and extract data from XML documents. XPath’s syntax is simple, yet powerful, allowing for precise selection of elements, attributes, and other nodes in an XML document.
What is XPath Syntax?
XPath uses a path-like notation to define and navigate nodes in an XML document. It allows for:
- Selecting specific elements or attributes.
- Filtering nodes based on conditions.
- Traversing parent, child, and sibling relationships.
Basic XPath Syntax
Here are some common patterns in XPath:
Symbol/Function | Meaning |
---|---|
/ | Selects from the root node. |
// | Selects nodes anywhere in the document. |
. | Refers to the current node. |
.. | Refers to the parent of the current node. |
@ | Selects attributes. |
* | Matches any element node. |
node() | Matches any type of node. |
text() | Selects text nodes. |
[] | Filters nodes based on conditions. |
` | ` |
() | Groups expressions. |
XPath Expressions
1. Absolute Path
An absolute path starts from the root (/
).
Example XML:
<library>
<book id="1">
<title>XML Basics</title>
<author>John Doe</author>
</book>
</library>
XPath Expression:
/library/book/title
Result:
Selects the <title>
element of the <book>
node:
<title>XML Basics</title>
2. Relative Path
A relative path starts from the current node (.
).
XPath Expression:
./title
Result:
Selects the <title>
element relative to the current node.
3. Select Nodes Anywhere (//
)
The //
operator selects nodes from anywhere in the document.
XPath Expression:
//author
Result:
Selects all <author>
elements:
<author>John Doe</author>
4. Select by Attribute
You can use the @
symbol to filter nodes by attributes.
XPath Expression:
//book[@id="1"]
Result:
Selects the <book>
element where id="1"
:
<book id="1">
<title>XML Basics</title>
<author>John Doe</author>
</book>
5. Select All Attributes
Use @*
to select all attributes of a node.
XPath Expression:
//book/@*
Result:
Selects all attributes of <book>
elements:
id="1"
6. Select Text Nodes
Use the text()
function to select the text content of an element.
XPath Expression:
//title/text()
Result:
Selects the text inside <title>
:
XML Basics
7. Wildcard (*
)
The *
operator matches any element.
XPath Expression:
/library/*
Result:
Selects all child elements of <library>
:
<book id="1">
<title>XML Basics</title>
<author>John Doe</author>
</book>
XPath Axes
1. Child Axis (child::
)
Selects child nodes of the current node (default behavior).
XPath Expression:
/library/child::book
Result:
Selects all <book>
elements under <library>
:
<book id="1">
...
</book>
2. Parent Axis (parent::
)
Selects the parent node of the current node.
XPath Expression:
//title/parent::book
Result:
Selects the parent <book>
node of <title>
:
<book id="1">
<title>XML Basics</title>
<author>John Doe</author>
</book>
3. Attribute Axis (@
)
Selects attributes of the current node.
XPath Expression:
/library/book/@id
Result:
Selects the id
attribute of the <book>
element:
1
XPath Predicates
1. Filter by Position
Use [n]
to select nodes based on their position.
XPath Expression:
/library/book[1]
Result:
Selects the first <book>
element:
<book id="1">
<title>XML Basics</title>
<author>John Doe</author>
</book>
2. Filter by Attribute Value
Filter nodes based on attribute values using [@attribute="value"]
.
XPath Expression:
//book[@id="1"]
Result:
Selects the <book>
element with id="1"
.
3. Filter by Text Content
Use contains()
to filter nodes based on their text content.
XPath Expression:
//title[contains(text(), "XML")]
Result:
Selects all <title>
elements containing the text “XML”:
<title>XML Basics</title>
4. Logical Operators
Combine conditions using and
, or
, and not()
.
XPath Expression:
//book[@id="1" and @id="2"]
Result:
Selects nodes that match both conditions (none in this case).
Combining Paths
1. Union Operator (|
)
Combine multiple paths to select nodes from different locations.
XPath Expression:
//title | //author
Result:
Selects all <title>
and <author>
elements:
<title>XML Basics</title>
<author>John Doe</author>
Practical Example
Querying Complex XML
XML Example:
<catalog>
<product id="1" category="Electronics">
<name>Smartphone</name>
<price>699</price>
</product>
<product id="2" category="Appliances">
<name>Blender</name>
<price>99</price>
</product>
</catalog>
XPath Expression to Find Electronics:
/catalog/product[@category="Electronics"]/name
Result:
<name>Smartphone</name>
Conclusion
XPath syntax is the foundation for navigating and querying XML documents. By understanding XPath expressions, predicates, and axes, you can efficiently extract and manipulate XML data.