Search code examples
saxons9api

How to change XdmNode base URI in S9API


I have a home grown cache for Saxon's S9API XdmNode instances, implementing get, put, move, and remove operations by URI. The move operation allows moving a XdmNode from one URI to another.

My problem is that the XdmNode internally retains the base URI of the document. How do I update the base URI of the underlaying NodeInfo? When the underlaying node is a DOMNodeWrapper, the base URI can be updated. But for example TinyDocumentImpl is immutable.

Is there a solution that allows updating the base URI of TinyDocumentImpl that doesn't involve building a new document by making a full copy of the tree?


Solution

  • I missed this question when it first appeared because of the lack of a "saxon" tag.

    One option is to use the LinkedTree as your chosen tree model; linked trees are mutable and the DocumentImpl class has a setBaseURI() method. You can choose the tree model using DocumentBuilder.setTreeModel(). (But use this with care; if you change properties of a node in the middle of a transformation, the consequences are unpredictable.)

    Another option is to wrap a TreeInfo in a net.sf.saxon.tree.wrapper.RebasedDocument, which creates a virtual copy of the original node, identical except for having a different base URI. The virtual copy carries almost zero memory cost, but there is a small overhead when navigating the tree.