Access DOM Node from NodeInfo
Added by Anonymous over 16 years ago
Legacy ID: #5070827 Legacy Poster: David Lee (daldei)
Is there a way to get at the DOM Node from NodeInfo ? Here's my real goal. I'm implementing a "replace" module which will take a set of pairs of xpath/replacement values. I execute the XPath, retrieve the list of affected nodes and replace them with the replacement values (new nodes or text). I have implemented this with the apache XPathAPI module but I'd rather use Saxon. However I cant figure out a way of doing the modify part. With DOM Node I can use getParentNode()/replaceChild() to replace the selected node. I havent found a way to do that using Saxon even if I pass in a DOM tree as its source. The furthest I've gotten is to get ahold of the NodeInfo for the effected node but there is no modification methods on this class and I cant figure out how to get the underling DOM Node ... if in fact there is one. I'd like to use Saxon instead of apache because saxon supports XPath 2, and also because the values I want to swap in came from saxon (XdmValue or XdmItem) and I dont want to go through the hoops of converting those ... somehow ... into a DOM representation which the Node.replaceChild() would accept. A corollary question ... are you planning to implement the XQuery with Updates at some point ? That would seem the cleanest way to do this, but obviously I cant do that now. Maybe dynamically creating an XQuery or XSLT script would best do this rather then just using the XPath API ? Thanks for any suggestions. -David
Replies (1)
RE: Access DOM Node from NodeInfo - Added by Anonymous over 16 years ago
Legacy ID: #5071258 Legacy Poster: Michael Kay (mhkay)
If the NodeInfo was created as a wrapper around a DOM Node, then you can "unwrap" it by casting to net.sf.saxon.dom.NodeWrapper and calling getUnderlyingNode(). With all releases prior to 9.1 this is probably the only feasible approach, because the native tree implementations in Saxon are read-only. But remember all the messy restrictions in DOM about how you can only add a node to a tree if it was created from that tree, and so on. With Saxon 9.1 (released yesterday) you have another option. This release supports XQuery Update over the "linked" tree (also called the standard tree). If your source document is built as a linked tree, then the nodes will implement MutableNodeInfo, which contains a number of updating methods including replace() which replaces this node in the tree with a different node (or sequence of nodes). This would probably be more suitable than using XQuery Update directly, because XQuery Update has delayed effect - it creates a pending update list which is only applied at the end of processing. One caveat is that the methods on MutableNodeInfo were designed largely for use by XQuery Update and may not do all the error checking that should be done if they are called by an external application (or contain all the documentation that should be there for a public interface). Michael Kay Saxonica
Please register to reply