Browse DevX
Sign up for e-mail newsletters from DevX


Introduction to XQuery (Part 4 of 4) : Page 4

Learn to use XQuery conditional and quantified expressions to filter and select data, and to modify query results by including custom functions.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

User-Defined Functions
You can augment the built-in function library by defining your own functions. A user-defined function begins with a define function clause followed by the body of the function enclosed in curly braces ({}). Here's a simple function example:

define function greetings() returns element { Hello World!! }

The define function clause defines the signature of the function— the name of the function, the number and types of parameters, and the return value type. In the preceding example, the greetings() function takes no parameters, but it returns an element named greetings. Here's the same function defined to accept a parameter:

define function greetings(xsd:string $message) returns element { {$message} }

Now the function takes a string parameter and returns a greetings element with the parameter value as its content.

Function signatures
A function signature describes the interface to a function. There are two important components to a function signature, the input parameter list and the return type. The input parameter list is a comma-separated list of formal parameters that the function will accept. Each parameter is bound to a variable that can be used within the function body.

The XQuery specification is a work in progress, but almost all the features described in this series are stable, and you can expect them to appear unchanged in the finished specification.
Here are a few sample function signatures:

define function trim(xsd:string $str) returns xsd:string {...}

The ellipsis (...) represents the body of the function (not shown in the example). Even without the function body, you can tell from the signature that the function accepts a string parameter and returns a string as output. The function assumes that the prefix xsd is mapped to the XML schema namespace using the namespace clause.

define function xyz($p) {...}

Notice that this function signature is missing the data type of the parameter and return values, meaning the parameter and return value can be any type.

define function formatItem(element item $item) returns element {...}

This function accepts only elements named item. Passing any other parameter type or any element type not named item causes an error.

define function formatItem(element of type ix:itemType $item) returns element {...}

This function accepts only items of the type itemType in the target namespace mapped by the prefix ix, and shows that XQuery supports strongly-typed functions that use custom types.

Function body
The function body is enclosed in curly braces ({}). It can be arbitrarily complex in the sense that it can include any of the different types of expressions discussed in this series, but it must return a value of the type declared in the function signature.

Pointers to the future
This series was intended to give you a quick, yet reasonably comprehensive introduction to XQuery. Hopefully, you've seen enough to get started writing queries in this simple yet powerful query language. The XQuery specification is a work in progress, but almost all the features described in this series are stable, and you can expect them to appear unchanged in the finished specification. In the future, features such as update syntax, a well-defined type system etc., will be added to the query language. As more applications choose to maintain their data in XML, XQuery may well become the SQL of the future.

Srinivas Pandrangi is an architect at Ipedo and a member of the W3C XML Query working group, where he is working on the standardization of XQuery. His article on XML performance techniques was published earlier this year in XML Journal. You can reach him at srinivas@ipedo.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date