XPaths Flashcards
XPath
Allows us to return a set of values or nodes from an XML document.
What can be returned from an XPath?
- values, like strings or integers
- nodes, like document nodes, element nodes and attributes
Basic XPath format
/E1/E2/E3/E4/…/En
Essentially, select all nodes reachable from the root by following the edges labelled as above.
XPath example
If we had:
<students>
<student>
<name>Jane</name>
<number>75290</number>
</student>
</students>
We can do:
students/student
Resulting in all the student elements::
<student>
<name>Jane</name>
<number>75290</number>
</student>
XPath Root Line
Any XPath should start with the documents name, for example:
students/student
should be
doc(“mydoc.xml”)/students/student
XPath attribute
If we wanted to return the attribute values themselves, we would write:
/students/student/@name
If we were to write /students/student/name, it would return:
<name>"Sarah"</name>
But with the @ symbol we get:
“Sarah”
Depending on processing, we could also have to include /data() at the end.
Advanced XPaths
Normally comes with the use of an axis. So the format changes from
/E1/E2/E3/E4/…/En
to
/axis1::E1/axis2::E2/axis3::E3/axis4::E4
We end on an E.
XPath Wildcard
Represented with *.
Can be used to stand for any tag name or attribute name.
Using the attribute example:
/students/student/@name
This would result in:
/students/student/*
And both would return Sarah, SO LONG as there are no other attributes under student, as * would return them too.
Axis
Used to determine the next item on the path. For example, if we had:
axis1::E1
and axis1 is an attribute, then E1 is the name of an attribute.
We can remember that the shorthand for attribute is by using the @ symbol, or @name as an example.
Descendants axis
To represent a descendent, we can replace
/descendent::E
with
//E
Descendent definition
Any number of outgoing arrows away, but at least one.
A way to see it is:
x is the parent of y / y is the child of x
x is the ancestor of y / y is the descendent of x
Preceding sibling(s)
Nodes with the same parent which come before the node.
Following sibling(s)
Nodes with the same parent which come after the node.
Conditions
We can also add conditions:
/axis1::E1[c1]
in which if the condition is true, then continue the path.
Conditions Examples
//book[category=”CS”]/title
This will look at all books which contain the category CS in one of their nodes and return the title of that book.