|
Home >Online Product Documentation >Table of Contents >The Principal Parts of an XQuery FLWOR Expression The Principal Parts of an XQuery FLWOR Expression
The name FLWOR comes from the five clauses that make up a FLWOR expression: F is for For
The behavior of the
returns the sequence (1, 4, 9, 16, 25, 36, 49, 64, 81, 100). In this example, the input items are simple numbers, and the output items are also simple numbers. Numbers are an example of what XQuery calls atomic values; other examples are strings, dates, booleans, and URIs. But the XQuery data model allows sequences to contain XML nodes as well as atomic values, and the Here is an example that takes nodes as input, and produces numbers as output. It counts the number of actors listed for each video in a data file: You can run this in Stylus Studio or in Saxon, using the example videos.xml file as input. (Tips on setting up these tools are described in the previous section, An XQuery Primer.) Here's the output from Stylus Studio: The Preview window shows the result: a rather mundane sequence of numbers (2, 4, 3, 3, 3...).
This gives us a good opportunity to point out that a FLWOR expression is just an expression, and you can use it anywhere an expression is allowed: it doesn't have to be at the top level of the query. There is a function, The answer is 2.2941176 and a bit - the number of decimal places shown will depend on the XQuery processor that you use. If you are only interested in the answer to two decimal places, try:
which gives a more manageable answer of 2.29. (The strange name
If you are coming from SQL, your instinct was probably to try and do the averaging and rounding in the As with some of the examples in An XQuery Primer, XPath 2.0 allows you to write this example using path expressions alone if you prefer:
We have seen a And we can get from one sequence of nodes to another sequence of nodes. This example shows all the actors that appear in any video:
In fact, this last example probably represents the most common kind of Once again, you could write this example as an XPath expression: However, this time the two expressions are not precisely equivalent. Try them both in Stylus Studio: the FLWOR expression produces a list containing 38 actors, while the list produced by the path expression contains only 36. The reason is that path expressions eliminate duplicates, and FLWOR expressions do not. Two actors are listed twice because they appear in more than one video.
The FLWOR expression and the "
The L is for Let
The XQuery Hopefully the meaning of that is fairly intuitive. In fact, in this example you can simply replace each variable reference by the expression that provides the expression's value. This means that the result is the same as this:
In a In most cases, variables are used purely for convenience, to simplify the expressions and make the code more readable. If you need to use the same expression more than once, then declaring a variable is also a good hint to the XQuery processor to only do the evaluation once.
In a FLWOR expression, you can have any number of To understand this, just translate it into English:
For each choice of genre, let's call the set of videos in that genre Here is the result in Stylus Studio: As a quick aside, the Stylus Studio XQuery Mapper allows you to visually map from one or more XML input documents to any target output format. In a nutshell - click on the Mapper tab on the bottom of the XQuery Editor. Next, click Add Source Document and add your source document(s). Our last XQuery would look like this in the XQuery Mapper: The FLWOR block is graphically represented as a function block with three input ports going into it on the left (For, Where, Order By), a flow control port on the top, and an output port on the right. As you draw your XML mappings, Stylus Studio writes the XQuery code; similarly, you can edit the XQuery code manually and Stylus Studio which will update the graphical model - both views of the XQuery are kept synchronized. See Building an XQuery Using the Mapper for more information on the Mapper module.
One important thing to note about variables in XQuery (you can skip this if you already know XSLT, because the same rule applies there): variables cannot be updated. This means you cannot write something like You might ask yourself at this point, Isn't a variable being updated when we write something like the following?
(This query shows the running time of each video. It first converts the stored value from a string to an integer, then multiplies it by one minute (
Here the variable The result is not a single number, but a sequence of numbers, one for each video. This example is actually declaring two separate variables that happen to have the same name. You are allowed to use the same variable name more than once, but this is probably not a good idea, because it will only get your readers confused. You can see more clearly what this query does if we rename one of the variables. which is the same as this:
Hopefully it is now clear why this returns a sequence of numbers rather than a single total. The correct way to get the total duration is to use the W is for Where
The XQuery
This style of coding is something that SQL users tend to be very comfortable with: first define all the tables you are interested in, then define a
Although many users seem to find that this style comes naturally, an alternative is to do the restriction in a predicate attached to one of the Perhaps there is a balance between the two; you will have to find the style that suits you best. With some XQuery processors one style or the other might perform better (and with Stylus Studio, you can easily create multiple XQuery scenarios that execute the same code but use different XQuery processors), but a decent optimizer is going to treat the two forms as equivalent.
Do remember that in a predicate, you select the item that you are testing relative to the context node, while in the O is for Order By
If there is no
Often however you want the query results in sorted order, and this can be achieved using the Note that we have not actually included the sort keys in the data that we are returning (which makes it a little difficult to verify that it is working properly; but it is something you might well want to do in practice). We have explicitly converted the user-rating to a number here to use numeric sorting: this makes sure that 10 is considered a higher rating than 2. This is not necessary if the query is schema-aware, because the XQuery processor then knows that user-rating is a numeric field.
Ordering gets a little complicated when there is more than one
To understand this we have to stop thinking about the two to sort first on the surname, then on the year, then on the first name (admittedly, nonsensical coding, but we show it only to illustrate that it is allowed). The XQuery specification introduces a concept of tuples, borrowed from the relational model, and describes how the sort works in terms of creating a sequence of tuples containing one value for each of the variables, and then sorting these notional tuples. R is for Return
Every XQuery FLWOR expression has a
Usually the XQuery which selects all the actors for each comedy video. However, the result is a little unsatisfactory, because we cannot tell which actors belong to which video. It is much more common here to construct an element wrapper around each result: We have not discussed XQuery element and attribute constructors until now. But in practice, a FLWOR expression without element constructors can only produce flat lists of values or nodes, and that is not usually enough. We usually want to produce an XML document as the output of the query, and XML documents are not flat. This means that very often, instead of doing purely relational joins that generate a flat output, we want to construct hierarchic output using a number of nested FLWOR expressions. Here is an example that (like the previous query) lists the videos for each actor, but with more structure this time: Here we really do have two nested XQuery loops. The two queries below are superficially similar, and in fact they return the same result: and:
But now add an and:
The difference now becomes apparent. In the first case the result sequence is a1, a2, a3, ... b1, b2, b3,. In the second case it remains a1, b1, c1,... a2, b2, c2. The reason is that the first query is a single FLWOR expression (one
So, the |
The Stylus Studio Advantage
Read in their own words, why customers have decided to make the switch to Stylus Studio, the World's best XML editor.
Convert CSV to XML
Convert CSV to XML with a handy CSV to XML document wizard that helps you extract data from your CSV files with ease.
Hotels, Casinos & Resorts That Use Stylus Studio
What's more romantic than a vacation for two and the world's best XML IDE at your finger tips? Nothing, that's why these hotels, casinos, & resorts are using Stylus Studio to help you get on your dream vacation.
Support for DataDirect SequeLink
Learn about Stylus Studio's support for DataDirect SequeLink, an end-to-end database middleware solution that provides a universal thin client for ODBC, JDBC, ADO or .NET, providing flexibility for both client- and server-based deployments.





