Home | SourceForge | Forums | Contact

Example: XQuery use in the Web-Harvest

This example doesn't solve some real-life problem, but demonstrates how XQuery processor may be used inside Web-Harvest. There are several issues worth noting:

  • Several external parameters are proceeded to XQuery with varous types.
  • Each parameter has it's proper declaration inside XQuery expression.
  • Standard Java library java.lang.Math is used inside XQuery, previously declared in the separate namespace.
  • New function myFunc is defined and used inside XQuery expression.

<?xml version="1.0" encoding="UTF-8"?>
<config charset="UTF-8">
    <file action="write" path="xquerytest.xml">
            <xq-param name="logicvalue" type="boolean">true</xq-param>
            <xq-param name="numbervalue" type="double">12</xq-param>
            <xq-param name="intseq" type="integer*">
            <xq-param name="stringvalue" type="string">ABCDEFGH123456</xq-param>
            <xq-param name="doc"><![CDATA[
                (: example of external namespace and usage of Java function library inside the XQuery :)
                declare namespace math ="java:java.lang.Math";
                (: declare namespace for user-defined functions :)
                declare namespace fn ="f:ns";
                (: declaration of external variables with names and types matching to those in xq-param :)
                declare variable $logicvalue as xs:boolean external;
                declare variable $numbervalue as xs:double external;
                declare variable $intseq as xs:integer* external;
                declare variable $stringvalue as xs:string external;
                declare variable $doc as node()* external;
                (: user-defined function inside XQuery :)
                declare function fn:myFunc($items as xs:integer*) as node()* {
                    for $num in $items return
                            { math:sqrt($num) + 1 }
                (: resuting XML :)
                        { if ($logicvalue) then 1 else 2 },
                        { $logicvalue eq ($numbervalue gt 15) }
                        { $numbervalue * 2 + 10 },
                        { $numbervalue instance of xs:float },
                        { round($numbervalue) }
                        { concat($stringvalue, $logicvalue, $numbervalue) }
                        { fn:myFunc($intseq) }
                        { concat($intseq[1], "mama") }
                        { for $td in $doc//td return $td }

The result of execution is file xquerytest.xml.