Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
202
Path Expressions
Path Expressions
This type of expression is used to locate nodes in a given tree or XML
document.
You can use either relative or absolute (rooted) path expressions. Relative
path expressions are used in context with the current node.
If you query a document, you should always use the document() function, which
takes the documents name as its parameter.
In this first example (which is by default shown when the application
is run), the output is simply the same as your input XML. The document()
function is used here to access the XML source as an XQuery navigator
collection, in which each navigator has its own name in this case, xqt.
We'll see more of this when we come to look at the code, later on in this
tutorial.
2. <root> { document("xqt")//title } </root>
which in this case is basically the same as
<root>
{ document("xqt")/root/tutorials/tutorial/title }
</root>
The output shown is:
<?xml version="1.0"?>
<root>
<title>Introduction to XQuery</title>
<title>XQuery's Syntax</title>
<title>XQuery: Advanced Tricks</title>
</root>
Here, the path expression selects every title that can be found in the
XML source. From this xquery, a screenshot is added after the next third
example.
3. <root>
{ document("xqt")//title/text() }
</root>
Here, the input xquery is neatly formatted, but that does not influence
the output in any way it is just for readability and easy writing.
The output shown is:
<?xml version="1.0"?>
<root>Introduction to XQueryXQuery's
SyntaxXQuery: Advanced Tricks</root>
In this case, the values of the four found titles are concatenated into
one string here (actually on one line) because the path expression
contains text(). Note that whitespace or other special characters (like
returns and linefeeds) located outside the element nodes are not outputted
by the XSL processor.
To show one more screenshot of XQueryTester in action, the output of the
second example is shown on the next screenshot:
Fig. 7: Screenshot of a working example with its output
You can see that the output is nicely colored and indented, just like IE
does. This cool look is due to Jason Patterson's stylesheet.