Creating a Text NodeInfo
Added by Anonymous almost 16 years ago
Legacy ID: #6042849 Legacy Poster: David Lee (daldei)
In Java, Saxon 9, Is there a way to create a NodeInfo from a String that represents a Text Node ? My use case is I'm using MutableNodeInfo and I want to replace a matched node (either element or text) with text content. The appropreate API I can find is String text = "got the text from somewhere"; MutableNodeInfo node = got the node from somewhere ; node.replace( new NodeInfo[] { text } , true ); But I need to convert the String to a NodeInfo class somehow. I cant use node.replaceStringValue( text ) because I need to replace the entire node itself, not its children. The context of this question is that I'm trying to implement the xproc function "p:replace-string" which requires that I replace the selected node , not its children, with text that was a result of an xpath expression. Any help or clues appreciated ! Thanks. -David
Replies (2)
RE: Creating a Text NodeInfo - Added by Anonymous almost 16 years ago
Legacy ID: #6044590 Legacy Poster: Michael Kay (mhkay)
Try creating an instance of class net.sf.saxon.om.Orphan with nodeKind set to Type.TEXT.
RE: Creating a Text NodeInfo - Added by Anonymous almost 16 years ago
Legacy ID: #6045887 Legacy Poster: David Lee (daldei)
I know this is jumping WAY over the fence of "supported" or even "encouraged" but for reference here is what I did to get this to work. First thanks for the suggestion of Orphan ! It was going to be my favorate saxon class :) But alas, it doesnt work. MutableNodeInfo.replace() throws an execption when it tries to cast NodeInfo to NodeImpl. ( ParentNodeImpl.replaceChildrenAt() NodeImpl child = (NodeImpl)source[i]; ) Then I tried creating a TextImpl directly but no go, its a default access class. Then I tried cheating and using Reflection (Class.forName, getConstructor ... ) and got a runtime exception from the security manager. I should have known better but I had read that trick works sometimes. No go. Then I really cheated. I created my own package package net.sf.saxon.tree and made a utility class in there which did simply return new TextImpl( null , replace); And Voila !! It actually worked !!! Thankfully saxon9.jar isnt "sealed" or I think that would have failed also. Now the fence is really down ... no more private classes hidden from me ;) My 'Last Resort' was I was going to use XQuery to run the expression "text { $value }" but I think that would be way overkill for this, but if my above Super Hack stops working someday I may. -David
Please register to reply