
- XQuery Tutorial
- XQuery - Home
- XQuery - Overview
- XQuery - Environment Setup
- XQuery - First Application
- XQuery - FLWOR
- XQuery - HTML Format
- XQuery - XPath
- XQuery - Sequences
- XQuery - Sequence Functions
- XQuery - String functions
- XQuery - Date Functions
- XQuery - Regular Expressions
- XQuery - If Then Else
- XQuery - Custom Functions
- XQuery Useful Resources
- XQuery - Quick Guide
- XQuery - Useful Resources
- XQuery - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
XQuery - Custom Functions
XQuery provides the capability to write custom functions. Listed below are the guidelines to create a custom function.
Use the keyword declare function to define a function.
Use the data types defined in the current XML Schema
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.
Advertisements