Databases Reference
In-Depth Information
FLWOR with loop
without any “let”
statements
Figure 5.6
A sample
FLWOR statement in XQuery
that shows the for, where,
order, and return
statements. Many
structures in XQuery, such
as the
where
and
order
by
statements, are similar
to SQL, so many SQL
developers quickly learn
basic XQuery statements.
Get all “sale” nodes in this XML file
“Where” clause similar
to SQL returns only
amounts over 15
“Order by” clause to
change sort order
for $sale in doc('sales.xml')//sale
where $sale/amount >15
order by $sale/amount descending
return
$sale
“Return” returns
a sequence of
sale nodes
experts in query language design. Some of these experts assisted in developing stan-
dards for
SQL
, and in time found
SQL
inappropriate for querying nontabular data.
The
W3C
query standards group was charged with creating a single query language
that would work well for all use cases, which included business as well as text docu-
ments, books, and articles. Beginning with a list of relational and document query lan-
guages and a set of 70 use cases for structured and unstructured data sets, the
W3C
group embarked on an multiyear mission to define the XQuery language.
XQuery was designed to be a flexible language that allowed each of the 70 use
cases to be queried and run on multiple processors, and to be easy to learn, parse, and
debug. To accomplish its task, it borrowed concepts from many other query systems.
This rigorous process resulted in a query language that's widely adopted and used in
many products. These XQuery products include not only native
XML
databases, but
also tools for in-memory data transformation and integration tools.
XQ
UERY
—
A
FUNCTIONAL
PROGRAMMING
LANGUAGE
XQuery is defined as a
functional programming
language
because its focus is the parallel
transformation of sequences of data items using functions. Note that we'll cover the
topic of functional programming in depth in chapter 11. With XQuery, functions can
be passed as parameters to other functions. XQuery has many features not found in
SQL
that are used for the efficient transformation of hierarchical
XML
data. For exam-
ple, XQuery allows you to call recursive functions and returns not only tables but any
other tree-like data structures. XQuery can return simple
XML
or a sequence of items
including
JSON
or graph structures. Due to its functional nature, XQuery can be eas-
ier to execute on multiple
CPU
systems.
The parallel processing power of XQuery is the
FLWOR
statement
.
FLWOR
stands for
for, let, where, order, and return
, as shown in figure 5.6. Unlike the for loops found in pro-
cedural languages such as Java or .Net,
FLWOR
statements can execute in independent
parallel processes and run on a large number of parallel
CPU
s or different processors.
XQ
UERY
—
CONSISTENT
WITH
WEB
STANDARDS
XQuery is designed to be consistent with other
W3C
standards beyond XPath. For
example, XQuery shares data types with other
XML
standards such as
XML
Schema,
XProc, and Schematron. Because of this standardization, XQuery implementations
tend to be more portable than applications that are ported between
SQL
databases.