how to set xquery context document in java using saxon 9.2he?

╄→尐↘猪︶ㄣ 提交于 2019-12-24 19:23:09

问题


how do I execute a simple this xquery, such as this

for $elem in /root/element()
return 
    $elem

on an xml file using java without using fn:doc?

I keep getting XPDY0002: The context item for axis step child::element(xml, xs:anyType) is undefined

--the rundown: I need a simple solution to load an xml file, load an xquery and process


回答1:


here is the simplest way I found to do it however I was hoping to use XQStaticContext to "bind" the contextDocument to the expression.

XQDataSource ds = new SaxonXQDataSource();
XQConnection xqjc = ds.getConnection();
XQPreparedExpression xqje = xqjc.prepareExpression(new FileInputStream("xml\\foo.xquery"));

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = factory.createXMLStreamReader(new FileReader("xml\\foo.xml"));

xqje.bindDocument(XQConstants.CONTEXT_ITEM,streamReader, xqjc.createDocumentType());

XQResultSequence xqjs  = xqje.executeQuery();

xqjs.writeSequence(System.out, null);


来源:https://stackoverflow.com/questions/3111473/how-to-set-xquery-context-document-in-java-using-saxon-9-2he

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!