This example demonstrates use of the RANGE predicate. The first element always has a ordinal number of 1.

Find captions of figures that are referenced by <figref> elements in the chapter of "zoo.xml" with title "Frogs".

document("zoo.xml")/chapter[title = "Frogs"] //figref/@refid->fig/caption

XQuery has allowed for a de-reference operator(“->”). In HTML, IDREF and IDREFS values refer to values of other elements' ID attributes. An IDREF value is a single ID while an IDREFS value is a space-separated list of IDs. IDREF and IDREFS are case-sensitive. In this case, the deference operator follows the IDREF-type attribute, and returns elements referenced by the attribute. In the example above, the de-reference operator locates, in the “figref” element, in the “refid” attribute, the caption of the “fig” element.

List the names of the second-level managers of all employees whose rating is "Poor".

/emp[rating = "Poor"]/@mgr->emp/@mgr->emp/name

Here the query locates, where the employees having a “poor” rating, the name of the employees by looking up another emp element having a “mgr” attribute.

In the document "zoo.xml", find <tiger> elements in the namespace defined by www.abc.com/names that contain any supplement in the namespace defined by

www.xyz.com/names:

NAMESPACE abc = "www.abc.com/names" NAMESPACE xyz = "www.xyz.com/names" document("zoo.xml")//abc:tiger[xyz:*]

Here query provides syntax for URIs. A default namespace can also be specified as in this last example:

NAMESPACE DEFAULT = "www.abc.com/names" NAMESPACE xyz = "www.xyz.com/names" document("zoo.xml")//tiger[xyz:*]

Element constructors

The typical use of an element constructor is that it is nested inside another expression that binds one or more variables.

Generate an <emp> element containing an "empid" attribute and nested <name> and <job> elements. The values of the attribute and nested elements are specified by variables that are bound in other parts of the query:

<emp empid = $id> <name> $n </name> ,

42 The XML Files: Development of XML/XSL Applications Using WebSphere Studio

Page 58
Image 58
IBM Version 5 manual Element constructors