Saxon 8 problem (Java version)
I will now point out that I am new to using saxon and I tried following the docs and examples in the package, but I just had no luck with this problem.
Basically I am trying to do some XML processing in java using saxon v8. To get something working I took one of the sample files included in the package and modified it for my needs. It works as long as I don't use namespaces, which is my question. How can I get around the namespace problem? I don't really want to use it, but it exists in my xml, so I need to either use it or ignore it. Any solution is ok.
Anyway, here's my starter code. This does nothing but an xpath query trying to use it against the hardcoded xml document.
public static void main(String[] args) {
String query = args[0];
File XMLStream=null;
String xmlFileName="doc.xml";
OutputStream destStream=System.out;
XQueryExpression exp=null;
Configuration C=new Configuration();
C.setSchemaValidation(false);
C.setValidation(false);
StaticQueryContext SQC=new StaticQueryContext(C);
DynamicQueryContext DQC=new DynamicQueryContext(C);
QueryProcessor processor = new QueryProcessor(SQC);
Properties props=new Properties();
try{
exp=processor.compileQuery(query);
XMLStream=new File(xmlFileName);
InputSource XMLSource=new InputSource(XMLStream.toURI().toString());
SAXSource SAXs=new SAXSource(XMLSource);
DocumentInfo DI=SQC.buildDocument(SAXs);
DQC.setContextNode(DI);
SequenceIterator iter = exp.iterator(DQC);
while(true){
Item i = iter.next();
if(i != null){
System.out.println(i.getStringValue());
}
else break;
}
}
catch (Exception e){
System.err.println(e.getMessage());
}
}
Sample XML file here ...
<?xml version="1.0"?>
<ns1:animal xmlns:ns1="http://my.catservice.org/">
<cat>
<catId>8889</catId>
<fedStatus>true</fedStatus>
</cat>
</ns1:animal>
If I run this with a query including the namespace I get an error. For example:
/ns1:animal/cat/
gives an error: "The ns1 prefix was not declared".
If I remove ns1: from the request it gives nothing. If I parse the xml to remove the "ns1:" added to the "animal" I can successfully complete the request /animal/cat/
.
Any help would be greatly appreciated. Thanks.
a source to share
The error message correctly indicates that your xpath expression does not indicate which namespace prefix "ns1" means (binds to). Just because the document to work with using the binding for "ns1" does not mean that this is what to use: it's because in XML it has a namespace URI and prefixes are just convenient shortcuts to the real thing.
So: how do you define binding? There are 2 general ways; either provide a context that can resolve the prefix, or insert the actual URI in the XPath expression.
Regarding the first approach, this letter from author Saxon mentions the JAXP method XPath.setNamespaceContext (), similar to the Jaxen XPath Processor FAQ contains sample code that might help This is not very convenient since you need to implement the NamespaceContext, but as soon as you will have an implementation, you will be installed.
So, the notation approach ... let's see: Ten Tips for Using XPath and XPointer shows this example:
to map an element declared with a namespace like:
XMLNS: book = "http://my.example.org/namespaces/book"
you use an XPath name like:
{ http://my.example.org/namespaces/book } section
which, we hope, is understood by Saxon (or Jaksen).
Finally, I would recommend upgrading to Saxon9 if possible if you have any problems using one of the solutions above.
a source to share
If you want to do something out of the box, you can check out embedding-xquery-in-java . There's a github project that uses Saxon to evaluate some sample XQuery expressions.
Hello
a source to share