XPath Exercise 3
To begin, download this XML file from our Pacific Voyage project that I have linked here:
ForsterGeorgComplete.xml. Open the file in
<oXygen/> and work with the XPath 3.1 Window. Respond to the XPath questions below in
a text file, and upload to Canvas for this
assignment when you’re finished. (Please use an attachment! If you paste your answer into
the text box, Canvas may munch your code syntax.) Some of these tasks are
thought-provoking, and even difficult. If you get stuck, do the best you can, and if you
can’t get a working answer, give the answers you tried and explain where they failed to get
the results you wanted. Sometimes doing that will help you figure out what’s wrong, and
even when it doesn’t, it will help us identify the difficult moments.
These tasks require the use of path expressions, predicates, and the functions count()
,
not()
, name()
, position()
, last()
, and distinct-values()
, but they should not require
any other XPath functions. There may be more than one possible answer. To read about
these functions, you should consult The XPath Functions We Use
Most page and if you have the Michael Kay text, it may be useful to you here.
As always, consult our class notes and our introductory guide Follow the XPath!.
Be sure to give the FULL
XPath expression you used in your answer, and don’t just report your results. This
way, if the answer is incorrect, we can help explain what went wrong.
** Notation:
For ease in recognition, from now on when we refer in discussion to an attribute name,
we’ll precede it with an at sign (@). In other words, when we
write about the @type attribute below, the name of the attribute
is actually type (without an at sign).
- Working with @type:
- Write an XPath using a function that returns a count of number of times we’ve
used @type attributes in the Georg Forster file.
- What’s the XPath to return the parent elements (whatever they are) of @type?
- Modify the XPath in your previous statement to return in the bottom results
window the names of those parent elements
- Modify the XPath expression once more to return a list of only the distinct-values of those parent elements.
- Working with attributes of ANY kind:
- Write an XPath expression to return all the attributes of any kind, anywhere in
this file.
- Using the name() function, build on your previous XPath
expression to return the names of these attributes.
- Now, return only the distinct-values of those attribute names: What XPath
expression does this?
- Now, what if we wanted to find all the parent elements (without knowing
what they are) of any attributes in use within the body element of the
file? Write the XPath expression.
- What’s the XPath to return the distinct-values of the
names of those parent elements in the body of the file.
- How many distinctly different element names are holding attributes of any kind?
(What expression returns this as a count?)
- Working with the
count()
, position()
, and last()
functions in predicates:
(These questions involve locating specific paragraphs by writing some complex predicates. The questions will ask you to find a special paragraph that contains a count()
of something
and set it equal to, greater than, or less than a particular number.
You will find examples of how to write this code in the Value Comparison section of our XPath tutorial.)
- Write an XPath expression that returns the last paragraph in the ENTIRE
Georg Forster file that contains more than one latitude record, coded as
<geo select="lat">. (Hint: This builds on
things we showed you in the XPath Exercise 2 homework. Note that you should only
get ONE result here!)
- Modify the expression so you return the first paragraph in the ENTIRE
Georg Forster file that contains more than two latitude records. (There’s
no such thing as a first function, but remember how we found the first, second,
and third books and chapters in past XPath exercises? The same working with
position numbers applies here.) Again, you should only get one result in your
results window.
- Now, how would we write XPath to find the very last paragraph in Book 2
that contains more than 1 latitude record? As before, you should only get
one result for this.
- Now, can you write an XPath expression that finds the very first paragraph
holding more than two latitude records, that also holds more than one
placeName element?
- How would you modify the previous expression to return the contents of the
placeName elements in that paragraph? What are the placeNames?
- To earn Extra Credit: Try out a new XPath function that we haven't yet assigned on this
file. Look up functions in The XPath Functions We Use
Most, the MDN list of XPath functions, or in the Michael Kay book if you have it. Explain what you tried, give
your XPath expression, and describe its results in your return window.