XQuery - Пользовательские функции

XQuery предоставляет возможность писать собственные функции. Ниже приведены рекомендации по созданию пользовательской функции.

  • Используйте ключевое слово declare function для определения функции.

  • Использовать типы данных, определенные в текущей схеме XML

  • Enclose the body of function inside curly braces.

  • Prefix the name of the function with an XML namespace.

The following syntax is used while creating a custom function.

Syntax

declare function prefix:function_name($parameter as datatype?...)
as returnDatatype?
{
   function body...
};

Example

The following example shows how to create a user-defined function in XQuery.

XQuery Expression

declare function local:discount($price as xs:decimal?,$percentDiscount as xs:decimal?) as xs:decimal? { let $discount := $price - ($price * $percentDiscount div 100) return $discount
};

let $originalPrice := 100 let $discountAvailed := 10

return ( local:discount($originalPrice, $discountAvailed))

Output

90

Verify the Result

To verify the result, replace the contents of books.xqy (given in the Environment Setup chapter) with the above XQuery expression and execute the XQueryTester java program.