Project

Profile

Help

XPath for Document Manipulation

Added by Anonymous over 19 years ago

Legacy ID: #3188828 Legacy Poster: Jeffrey Lage (jlage)

I will creating a class that will process an xpath statement against a source document and then either remove the nodes that are return from that statement, or insert a fragment as a child to each node returned. couple of questions if anyone has any thoughts: 1. should i use saxon 7.5 or 8.4? 2. given that will be manipulating the source document, i need editable doms, with it seems saxon created readonly doms by default. anyone have thoughts on what i should do to get around that? thanks.


Replies (7)

Please register to reply

RE: XPath for Document Manipulation - Added by Anonymous over 19 years ago

Legacy ID: #3188897 Legacy Poster: Michael Kay (mhkay)

I suggest you use Saxon 8.4 for this rather than 6.5.3, because the free-standing XPath API is much better documented and more standard, and also because it allows access to a Saxon tree that wraps a XOM, DOM, or JDOM tree. If you create a Saxon wrapper over such a tree, the results of the XPath expression will return nodes in the underlying tree, which you can then manipulate (e.g. by doing deletes) using the native API for that object model. I would suggest using XOM as it's the cleanest of the three models from a Java programming viewpoint. You might like to look at nux, which packages Saxon XQuery access to XOM. Michael Kay

RE: XPath for Document Manipulation - Added by Anonymous over 19 years ago

Legacy ID: #3188901 Legacy Poster: Jeffrey Lage (jlage)

Thanks for the quick reply. Not to sound like I don't know what I am talking about, but what exaclty do you mean by 'Saxon wrapper of such a tree'... i just tried doing a quick test for myself and the 1.4 JDK seems to use crimson by default with using DocumentBuilder and i get the following error: javax.xml.xpath.XPathExpressionException: Cannot locate an object model implementation for nodes of class org.apache.crimson.tree.XmlDocument what exactly am i doing wrong?

RE: XPath for Document Manipulation - Added by Anonymous over 19 years ago

Legacy ID: #3188960 Legacy Poster: Jeffrey Lage (jlage)

I was able to get around the crimson issue, as crimson will only be involved in my lazy testing. however, i think that it might make sense for me to elaborate a little more on what I am trying to accomplish: I am looking to integrate a service in a SOA environment that will allow me to manipulate my xml messages. specifically, i will have an xml document and an xpath statement. my jms provider has a specific XMLMessage message type that will likely (is created the first time it is requested) already contain my xml as an implementation of org.w3c.dom.Documnet. I need to be able to apply my xpath statement to that object and then remove from the document all matching nodes. if that is not a possibiliyt, i also have access to the document as text and can use something like xom, assume it supports this concept. thanks.

RE: XPath for Document Manipulation - Added by Anonymous over 19 years ago

Legacy ID: #3189391 Legacy Poster: Michael Kay (mhkay)

If you're using DOM (and I really don't recommend it...) then you must have saxon8-dom.jar on the class path; if you're using xom or jdom, there are similarly-named support libraries that must be on the class path. For DOM you get a Saxon wrapper automatically if you supply the input as a DOMSource. For XOM or JDOM, you construct the relevant wrapper yourself: see the JavaDoc for the packages net.sf.saxon.xom and net.sf.saxon.jdom.

RE: XPath for Document Manipulation - Added by Anonymous over 19 years ago

Legacy ID: #3189629 Legacy Poster: Jeffrey Lage (jlage)

I am not especially tied to DOM. I made an additional post yesterday elaborating on my problem, but I think I put it in out of place. Here it is again: I am looking to integrate a service into a SOA environment that will allow me to manipulate my xml messages. specifically, i will have an xml document and an xpath statement. my jms provider has a specific XMLMessage message type that will likely (as it is created the first time it is requested) already contain my xml as an implementation of org.w3c.dom.Documnet. I need to be able to apply my xpath statement to that object and then remove from the document all matching nodes. I have looked at JDom and XOM and it seems that NIETHER support removing an Element from the Document. That is want I really need to do. Any thoughts? -Jeff

RE: XPath for Document Manipulation - Added by Anonymous over 19 years ago

Legacy ID: #3190611 Legacy Poster: Michael Kay (mhkay)

If what you're given is a DOM Document, then just wrap it in a DOMSource, supply it to the XPath API, and use XPath expressions on it. With JAXP 1.3 this can all be done using standard APIs, there's nothing specific to Saxon about it (except that Saxon supports XPath 2.0 rather than only 1.0). Michael Kay

RE: XPath for Document Manipulation - Added by Anonymous over 19 years ago

Legacy ID: #3190615 Legacy Poster: Jeffrey Lage (jlage)

i was getting objects back from my xpath statement of type net.sf.saxon.tinytree.TinyElmentImpl, which did not support any kind of 'remove' to then take that element out of the document. i have been able to get this running using jaxen and dom4j.

    (1-7/7)

    Please register to reply