Reputation: 827
On trying to parse an xml with Xquery i am getting the following exception, javax.xml.xquery.XQException: A processing instruction must not be named 'xml' in any combination of upper and lower case. Syntax error on line 1 at column 1 of near {...rsion="1.0" encoding="UTF-8...} XPST0003: A processing instruction must not be named 'xml' in any combination of upper and lower case. Given below is the XML file. Can someone please suggest what needs to be done here.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Upvotes: 1
Views: 1612
Reputation: 1436
try with this,
XQueryMain.java (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 XQueryMain {
public static void main(String[] args){
try {
InputStream inputStream = new FileInputStream(new File("condition.xqy"));
XQDataSource dataSource = new SaxonXQDataSource();
XQConnection connection = dataSource.getConnection();
XQPreparedExpression preparedExpression = connection.prepareExpression(inputStream);
XQResultSequence resultSequence = preparedExpression.executeQuery();
while (resultSequence.next()) {
System.out.println(resultSequence.getItemAsString(null));
}
}
catch (FileNotFoundException | XQException e) {
e.printStackTrace();
}
}
}
condition.xqy (XQuery)
for $x in doc("bookstore.xml")/bookstore/book
where $x/price=30
return $x/title
bookstore.xml (XML)
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
following jar files (SaxonHE9-9-0-2J) add to class path
Upvotes: 0
Reputation: 163262
I think you have presented an XML file to the XQuery processor when it is expecting an XQuery file. Most things in XML are also valid in XQuery, but the XML declaration is an exception. (It would be recognized as a processing instruction, except that processing instructions named "xml" are not allowed).
Check how you are invoking your XQuery processor. You're supplying an XML data file where it expects a query.
Upvotes: 4