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

<cat:catalog xmlns:cat="http://datypic.com/cat"
             xmlns:prod="http://datypic.com/prod">
  <cat:number>1446</cat:number>
  <prod:product>
    <prod:number>565</prod:number>
    <prod:name prod:language="en">Sun Hat red</prod:name>
  </prod:product>
</cat:catalog>

Xqurery and Namespaces declarations in xquery file :

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

xquery-namespace

 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)

basex-xquery

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 :

doc(‘http://www.stylusstudio.com/examples/videos.xml’)

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

.//actors

This produces the 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>

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

accessing_xquery

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 "http://datypic.com/report";
declare namespace cat = "http://datypic.com/cat";
declare namespace prod = "http://datypic.com/prod";
<report> {
  for $product in doc("prod_ns.xml")/prod:product
  return <lineItem>
           {$product/prod:number}
           {$product/prod:name}
         </lineItem>
} </report>

Some related links :

http://www.xpathtester.com/xquery

http://www.datypic.com/books/xquery/examples.html