Project

Profile

Help

Importing a Saxon node into a DOM Document

Added by Anonymous about 19 years ago

Legacy ID: #3035085 Legacy Poster: Eric P. Wittmann (paalin)

Here's the situation. From an XQuery of the form: <dataout xmlns:exp="urn:/my/url/path"> <exp:dataA>1</exp:dataA> <exp:dataB>1.0</exp:dataB> </dataout> I get back a Saxon TinyElementImpl object. From that, I can create a DOM Element by calling NodeOverNodeInfo.wrap(). I need to import the node returned by Saxon into a larger DOM document. I have tried two things. The first is a simple importNode on the DOM document, giving the NodeOverNodeImpl wrapper as the arg. This gives me a NullPointerException because (in my case) the Xerces importNode impl tries to query the ownerDocument (which is null for this NodeOverNodeImpl). The second thing I tried was to serialize the NodeOverNodeImpl to a String and then reparse it into a DOM. However, when I do this the xmlns:exp attribute does not get serialized, so I'm left with: <dataout> <exp:dataA>1</exp:dataA> <exp:dataB>1.0</exp:dataB> </dataout> ...which of course fails to parse because the 'exp' prefix is not declared. The code for serialization is something like: Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes" ); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.INDENT, "yes" ); //$NON-NLS-1$ //$NON-NLS-2$ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); //$NON-NLS-1$ //$NON-NLS-2$ transformer.transform(new DOMSource(aNode), new StreamResult(writer)); Any thoughts on how this should be done?


Replies (3)

Please register to reply

RE: Importing a Saxon node into a DOM Documen - Added by Anonymous about 19 years ago

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

Generally speaking one implementation of the DOM isn't going to allow you to attach nodes that were created by a different implementation, so wrapping the NodeInfo (TinyElementImpl) in a Saxon DOM wrapper isn't going to do any good. The cleanest way to achieve this is to use the run() method to run the query, specifying for the result argument a DOMResult, on which you have done setNode() specifying the Xerces Element to which you want the results attached. Saxon will then write the query results directly to the Xerces DOM. (Let's hope it gets the namespaces right!) Michael Kay

RE: Importing a Saxon node into a DOM Documen - Added by Anonymous about 19 years ago

Legacy ID: #3041128 Legacy Poster: Eric P. Wittmann (paalin)

Excellent, thanks for the tip. Using run() worked like a charm.

RE: Importing a Saxon node into a DOM Documen - Added by Anonymous almost 18 years ago

Legacy ID: #3745040 Legacy Poster: Brian Dueck (bdueck)

Michael; As a follow-up to this old thread, I have a similar (but not quite identical) problem where calling run() doesn't seem to help. In my case, I need to use the evaluate() method because I'm not sure when I run the xquery what kind of results it will return (could be a java primitive, could be a java collection, could be a NodeInfo etc.). In the case where one of the elements in the list returned by evaluate() is a NodeInfo, I want to be able to serialize that NodeInfo to a string with all the namespace declrations intact. However, when I use NodeOverNodeInfo.wrap() on a NodeInfo returned by evaluate(), and then run normal XML serialization on it, there is no namespace declarations to be found. Any ideas on what I can do here? Thanks in advance, Brian Dueck

    (1-3/3)

    Please register to reply