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:

<actors> 
    <actor id="00000015">Anderson, Jeff</actor> 
    <actor id="00000030">Bishop, Kevin</actor> 
    <actor id="0000000f">Bonet, Lisa</actor> 
    <actor id="00000024">Bowz, Eddie</actor> 
    <actor id="0000002d">Curry, Tim</actor> 
    <actor id="00000033">Dullea, Keir</actor> 
    <actor id="00000042">Fisher, Carrie</actor> 
    <actor id="00000006">Ford, Harrison</actor> 
    <actor id="00000045">Foster, Jodie</actor> 
...etc... 
</actors> 

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:

.//actors/actor[ends-with(., 'Lisa')]  

which gives us this output:

<actor id="0000000f">Bonet, Lisa</actor> 
<actor id="0000001b">Spoonhauer, Lisa</actor> 

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:

<video id="647599251"> 
   <studio></studio> 
   <director>Francesco Rosi</director> 
   <actorRef>916503211</actorRef> 
   <actorRef>916503212</actorRef> 
   <title>Carmen</title> 
   <dvd>18</dvd> 
   <laserdisk></laserdisk> 
   <laserdisk_stock></laserdisk_stock> 
   <genre>musical</genre> 
   <rating>PG</rating> 
   <runtime>125</runtime> 
   <user_rating>4</user_rating> 
   <summary>A fine screen adaptation of Bizet's popular opera.</summary> 
   <details>Placido Domingo does it again, this time in Bizet's popular opera.</details> 
   <vhs>15</vhs> 
   <beta_stock></beta_stock> 
   <year>1984</year> 
   <vhs_stock>88</vhs_stock> 
   <dvd_stock>22</dvd_stock> 
   <beta></beta> 
</video> 

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

//video[actorRef=//actors/actor[ends-with(., 'Lisa')]/@id]/title  

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

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

<title>Enemy of the State</title> 
<title>Clerks</title> 

Many people find that at this level of complexity, XPath syntax gets rather mind-boggling. In fact, this example just about stretches XPath to its limits. For this kind of query, and for anything more complicated, XQuery syntax comes into its own. But it is worth remembering that there are many simple things you can do with XPath alone, and that every valid XPath expression is also valid in XQuery.

XPath Query Editor

Stylus Studio provides a built-in XPath Query Editor in its XML Editor that allows you to visually edit and test complex XPath expressions, and it supports both version 1.0 and 2.0.

See Using the XPath Query Editor to learn more about using this tool.

 
Free Stylus Studio XML Training: