我正在尝试从我的 XML 文档中检索作者,但一些作者的姓名中有撇号,因此结果会引发错误。
输入:
<dblp>
<book mdate="2002-01-03" key="books/aw/CeriF97">
<author>Stefano Ceri</author>
<author>Piero Fraternali</author>
<title>Designing Database Applications with Objects and Rules: The IDEA Methodology</title>
<publisher href="db/publishers/aw.html">Addison-Wesley</publisher>
<year>1997</year>
<isbn>0-201-40369-2</isbn>
</book>
</dblp>
Java/XQuery 代码:
public ArrayList<String> getArrayListOfAuthors(){
String query = "for $x in fn:distinct-values(doc(\"" +xml_file_name+ "\")//author) " +
"order by $x "+
"return $x";
System.out.println("XQuery query:"+query);
ArrayList<String> myList = new ArrayList<String>();
try{
XQDataSource ds = new SaxonXQDataSource();
XQConnection conn = ds.getConnection();
XQExpression exp = conn.createExpression();
XQSequence seq = exp.executeQuery(query);
int i = 1;
while (seq.next()) {
i++;
//System.out.println(seq.getAtomicValue());
myList.add(seq.getAtomicValue());
}
//System.out.println("\n== Total number of authors is "+i+" ==");
seq.close();
} catch (XQException err) {
System.out.println("Failed as expected: " + err.getMessage());
}
return myList;
}
错误信息:
XPST0003 XQuery syntax error near #...e $y/author = 'Kieran O'Neill'#:
Unmatched quote in expression
Error on line 1 column 109