Domanda

I'm writing a Java external function that is supposed to be called from an XSLT stylesheet. I intend to return a modified node passed to my function as a parameter. However, I cannot deep copy a node since I'm getting The Saxon DOM cannot be updated. (I think the exception is a bit misleading since I'm not trying to modify a Saxon node). So my question is: what's the best way to utilize external function input nodes in order to return a modified version of it (e.g. adding an additional text node as a child)

Thanks in advance!

È stato utile?

Soluzione

My first reaction would be that manipulating XML node trees is much better done in XSLT than in Java, so calling extension functions in order to do node manipulation seems very strange. In particular, "adding an additional text node as a child" is what XSLT was designed to do, so it seems very odd to call Java to do it.

Any code you do write in Java to manipulate nodes is going to be dependent on the tree model used. From the error message, it sounds as if you're probably building the tree initially using Saxon's TinyTree model, and then wrapping the TinyTree nodes in a DOM wrapper for the benefit of your Java code. The DOM wrapper will give you a DOM interface for retrieval/navigation, but not for update, because the TinyTree is immutable.

Altri suggerimenti

Unfortunately, the following code fails:

DOMWriter w = new DOMWriter(); 
w.setNode(programmaticallyCreatedNode); 
tinyTreeNode.copy(w, CopyOptions.ALL_NAMESPACES, 0);

with (I'm using Saxon 9.1.0.8)

java.lang.NullPointerException
at net.sf.saxon.dom.DOMWriter.startElement(DOMWriter.java:103)
at net.sf.saxon.tinytree.TinyElementImpl.copy(TinyElementImpl.java:280)
...

I copied the node with:

DOMWriter w = new DOMWriter();
w.setNode(parentElementProgramaticallyCreated);
Element toCopy = ...;
((ElementOverNodeInfo)toCopy).getUnderlyingNodeInfo().copy(w, NodeInfo.ALL_NAMESPACES, false, 0);

Brief look at the code showed me that the NPE is because of not initialized namePool field in DOMWriter. Maybe I'm doing something completely wrong, but my intuition told me that copying a node should be somewhat simple.

Btw, I'm not tied to using DOM with Saxon, anything that would allow me receive a list of elements, read basic info from them (I was not able to quickly figure out how to get a list of attributes with their values from TinyElementImpl) and construct/copy nodes will serve my purpose. Again, I'm doing some complicated logic with attribute values in Java and depending on the result return a rearranged XML tree. That would be really painful to do it in plain XSLT.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top