gpt4 book ai didi

java - 如何在 Saxon 的 XQuery 中动态引用 XML 文件

转载 作者:数据小太阳 更新时间:2023-10-29 01:56:28 26 4
gpt4 key购买 nike

我正在使用 XQuery 处理器 Saxon。
现在我们将 XQuery 写在一个“.xqy”文件中,我们引用将在其上执行 XQuery 的 XML 文件。
请看下面的例子:

for $x in doc("books.xml")/books/book
where $x/price>30
return $x/title

现在我想使用未存储在某些路径中的动态生成的 XML。比方说,我想在下面引用以字符串形式提供的 XML。

该怎么做?

String book=
<books>
<book category="JAVA">
<title lang="en">Learn Java in 24 Hours</title>
<author>Robert</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="DOTNET">
<title lang="en">Learn .Net in 24 hours</title>
<author>Peter</author>
<year>2011</year>
<price>40.50</price>
</book>
<book category="XML">
<title lang="en">Learn XQuery in 24 hours</title>
<author>Robert</author>
<author>Peter</author>
<year>2013</year>
<price>50.00</price>
</book>
<book category="XML">
<title lang="en">Learn XPath in 24 hours</title>
<author>Jay Ban</author>
<year>2010</year>
<price>16.50</price>
</book>
</books>

Java代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;

import com.saxonica.xqj.SaxonXQDataSource;

public class XQueryTester {
public static void main(String[] args){
try {
execute();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XQException e) {
e.printStackTrace();
}
}

private static void execute() throws FileNotFoundException, XQException{
InputStream inputStream = new FileInputStream(new File("books.xqy"));
XQDataSource ds = new SaxonXQDataSource();
XQConnection conn = ds.getConnection();
XQPreparedExpression exp = conn.prepareExpression(inputStream);
XQResultSequence result = exp.executeQuery();
while (result.next()) {
System.out.println(result.getItemAsString(null));
}
}
}

最佳答案

如果您正在寻找一种使用 Java 绑定(bind)查询输入(上下文项)的方法,我建议您使用 Saxon 的 S9API(Java 中用于 XSLT、XPath 和 XQuery 处理的最直观的 API) ).

下面是如何实例化 Saxon、编译查询、解析输入并使用绑定(bind)的输入文档作为其上下文项评估查询:

// the Saxon processor object
Processor saxon = new Processor(false);

// compile the query
XQueryCompiler compiler = saxon.newXQueryCompiler();
XQueryExecutable exec = compiler.compile(new File("yours.xqy"));

// parse the string as a document node
DocumentBuilder builder = saxon.newDocumentBuilder();
String input = "<xml>...</xml>";
Source src = new StreamSource(new StringReader(input));
XdmNode doc = builder.build(src);

// instantiate the query, bind the input and evaluate
XQueryEvaluator query = exec.load();
query.setContextItem(doc);
XdmValue result = query.evaluate();

请注意,如果 Java 代码正在生成 XML 文档,我强烈建议您使用 S9API 直接在内存中构建树,而不是将 XML 文档生成为字符串,然后对其进行解析。如果可能的话。

关于java - 如何在 Saxon 的 XQuery 中动态引用 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com