This topic contains the XQuery main module example.xq.
import module namespace http = "http://www.endeca.com/XQuery/http/2008" at "http.xq"; import module namespace eutil = "http://www.endeca.com/XQuery/eutil/2008" at "eutil.xq"; import module namespace mdex = "http://www.endeca.com/XQuery/mdex/2008" at "mdex.xq"; declare namespace soap = "http://schemas.xmlsoap.org/soap/envelope/"; declare namespace mdata = "http://www.endeca.com/MDEX/data/IR600"; declare namespace ex = "http://endeca.com/example.xsd";
declare function local:generateFaultResponse($message as xs:string*) as element(soap:Envelope, xs:untyped) { <soap:Envelope> <soap:Body> <soap:Fault> <mdata:query-fault> <mdata:fault-string>{$message}</mdata:fault-string> </mdata:query-fault> </soap:Fault> </soap:Body> </soap:Envelope> };
declare function local:generateWsdl() { <definitions name="ExampleMDEXQuery" targetNamespace="http://endeca.com/example.wsdl" xmlns:es="http://endeca.com/example.wsdl" xmlns:esxsd="http://endeca.com/example.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <xsd:schema targetNamespace="http://endeca.com/example.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- request contains just a word to be matched --> <xsd:element name="MatchWord"> <xsd:complexType> <xsd:sequence> <xsd:element name="word" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <!-- response contains just the number of matches --> <xsd:element name="MatchCount"> <xsd:complexType> <xsd:all> <xsd:element name="count" type="xsd:int"/> </xsd:all> </xsd:complexType> </xsd:element> </xsd:schema> </types> <!-- request message uses an element from the above schema --> <message name="CountMatchesInput"> <part name="word" element="esxsd:MatchWord"/> </message> <!-- response message uses an element from the above schema --> <message name="CountMatchesOutput"> <part name="count" element="esxsd:MatchCount"/> </message> <!-- port type specifies the above request and response message --> <portType name="ExampleMDEXPortType"> <operation name="CountMatches"> <input message="es:CountMatchesInput"/> <output message="es:CountMatchesOutput"/> </operation> </portType> <!-- binding specifies the above port type and declares this web service to be of the document/literal variety --> <binding name="ExampleMDEXSoapBinding" type="es:ExampleMDEXPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="CountMatches"> <soap:operation soapAction="http://endeca.com/Example"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <!-- associate the above port type and binding --> <service name="ExampleMDEXService"> <documentation>Endeca Example MDEX Query Service</documentation> <port name="ExampleMDEXPortType" binding="es:ExampleMDEXSoapBinding"> <soap:address location="http://endeca.com/mdex"/> </port> </service> </definitions> };
(: get the request :) let $body-str := fn:trace(http:get-body(), "REQUEST") return if (fn:exists(http:get-query-parameter("WSDL"))) then (: caller used ?WSDL request format -- send WSDL :) local:generateWsdl() else if (fn:empty($body-str)) then (: no request body -- send SOAP fault :) local:generateFaultResponse("Empty body in SOAP request") else (: reach into the SOAP request body :) let $body := eutil:parse(fn:exactly-one($body-str))/soap:Envelope/soap:Body (: pull out the input message :) let $query := $body/ex:MatchWord return if (fn:count($query) ne 1) then (: should be exactly one input message :) local:generateFaultResponse($query) else (: start making a response enveleope :) fn:trace( <soap:Envelope> <soap:Body> { (: setup an MDEX API call to do the text search :) let $str := fn:data($query/ex:word) let $cl := <mdata:Query> <mdata:Searches> <mdata:Search Key = "English">{$str}</mdata:Search> </mdata:Searches> </mdata:Query> (: call MDEX API :) let $result := mdex:navigation-query($cl) (: fill in an output message :) return <ex:MatchCount> <ex:count> { fn:data($result/mdata:RecordsResult/@TotalRecordCount) } </ex:count> </ex:MatchCount> } </soap:Body> </soap:Envelope>, "RESPONSE")