Page 1 of 1

javax.xml.xpath.XPathExpressionException

PostPosted: Fri Apr 13, 2012 1:32 am
by Nanocom
Hi, me again :)
I have a problem when calling getJAXBNodesViaXPath("//w:bookmarkStart") (the problem is still there with any other valid xpath expression) on any header, document or footer part, when the part is containing a floating image ("in front of the text").
It's related to my last issue : http://www.docx4java.org/forums/docx-java-f6/classcastexception-org-docx4j-wml-tblpr-cannot-be-cast-t1051.html.
My stack trace :
Code: Select all
java.lang.RuntimeException: javax.xml.xpath.XPathExpressionException
   at org.docx4j.XmlUtils.xpath(XmlUtils.java:922)
   at org.docx4j.XmlUtils.xpath(XmlUtils.java:895)
   at org.docx4j.XmlUtils.getJAXBNodesViaXPath(XmlUtils.java:873)
   at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getJAXBNodesViaXPath(MainDocumentPart.java:175)
...
Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context
   at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
   at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210)
   at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335)


The argument nodehandle of the setRoot(,) method is -1, as node is null in getJAXBNodesViaXPath when called on the part containing the floating image :
Code: Select all
public static List<Object> getJAXBNodesViaXPath(Binder<Node> binder,
         Object jaxbElement, String xpathExpr, boolean refreshXmlFirst)
         throws JAXBException {
      
      Node node;
      if (refreshXmlFirst)
         node = binder.updateXML(jaxbElement);
      [color=#FF0000]node = binder.getXMLNode(jaxbElement);[/color]
      
      //log.debug("XPath will execute against: " + XmlUtils.w3CDomNodeToString(node));
      
        List<Object> resultList = new ArrayList<Object>();
        for( Node n : xpath(node, xpathExpr) ) {
...
}


Thanks.

Re: javax.xml.xpath.XPathExpressionException

PostPosted: Fri Apr 13, 2012 9:17 pm
by jason
The workaround in http://www.docx4java.org/trac/docx4j/changeset/1736 is not to use a binder if the class cast exception occurs. Without the binder, you can't evaluate xpaths. An alternative is to use TraversalUtil

Re: javax.xml.xpath.XPathExpressionException

PostPosted: Sat Apr 14, 2012 12:20 am
by Nanocom
Ok fine, will try this way. Thanks.