Reputation: 65
I am using saxon s9api to transform xml with XQuery. With below code it is possible but I'm getting XPathException.
The XQueryEvaluator expects Element node but I am not sure how to get Element from Document node.
I have tried to iterating and passing the children of the document.
// first java class
Processor processor = new Processor(false);
DocumentBuilder db = processor.newDocumentBuilder();
XdmNode doc = db.build(new StreamSource(new
StringReader(innerResponse.getBody())));
// second java class where new processor is created.
XQueryCompiler compiler = processor.newXQueryCompiler();
XQueryExecutable executable =
compiler.compile(getXQueryFileAsString(interfaceId));
XQueryEvaluator query = executable.load();
query.setExternalVariable(new QName("result1"), ((XdmNode) doc));
XdmValue nodes = query.evaluate();
// XQuery
(:: pragma bea:global-element-parameter parameter="$result1" element="result" location="../XMLSchemas/myxsd.xsd" ::)
(:: pragma bea:global-element-return element="result" location="../XMLSchemas/anotherxsd.xsd" ::)
declare namespace xf = "http://tempuri.org/somepath/XQueries/Result/";
declare function xf:Result($result1 as element(result),
// input xml
<?xml version='1.0' encoding='UTF-8'?>
<result>
<code>OK</code>
<somedata>
..
</somedata>
</result>
Exception:
XPTY0004: The required item type of the value of variable $result1 is element(Q{}result); the supplied value doc() does not match. The supplied value is a document node net.sf.saxon.s9api.SaxonApiException: The required item type of the value of variable $result1 is element(Q{}result); the supplied value doc() does not match. The supplied value is a document node at net.sf.saxon.s9api.XQueryEvaluator.evaluate(XQueryEvaluator.java:430)
Upvotes: 0
Views: 430
Reputation: 163262
Assuming that the result
element is the outermost element of the document, and that you are using Saxon 9.9, use
doc.select(child("result")).asNode();
to get the result
element.
You will need
import static net.sf.saxon.s9api.streams.Steps.child;
to access the child()
method
Upvotes: 0