XPaths Flashcards

1
Q

XPath

A

Allows us to return a set of values or nodes from an XML document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What can be returned from an XPath?

A
  • values, like strings or integers
  • nodes, like document nodes, element nodes and attributes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Basic XPath format

A

/E1/E2/E3/E4/…/En
Essentially, select all nodes reachable from the root by following the edges labelled as above.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

XPath example

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

XPath Root Line

A

Any XPath should start with the documents name, for example:
students/student
should be
doc(“mydoc.xml”)/students/student

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

XPath attribute

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Advanced XPaths

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

XPath Wildcard

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Axis

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Descendants axis

A

To represent a descendent, we can replace
/descendent::E
with
//E

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Descendent definition

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Preceding sibling(s)

A

Nodes with the same parent which come before the node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Following sibling(s)

A

Nodes with the same parent which come after the node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Conditions

A

We can also add conditions:
/axis1::E1[c1]
in which if the condition is true, then continue the path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Conditions Examples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

//*[category=”CS” or category=”Sc-fi”][1]

A

Returns the first element which fulfils the category conditions.

16
Q

//*[category=”CS” or category=”Sc-fi”][last()]

A

Presuming last() returns the last number, if will return the last book that fulfills these conditions.

17
Q

//book[category[1]=”CS”]/title

A

Returns the titles of the books whose first category is CS.

18
Q

//[.=”Maths”]/ancestor::[2]

A

Finds the grandparent of an element containing Maths.
. can be used to represent an element.
2 is essentially used to find the ancestors ancestor, aka traversing the tree up two steps.