XML and XPath - Basics

Thursday 12th November 2026

First Session: Learning Objectives

XML Syntax and Model

Nodes

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.

Documents, Elements, Attributes, and Text

In this basic session we need only be concerned with:

We won't explore Namespaces

An Example XML Document


                <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>
            

XML is a Tree!

Looking at an XML Document

By "tree" we mean a hierarchical structure where nodes are arranged according to the relationships to their parent, sibling, and child nodes.

XML Data Model: Trees of Nodes (Not Text)


          <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

Trees

XML Tree Sample

Processing order of Trees

XML Tree Processing Order

“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.

Basic XPath Axes

Path Expressions

An XPath Path Expression:

Child Axis


                    <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

                                <taxonomy>
                                    <taxonomy-term>Criminal Procedure</taxonomy-term>
                                    <taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
                                </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?

Descendant (or Self) Axis


                    <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

                                <taxonomy-term>Criminal Procedure</taxonomy-term>
                                <taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
                            
//text() Q: What is the result?
//element() Q: What is the result?

Parent Axis


                    <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

                                <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>
                            
//important[@value eq "true"]/parent::metadata Q: What is the result?
//taxonomy-term[. eq "Criminal Procedure"]/parent::element() Q: What is the result?

Basic XPath Predicates

Predicates

An XPath Predicate:

Predicate Examples


                    <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"]

                                <publish-date day="10" month="3" year="2011"/>
                            
/metadata/taxonomy-grp/taxonomy/taxonomy-term[2]

                                <taxonomy-term>Prosecution and Pre-trial Procedure</taxonomy-term>
                            
/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?

XPath Variables

Let Expressions

An XPath Variable:

Let Expression Examples


                    <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)
let $pi := 22 div 7
return
  $pi
Q: What is the result?
let $year-of-interest := "2011"
let $important-metadata := //metadata[important/@value = "true"]
return
  $important-metadata[publish-date/@year = $year-of-interest]
Q: What is the result?

Loops in XPath

For Expression

An XPath For Expression:

For Expression Examples


                    <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)
for $tm in //taxonomy-term
return
  $tm/text()

                                Criminal Procedure
                                Prosecution and Pre-trial Procedure
                            
for $x in ('a', 'b'), $y in (1, 2)
return
  ($x, $y)
Q: What is the result?
for $pd in //metadata[important/@value = "true"]/publish-date
return
  xs:date($pd/@year || "-0" || $pd/@month || "-" || $pd/@day)
Q: What is the result?

Simple Map Operator

The XPath Simple Map Operator:

Simple Map Operator Examples


                    <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)
//taxonomy-term ! text()

                                Criminal Procedure
                                Prosecution and Pre-trial Procedure
                            
('a', 'b') ! (., (1, 2) ! .)
Q: What is the result?
//metadata[important/@value = "true"]/publish-date !
        xs:date($@year || "-0" || @month || "-" || @day)
Q: What is the result?

Functions in XPath

Functions

Function Call Examples


                    <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)
//@*/fn:name(.)

                                xml:id
                                value
                                day
                                month
                                year
                            
/metdata ! fn:count(.//taxonomy-term)
Q: What is the result?
for $pd in //metadata[important/@value = "true"]/publish-date
return
  fn:format-date($pd, "[D1o] [MNn], [Y]")
Q: What is the result?

First Session: What did we cover?