Namespace declarations : example of catalog (cat) and products (prod) :

<cat:catalog xmlns:cat=""
    <prod:name prod:language="en">Sun Hat red</prod:name>

Xqurery and Namespaces declarations in xquery file :

How to Declare a name space and xquery loop function (in your file.xquery)


 BaseX as an xquery tool :

BaseX is a very light-weight, high-performance and scalable XML Database engine and XPath/XQuery Processor with Full Text and Update extensions (Sample File XML)


Some useful Xpath script notations :

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 will all be familiar if you already know XPath.


Accessing XML Documents with XQuery with Stylus Studio one of the best editors :

XQuery allows you to access the file directly from either of these locations, using a suitable URL as an argument to its doc() function. Here’s an XQuery that simply retrieves and displays the whole document :


The file contains a number of sections. One of them is an <actors> element, which we can select like this:


This produces the result:

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

If you’re working in Stylus Studio┬«, click on XQuery / Scenario Properties, and under Main Input (optional), browse to the input file and select it. You can now refer to this document in your query simply as “.” (dot).


In the scrennshot above we can also write more complex XPath expressions like this one:

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

which gives the following output :

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

XQuery FLWOR Expressions

If you’ve used SQL, then you will have recognized the last example as a join between two tables, the videos table and the actors table. It’s not quite the same in XML, because the data is hierarchic rather than tabular, but XQuery allows you to write join queries in a similar way to the familiar SQL approach. Its equivalent of SQL’s SELECT expression is called the FLWOR expression, named after its five clauses: for, let, where, order by, return. Here’s the last example, rewritten this time as a FLWOR expression:

let $doc := .
for $v in $doc//video,
$a in $doc//actors/actor
where ends-with($a, ‘Lisa’)
and $v/actorRef = $a/@id
return $v/title

And of course, we get the same result

Prolog namespace declarations

declare default element namespace "";
declare namespace cat = "";
declare namespace prod = "";
<report> {
  for $product in doc("prod_ns.xml")/prod:product
  return <lineItem>
} </report>

Some related links :