Wednesday, April 28, 2010

XQuery tutorial: frequently used stuff

See here http://en.wikibooks.org/wiki/XQuery  for a wealth of excellent examples.

Also http://www.xqueryfunctions.com/xq/  is wonderful.

XQuery is a terrible pain in the knee, but if you get organized it's only a matter of copying/pasting/hacking....

___________________

Variable substitution in a text:

xquery version "1.0" encoding "Cp1252";
(:: pragma  type="xs:anyType" ::)

declare namespace xf = "http://tempuri.org/PVOSBProject1/XQExceptionHandling/";

declare function xf:XQExceptionHandling($thefault as xs:string)
as element(*) {
<ws:handleException xmlns:ws="http://com/pierre/ws">
    <ws:fault>{$thefault}</ws:fault>
</ws:handleException>   
};


declare variable $thefault as xs:string external;

xf:XQExceptionHandling($thefault)

___________________

if you want to pass any xml, use
declare variable $thefault as element(*) external;


______


if you callout a service expecting a String , you cannot pass an XML.... first you must fn-bea:serialize($thevariable)

the function fn:string($body) will somehow generate a toString() of your XML
_____________

Assume this is the $body:


<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <dbac:insertCompany xmlns:dbac="http://com/acme/dbaccess">
    <dbac:company xmlns:java="java:com.acme.dbaccess">
      <java:CreationDate>3</java:CreationDate>
      <java:Id>2</java:Id>
      <java:Name>Pluto</java:Name>
    </dbac:company>
  </dbac:insertCompany>
</soapenv:Body>


$body/.  means all the body... equivalent to $body

$body/dbac:company returns this:

<dbac:company      xmlns:java="java:com.acme.dbaccess" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:dbac="http://com/acme/dbaccess">
    <java:CreationDate>3</java:CreationDate>
    <java:Id>2</java:Id>
    <java:Name>Pluto</java:Name>
    </dbac:company>


$body/*/node()  also returns

<dbac:company      xmlns:java="java:com.acme.dbaccess" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:dbac="http://com/acme/dbaccess">
    <java:CreationDate>3</java:CreationDate>
    <java:Id>2</java:Id>
    <java:Name>Pluto</java:Name>
    </dbac:company>

(be careful: $body/node() is different from $body/*/node() )

 $operation will contain insertCompany

 $body/node() will return
  <dbac:insertCompany xmlns:dbac="http://com/acme/dbaccess">
    <dbac:company xmlns:java="java:com.acme.dbaccess">
      <java:CreationDate>3</java:CreationDate>
      <java:Id>2</java:Id>
      <java:Name>Pluto</java:Name>
    </dbac:company>
  </dbac:insertCompany>

use the condition fn:compare($a, $b) = 0 to test equality of 2 strings

fn:name($body) will return  dbac:insertCompany

fn:local-name($body) will return  insertCompany

fn:node-name($body): {http://com/acme/dbaccess}insertCompany

___________

XQuery: converting nodes in a CSV list

I have this:

<getLocationsByLocationIds>
    <string>string_1</string>
    <string>string_2</string>
</getLocationsByLocationIds>


and I want to convert it into this:
string_1,string_2


use this:

fn:string-join( $body/getLocationsByLocationIds/string, ',')

___________

No comments: