Clarity

You can increase the clarity of your queries by improving the layout of the query, making appropriate use of names, and using comments liberally. In addition to the recommendations in this chapter, you can go to http://www.xqdoc.org/xquery-style.html for some more detailed XQuery style conventions.

Improving the Layout

To make the structure of a query more obvious, you should make appropriate use of whitespace and parentheses. Whitespace (line breaks, spaces, and tabs) is allowed anywhere between keywords to make it more readable.

It is helpful to split longer FLWOR and conditional expressions into multiple lines and indent each clause to line up, as shown in Example 15-1. FLWORs embedded within FLWORs should be further indented. When constructing XML results, you should indent the element constructors just as you would indent the elements in an XML document.

Example 15-1. Making use of whitespace

Less clear query
for $product in doc("catalog.xml")//product return
<product><number>{$product/number}</number>
<price>{for $price in doc("prices.xml")//prod
where $product/number = $price/@num
return $price/price}</price>
</product>
More clear query
for    $product in doc("catalog.xml")//product
return <product>
         <number>{$product/number}</number>
         <price>{for    $price in doc("prices.xml")//prod
                 return $price/price}</price>
       </product>

Parentheses can be used around most expressions to group them together. If the beginning and end of an expression are not obvious, parentheses are highly recommended. For example, a complex where clause in a FLWOR, or a complex then clause in a conditional expression, are much clearer when wrapped in parentheses.

Choosing Names

Choosing meaningful names can also make a query much easier to understand. This includes the names of variables, functions, and function parameters. Names can also be used along with repeating let clauses to make an expression more clear. For example, in the expression:

let $substring := substring($myString,1,32)
let $substringNoQuotes := replace($substring,'"','')
let $substringUpperCase := upper-case($substringNoQuotes)
return $substringUpperCase

the names are bound to the string in various states of processing. This is more obvious than its equivalent:

upper-case(replace(substring($myString,1,32),'"',''))

Namespace prefixes should also be chosen carefully. When possible, use popular prefix conventions such as xs for XML Schema,wsdl for Web Services Description Language, and html for XHTML. If you are using several namespaces, assign prefixes to all of them rather than making one the default. This makes it more clear which namespace each name belongs to.

Using Comments for Documentation

An important part of writing understandable queries is documenting them. Comments delimited by (: and :) can appear anywhere that insignificant whitespace is allowed in a query. For example, they may appear at the end of a line to explain the expression on that line, as a separate line, or as a block on multiple lines, as in:

 (:::::::::::::::::::::::::::::::::
  : The following expression returns the price of a product
  : It assumes there is one price per product element
  ::::::::::::::::::::::::::::::::::)

A standard method of documenting XQuery modules and functions is by using xqdoc tags. These tags, listed in Table 15-1, appear in normal XQuery comments. All of them are optional and most are allowed to repeat.

Table 15-1. xqdoc tags

Tag

Meaning

@author

The author of the component

@version

The version number

@since

The first version (e.g., of a library) when a component is supported

@see

Where to go for additional information; it can be a URL or a textual description

@param

A description of a function parameter, in the form @param $name text

@return

A description of what a function returns

@deprecated

An indication that the component has been deprecated and should therefore no longer be used; text can follow the keyword for further explanation

@error

A description of a type of error the function might return

Once a module is documented using xqdoc tags, human-readable HTML documentation can be generated automatically. The process is very similar to that of Javadoc, which generates documentation for Java classes. For more information, or to download the scripts to generate the documentation, see http://www.xqdoc.org.

Example 15-2 shows a function that is documented using xqdoc comments. The documentation, which appears before the function declaration, contains a textual description of the function, followed by the @param and @return tags to describe the inputs and output of the function. HTML tags (the b elements) are used in the description to enhance the display of the description in the resulting HTML documentation.

Example 15-2. Documenting a function with xqdoc

(:~
: The <b>functx:substring-after-last</b> function returns the part
: of <b>$string</b> that appears after the last occurrence of
: <b>$delim</b>. If <b>$string</b> does not contain
: <b>$delim</b>, the entire string is returned.
:
: @param $string the string to substring
: @param $delim the delimiter
: @return the substring
:)
declare function functx:substring-after-last
($string as xs:string?, $delim as xs:string) as xs:string?
 { ...  };
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.238