XQuery and XPath

We can access the actors section of videos.xml using this expression: .//actors. When we execute this XQuery we get the following result:

That was our first "real" query. If you are familiar with XPath, you might recognize that all the queries so far have been valid XPath expressions. We have used a couple of functions - current-time() and doc() - that might be unfamiliar because they are new in XPath 2.0, which is still only a draft; but the syntax of all the queries so far is plain XPath syntax. In fact, the XQuery language is designed so that every valid XPath expression is also a valid XQuery query.

This means we can write more complex XPath expressions like this one:

which gives us this output:

Different systems might display this output in different ways. Technically, the result of this query is a sequence of two element nodes in a tree representation of the source XML document, and there are many ways a system might choose to display such a sequence on the screen. Stylus Studio gives you the choice of a text view, like the one shown above, and a tree view: you use the buttons next to the Preview window to switch from one to the other. Here is what the tree view looks like:

This example used another function - ends-with() - that's new in XPath 2.0. We are calling it inside a predicate (the expression between the square brackets), which defines a condition that nodes must satisfy in order to be selected. This XPath expression has two parts: a path .//actors/actor that indicates which elements we are interested in, and a predicate [ends-with(., `Lisa')] that indicates a test that the nodes must satisfy. The predicate is evaluated once for each selected element; within the predicate, the expression "." (dot) refers to the node that the predicate is testing, that is, the selected actor.

The "/" in the path informally means "go down one level", while the "//" means "go down any number of levels". If the path starts with "./" or ".//" you can leave out the initial ".". (This assumes that the selection starts from the top of the tree, which is always the case in our examples.) You can also use constructs like "/.." to go up one level, and "/@id" to select an attribute. Again, this is all familiar if you already know XPath.

XPath is capable of doing some pretty powerful selections, and before we move on to XQuery proper, let us look at a more complex example. Suppose we want to find the titles of all the videos featuring an actor whose first name is Lisa. If we look at the videos.xml file, we see that each video in the file is represented by a video element like this one:

The query required to provide the results we desire is written like this:

Again, this is pure XPath (and therefore a valid XQuery). You can read it from left-to-right as:

l Start at the implicitly-selected document (videos.xml)
l Select all the <video> elements at any level
l Choose those that have an actorRef element whose value is equal to one of the values of the following:
n Select all the <actors> elements at any level
n Select all their <actor> child elements
n Select the element only if its value ends with `Lisa'
n Select the value of the id attribute
l Select the <title> child element of these selected <video> elements

When run against our source document, the result of this XQuery is:

 
Free Stylus Studio XML Training: