XML and XPath - Basics
Thursday 12th November 2026
Thursday 12th November 2026
XML
Syntax
Trees
XPath
Axes
Sequences
Predicates
Variables
Loops
Functions
An XML document marks up some text.
To markup the text you introduce nodes into your document.
There are different types of nodes:
Elements and Attributes may be placed into a Namespace.
In this basic session we need only be concerned with:
We won't explore Namespaces
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
Q: How many Document Nodes are there?
Q: What are the names of the Element nodes?
Q: How many Attribute Nodes are there?
Q: How many Namespaces appear in the document?
An XML document is a sequence of characters:
By "tree" we mean a hierarchical structure where nodes are arranged according to the relationships to their parent, sibling, and child nodes.
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
/
└── metadata
├── @xml:id = XMLasTree
├── taxonomy-grp
│ └── taxonomy
│ ├── taxonomy-term
│ │ └── # Criminal Prosecution
│ └── taxonomy-term
│ └── # Prosecution and Pre-trial Procedure
├── important
│ └── @value = false
└── publish-date
├── @day = 10
├── @month = 3
└── @year = 2011
Documents are processed in 'Document Order' (by default):
Top-down, Left-to-right.
“Traversing” the tree in Document Order is the same as visually looking at the XML syntax in a file, and then scanning down
it from top-to-bottom.
XPath, XQuery, and XSLT all follow this order.
An XPath Path Expression:
Is similar to a file path or URL/URI, e.g.:
C:\Users\aretter\Pictures\me.jpg/home/aretter/Pictures/me.jpghttps://www.adamretter.org.uk/images/new-profile.jpg/ character) or relative/ character
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
/metadata/@xml:id |
@xml:id="XMLasTree" |
/metadata/taxonomy-grp/taxonomy |
|
/metadata/taxonomy-grp/taxonomy/taxonomy-term |
Q: What is the result? |
/metadata/taxonomy-grp/taxonomy/element()/text() |
Q: What is the result? |
/metadata/taxonomy-term |
Q: What is the result? |
/metadata/publish-date/@date |
Q: What is the result? |
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
//@xml:id |
@xml:id="XMLasTree" |
/metadata//taxonomy-term |
|
//text() |
Q: What is the result? |
//element() |
Q: What is the result? |
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
//publish-date/parent::metadata |
|
//important[@value eq "true"]/parent::metadata |
Q: What is the result? |
//taxonomy-term[. eq "Criminal Procedure"]/parent::element() |
Q: What is the result? |
An XPath Predicate:
Filters the addressed Items
Zero or more Predicates may be placed on any Path Step
A Predicate is written as an XPath expression within [ ] brackets
Two types of Predicates:
Boolean Predicates
Only the Items within the Sequence for which the expression evaluates to true are selected
Positional Predicates
Only the Items at the indicated integer position within the Sequence are selected
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
/metadata[@xml:id eq "XMLasTree"]/publish-date[@year eq "2011"] |
|
/metadata/taxonomy-grp/taxonomy/taxonomy-term[2] |
|
/metadata[important/@value = "true"]//taxonomy-term |
Q: What is the result? |
/metadata[important/@value]//taxonomy-term |
Q: What is the result? |
//taxonomy-term[ancestor::metadata[publish-date[@month = "3"][@year = "2011"]]] |
Q: What is the result? |
An XPath Variable:
Declares a name that is bound to a Sequence or Item
The name is prefixed with a $ character to indicate that it is a Variable
The name can be referenced later in place of the actual Sequence of Item
A Variable declaration and binding is written as an XPath Let expression:
let $favourite-animal := "Cat"
The variable with the name: favourite-animal
Is bound to the string: Cat
Variables are Immutable!
Variable names can be re-bound; can be confusing!
Can improve your code:
Can help document your intention
Code reuse (DRY principle!)
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
|
Q: What is the result? |
|
Q: What is the result? |
An XPath For Expression:
Is a very simple Loop
Iterates over Items in an input Sequence
The Item in the input Sequence is bound to a Variable
For each input Item, zero-or-more output Items can be returned
XPath For Expressions are extended in XQuery to FLWOR Expressions, which add:
Tumbling/Sliding Windows
Conditional filtering
Grouping
Counting
Ordering
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
|
|
|
Q: What is the result? |
|
Q: What is the result? |
The XPath Simple Map Operator:
Syntactic shortcut/sugar for a For Expression
Represented by the ! character
Executes the expression on the RHS once for each Item on the LHS of the !(Simple Map Operator)
The RHS expression may return zero-or-more output Items
The Item from the LHS is represented on the RHS as the Context Item
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
|
|
|
Q: What is the result? |
|
Q: What is the result? |
In XPath you can only call known functions
The XPath Standard Library provides hundered of functions: https://www.w3.org/TR/xpath-functions-31/
The EXPath Project provides many additional functions
Vendor processors/implementations also provide many bespoke functions
A function is identified by its QName (Qualified Name) and its Arity (number) of arguments
The syntax for calling a function is its QName(the function's parameters), e.g.:
fn:count( ('a', 'b', 'c') )XQuery extends XPath to allow you to write your own named functions!
<metadata xml:id="XMLasTree">
<taxonomy-grp>
<taxonomy>
<taxonomy-term>Criminal Procedure</taxonomy-term>
<taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
</taxonomy>
</taxonomy-grp>
<important value="false"/>
<publish-date day="10" month="3" year="2011"/>
</metadata>
| Path Expression | Result(s) |
|---|---|
|
|
|
Q: What is the result? |
|
Q: What is the result? |
XML Nodes
XML as Trees
XPath Basics
Axes
Sequences
Predicates
Variables
Loops
Function (Calls)