Adding a Document to an orphaned node
Added by Anonymous about 16 years ago
Legacy ID: #5743287 Legacy Poster: David Lee (daldei)
This is an ongoing discussion from the previous topic but the indentation was getting too much :) I experimented today with trying to wrap an 'orphaned' node with a document. These nodes are produced as part of the S9API XQuery interface when no explicit document is given such as "<foo/>". In order to turn these XdmValue objects into a Node interface to import into a DOM tree I need to have them inside documents. This is what I came up with. It seems like its doing a lot more work then 'necessary' ... so I'm asking if maybe i'm missing a simplier solution. Sorry for the bad formatting. This DOES seem to work, I am able to use Document.importNode() on the resulting document and merge it into a writable DOM tree. /* * Wrap a XdmValue as a Node by optionally synthesizing a document * element for it if it doesnt exist already. */ public Node asNodeWithDoc() throws SaxonApiException { NodeInfo nodeinfo = (NodeInfo) mValue.getUnderlyingValue(); if( nodeinfo.getDocumentRoot() == null ){ Processorprocessor = Shell.getProcessor(); DocumentBuilder builder = processor.newDocumentBuilder(); XdmNode xnode = builder.build( ((XdmNode)mValue).asSource() ); NodeInfo docinfo = (NodeInfo) xnode.getUnderlyingNode(); return NodeOverNodeInfo.wrap( docinfo ).getFirstChild() ; } else return NodeOverNodeInfo.wrap( nodeinfo ); }
Please register to reply