I have the below xquery - books.xq
declare namespace bo="http://www.example.org/bookstore";
declare variable $ doc as element() external ;
for $book in $doc/bo:books/bo:book
where xs:decimal($book/bo:price) gt 10.00
return
$book/bo:title
It takes the input as books.xml -
<tns:books xmlns:tns="http://www.example.org/bookstore">
<tns:book>
<tns:title>A Game of Thrones</tns:title>
<tns:price>10.99</tns:price>
</tns:book>
<tns:book>
<tns:title>The Pillars of the Earth</tns:title>
<tns:price>7.99</tns:price>
</tns:book>
</tns:books>
Now, below is the java code that executes this xquery. I took reference from XML Developer's Kit Programmer's Guide .
package testing;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.namespace.QName;
import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQConstants;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQItemType;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQSequence;
import javax.xml.xquery.XQStaticContext;
import oracle.xml.xquery.OXQDataSource;
public class Streaming {
public static void main(String[] args) throws XQException, IOException {
OXQDataSource ds = new OXQDataSource();
XQConnection con = ds.getConnection();
XQStaticContext ctx = con.getStaticContext();
ctx.setBindingMode(XQConstants.BINDING_MODE_DEFERRED);
con.setStaticContext(ctx);
FileInputStream input = new FileInputStream("books.xml");
FileInputStream query = new FileInputStream("books.xq");
XQPreparedExpression expr = con.prepareExpression(query);
query.close();
QName qname = new QName("http://www.example.org/bookstore", "books");
XQItemType itemType = con.createDocumentElementType( con.createElementType(qname, XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT));
expr.bindDocument(new QName("doc"), input, null, itemType);
XQSequence result = expr.executeQuery();
System.out.println(result.getSequenceAsString(null));
result.close();
input.close();
expr.close();
con.close();
}
}
This code throws the exception -
Exception in thread "main" javax.xml.xquery.XQQueryException:
{http://www.w3.org/2005/xqt-errors}XPTY0004 [{http://xmlns.oracle.com/xdk/xquery/error}XPTY0004f]: "document { element {http://www.example.org/bookstore}books { {http://www.w3.org/2001/XMLSchema}untyped } }": bad value for type document { element {http://www.example.org/bookstore}books { {http://www.w3.org/2001/XMLSchema}dayTimeDuration } }
at oracle.xml.xquery.xqjimpl.OXQCUtils.createXQQueryException(OXQCUtils.java:992)
at oracle.xml.xquery.xqjimpl.OXQCSequence.getSequenceAsString(OXQCSequence.java:450)
at testing.Streaming.main(Streaming.java:38)
Caused by: oracle.xml.xquery.exceptions.XQueryTypeException: {http://www.w3.org/2005/xqt-errors}XPTY0004 [{http://xmlns.oracle.com/xdk/xquery/error}XPTY0004f]: "document { element {http://www.example.org/bookstore}books { {http://www.w3.org/2001/XMLSchema}untyped } }": bad value for type document { element {http://www.example.org/bookstore}books { {http://www.w3.org/2001/XMLSchema}dayTimeDuration } }
But If I remove as element() from line 3 of xquery and pass null as itemType in line 37 of java code, the java code runs successfully, but as soon as I give as element() in xquery, the code throws exception. I want to run java code by keeping the element() or any specific element type . I don't know how a specific type has to be passed from java.
Kindly provide any sample code if possible. Thanks in advance.