Databases Reference
In-Depth Information
In this example, only a subset of all roles can add or update the XML files in the data
collection. A larger number of roles can view and search for topics using the queries
in the view and search collections. The settings in the overall application information
file (app-info.xml) associate each role with specific read and write permissions.
Native XML databases can store many different views of the same structure. Each
view is a different transformation of the underlying XML data. In the next two sec-
tions, you'll see how XPath and XQuery are used to create these views.
5.2.3
Applying simple queries to transform complex data with XPath
XPath, a language that works within XQuery, allows you to easily retrieve the data
you're looking for even in complex documents using short path expressions. Keeping
your path expressions short allows you to quickly locate and retrieve the specific infor-
mation within the document you're interested in, and not spend time writing long
queries or looking at extraneous data.
XPath expressions are similar to the path commands you type into a DOS or UNIX
shell to navigate to a specific directory or folder. XPath expressions consist of a series
of steps that tell the system how to navigate to a specific part of the document. For
example, the XPath expression $my-sales-order/order-item[3]/price will return
the price of the third item in a sales order. The forward slash indicates that the next
step in the hierarchy should be the child of the current location.
XML documents have a reputation for being complex, with good reason. When
you apply all the features of XML into a single document (for example, mixed con-
tent, namespaces, and encoded elements) you can create documents that are difficult
for humans to read. These documents are often complex because they try to accu-
rately capture the structure of the real world—which is sometimes complex. Yet using
complex structures doesn't imply that the queries must also be complex. This fact may
not be intuitive, but it's one of the most
important and sometimes overlooked
qualities of native XML databases and
XQuery. Let's review this concept using
a concrete example.
Each of the figures in this topic is
assigned a number. The first digit repre-
sents the chapter number (1, 2, 3), and
the number following the chapter is a
sequential number which represents
where the figure is located in the chap-
ter. To calculate the figure number, we
only need to count the number of prior
figures in each chapter. Figure 5.5 is an
example of an XPath expression that
does this.
A count
function
A sequence of all the prior <figure>
elements in this chapter
count($figure/preceding::figure) + 1
The current figure
in the document
The XPath
function to get
all prior items
in a document
Select only the
prior <figure>
elements
Figure 5.5 How a complex document is queried
using a simple XPath query expression. The
expression is used to count figure numbers in each
chapter of a topic. It does this by counting the
number of preceding < figure > elements in each
chapter and adding one to calculate the current
figure number. This allows sequential numbering of
figures in each chapter as the topic is converted
from XML to HTML or PDF.
Search WWH ::




Custom Search