6/3/2016 8:44 PM
XML4U Notes
1 of 35
6/3/2016 8:44 PM
XML4U Notes
2) Background
Well-formed XML
HTML
XSLT Processing Model
2 of 35
6/3/2016 8:44 PM
XML4U Notes
3) XPath
XPath locates items within an XML file
It relies on the XML tree structure to locate elements
XPath is used within XSLT elements to identify what to
bring from the input XML file to the result tree
An XPath looks like a path through an XML tree
3 of 35
6/3/2016 8:44 PM
XML4U Notes
4) Nodes and Node Sets
A given XPath may identify one node in the input tree:
a node
Or it may identify several nodes in the input tree: a
node set
4 of 35
6/3/2016 8:44 PM
XML4U Notes
5) Using XPaths
XPath is used in almost all XSLT elements. For this
topic, we will use two.
<xsl:value-of select="XPATH"/>
value-of takes the XPATH, chooses the first node
in the node set if there is more than one, and then puts
all of the content nodes within that node into the result
tree as one flat content node. A nested element is
looked within for content. Attributes within elements
are not included.
Example:
<xsl:value-of select="/members/member/name"/>
5 of 35
XML4U Notes
6) Using XPaths (part 2)
<xsl:copy-of select="XPATH"/>
copy-of takes the XPATH, and copies the entire
node set to the result tree, including all nested nodes.
It includes attributes.
If you aren't sure if you have identified multiple nodes,
use both value-of and copy-of to see if they both
find a single node or not.
Example:
<xsl:copy-of select="/members/member/name"/>
6/3/2016 8:44 PM
6 of 35
6/3/2016 8:44 PM
XML4U Notes
7) XPath
Absolute path starts at the root node: "/"
Relative path starts relative to the current context
node. xsl:template and xsl:for-each set the
current context node (see later videos)
7 of 35
6/3/2016 8:44 PM
XML4U Notes
8) Choosing an element
To reach the element first in members.xml, starting at
root:
/members/member/name/first
Use / to denote the root
Name the root element (members)
Use a / to go down a level
Add the next element under that closer to your
element (member)
Repeat those two steps until you reach the element
you want. (/name/first)
8 of 35
6/3/2016 8:44 PM
XML4U Notes
9) Picking a particular element
When there is more than one element at a given level,
you will search in that node set if you continue looking
within, unless you use an index to specify which one
you want.
/members/member[2]/name/first
This bypasses Bob and goes to Mary.
Indexes start at 1 for the first one.
9 of 35
6/3/2016 8:44 PM
XML4U Notes
10) Choosing an attribute
To reach the attribute amt in pudding.xml, starting at
root:
/recipe/ingredients/li/@amt
Build a path to reach the element the attribute is on
(/recipe/ingredients/li)
Use a / to look within that last element
Add the attribute with a @ prefix on it (@amt)
value-of shows the contents; copy-of expects to
place it on an element, and since duplicate attributes
are not permitted, picks the last one
10 of 35
6/3/2016 8:44 PM
XML4U Notes
11) XPath shorthands
If you have a current context (from a template or a
for-each), you don't need to start at root.
Shorthands relative to the current node:
. : current node
.. : parent node
: all immediate children
// : any depth, used in place of a / in an XPath
Example:
<xsl:value-of select="//name"/>
Matches all names, value-of gets the first one and
displays its contents, so Bob Smith is output.
11 of 35
6/3/2016 8:44 PM
XML4U Notes
12) XPath location functions
Some nodes do not have names; these can be
accessed using XPath functions
comment() : locates comment nodes
processing-instruction() : locates processing in
text() : locates content nodes, available on ele
Example:
<xsl:copy-of select="//text()"/>
outputs all of the content nodes under elements in the tree.
note members.xml and pudding.xml have no processing instruction or comments
12 of 35
6/3/2016 8:44 PM
XML4U Notes
13) XPath predicates
A predicate is a test that chooses from the current
matching nodes.
Add a predicate by putting it in square braces on an
XPath item, for example:
<xsl:value-of select="/members/member[member_dat
If this matches more than one, you still have a node
set, and if needed, XSL will choose the first one.
The current context within the predicate is the current
XPath up to that point.
If there is no test, then it tests for existence: does the
specified node exist.
13 of 35
6/3/2016 8:44 PM
XML4U Notes
14) XPath predicate tests
You can compare two values:
= != : Equals / Not Equals
> >= : Greater / Greater or Equal
< <= : Less / Less or Equal
if one side is a number, then the other is converted
to a number; otherwise string comparison is used
You can call a function that returns a boolean value
You can combine tests with and (both have to be true)
or or (only one has to be true).
14 of 35
XML4U Notes
15) XPath predicate test example
Look for names with m in the last name (try this in both
value-of and copy-of, on members.xml):
//name[contains(last,'m')]
6/3/2016 8:44 PM
15 of 35
6/3/2016 8:44 PM
XML4U Notes
16) XPath functions
provide information about the current node set
provide information about the current node
manipulate strings
manipulate numbers
manipulate booleans
16 of 35
6/3/2016 8:44 PM
XML4U Notes
17) XPath node set functions
last(), count() : the number of nodes in the
current node set
last(XPATH), count(XPATH) : the number of
nodes in the specified XPath node set
sum(XPATH) : converts all nodes identifed to
numbers and adds them up.
17 of 35
6/3/2016 8:44 PM
XML4U Notes
18) XPath node set function examples
Add up all members' balances:
<xsl:value-of select=
"sum(/members/member/member_data/balance)"/>
18 of 35
6/3/2016 8:44 PM
XML4U Notes
19) XPath node information functions
name() : the name of the current node, if it has one
node() : the current node, equivalent to "."
shorthand.
text() : the content of the current node, if it has any,
as text
position() : the position of the current node within
a node set, or 1 if it is not in a node set; position is
defined by order within the source XML file.
19 of 35
6/3/2016 8:44 PM
XML4U Notes
20) XPath node information function
examples
The position of the current node (1 if not in a node set)
<xsl:value-of select="position()"/>
The text in all the first name nodes
<xsl:copy-of select="/members/member/name/first/
20 of 35
6/3/2016 8:44 PM
XML4U Notes
21) XPath string functions
concat(STRING1,STRING2,...) : combine two
or more strings
contains(STRING1,STRING2) : true if
STRING2 is found within the value of STRING1.
number() : converts value to a number; string must
be digits, booleans are 0/1.
starts-with(STRING1,STRING2) : true if
STRING1 starts with STRING2
string() : converts the current node's value to a
string
string-length() : the length of the text in the
current node
21 of 35
6/3/2016 8:44 PM
XML4U Notes
22) XPath string functions, continued
substring(STRING,START,END) : extracts a
portion of the string.
substring-after/substringbefore(STRING,SUBSTRING) : extracts a
portion of STRING
text() : the text contents of the current node, if it
has any
translate() : a simple transformation of a string
based on a character map
String literals are characters enclosed in single or
double quotes, following XML rules for values within
quotes and using quotes (like attribute values)
Any parameter can be an XPATH returning a node; its
contents will be used as the value.
22 of 35
6/3/2016 8:44 PM
XML4U Notes
23) XPath string function examples
Name of first member with e in their last name:
<xsl:value-of select=
"/members/member/name[contains(last,'e')]"/>
Try this with copy-of to see all the matches.
23 of 35
6/3/2016 8:44 PM
XML4U Notes
24) XPath number functions
+,-,*,div,mod : inline, i.e. 3 + 7. mod is
"modulus", or remainder of division
floor(VALUE)/ceiling(VALUE)
/round(VALUE) : truncates or rounds value to
nearest integer
number(VALUE) : converts strings and booleans to
numbers
sum(XPATH) : adds up all the numeric values in the
node set
Number literals are, for example, 1 or 1.234
VALUE can be an XPATH that identifies a numeric
value or a number literal
24 of 35
6/3/2016 8:44 PM
XML4U Notes
25) XPath number function examples
Show the member balance with the annual
membership fee deducted
<xsl:value-of select=
"/members/member/member_data/balance - 5"/
Note, this doesn't change the xml file, it just displays
the modified value.
25 of 35
6/3/2016 8:44 PM
XML4U Notes
26) XPath boolean functions
and, or : inline to combine two boolean tests. 'and'
is true if both are true, 'or' is true if either is true.
boolean(VALUE) : converts values to booleans; 0
and empty strings are false.
contains(STRING1,STRING2) : true if
STRING1 contains STRING2
false() : boolean false value
not(VALUE) : reverses a boolean value
starts-with(STRING1,STRING2) : true if
STRING1 starts with STRING2
true() : boolean true value
Parameters can be XPaths or literals
If not booleans, they are implicitly converted
26 of 35
XML4U Notes
27) XPath boolean function examples
Find a member with a title and with a positive balance
<xsl:value-of select="/members/member/name[
(string-length(../member_data/title) > 0)
and (../member_data/balance > 0) ]"/>
Can also do this within the path like so:
<xsl:value-of select="/members/member
[member_data/title != ''
and member_data/balance > 0]/name"/>
6/3/2016 8:44 PM
27 of 35
6/3/2016 8:44 PM
XML4U Notes
28) Combine XPaths
If you want all nodes that match two different paths,
you can combine two paths results with "|"
Example, all dates of interest, when members joined
or left:
//join|//left
returns 4 dates in a copy-of, just the first date in a
value-of (try it).
28 of 35
6/3/2016 8:44 PM
XML4U Notes
29) Review: How to build an XPath
Identify your starting location ("/" or no value, for the
current context node)
State your next step with one of these:
an element name, matches all element nodes at
the current context with that name
@ followed by an attribute name, matches all
attributes on the current element node with that
attribute name (note: you must be on an element,
or this matches nothing)
invoke an XPath function, for example comment()
to identify comment nodes
If that step would identify more than one node, you can
use a index to denote which one you want, if you only
want one; this is done by putting the index number in
square brackets. If you don't index, the first one is
taken if one must be chosen.
If that step would identify more than one node, you can
use an XPath predicate to pick from among them; this
is done by putting a boolean test in square brackets,
like the index. If it's just an XPath, then it's an
existence test.
If you want to take another step through the tree, add
a "/" and return to the previous step (this will limit you
to a single node match at the previous item, if there
was more than one the first one is taken)
At any point, if there is no match found, then your node
set is empty/you have no node. You will just keep on
29 of 35
XML4U Notes
with an empty set from that point forward.
6/3/2016 8:44 PM
30 of 35
6/3/2016 8:44 PM
XML4U Notes
30) Troubleshooting
If you get errors, check your syntax:
matching square braces
matching quotes
correctly embedded quotes
31 of 35
6/3/2016 8:44 PM
XML4U Notes
31) Troubleshooting
If you get no results, check your path:
does it identify elements correctly (case matters!)
does it mark attributes correctly (@)
are attributes on the correct elements
is it in the correct context
32 of 35
6/3/2016 8:44 PM
XML4U Notes
32) Troubleshooting, continued
Build up your XPath one step at a time, checking
results with copy-of to ensure the right parts of the
XML input tree are identified
Test an XPath used in another XSL element with both
value-of and copy-of to see if it is identifying the
correct node or nodes.
33 of 35
6/3/2016 8:44 PM
XML4U Notes
33) Resources
XPath Tutorial @ W3schools
XPath 1.0 Tutorial @ Zvon.org
XPath Tester @ FreeFormatter.com
34 of 35
6/3/2016 8:44 PM
XML4U Notes
34) Your Turn
Using value-of, get the name of the member who
is President, using a comparison test to check for
President.
Using value-of, get the number of calories in the
lemon pudding.
Using copy-of, get all of the first names' contents.
This requires an XPath shortcut and a function.
Find all of the contents in pudding.xml that contain the
word "or". Did you find just the 3 words, more, or fewer
matches? Why?
35 of 35
© Copyright 2026 Paperzz