Skip to content

Conversation

nverwer
Copy link

@nverwer nverwer commented Sep 23, 2025

Description

This is a retry for #5680 which was botched after I changed my develop-6.x.x branch.

The fn:transform function sometimes produced NullPointerExceptions. This happened for documents containing nodes of type org.exist.dom.memtree.ElementImpl and other subtypes of org.exist.dom.memtree.NodeImpl.

The function org.exist.xquery.functions.fn.transform.Convert.ToSaxon.ofNode(Node) uses org.exist.xquery.functions.fn.transform.TreeUtils.treeIndex(Node) to get a 'path' (as a list of indexes) of the Node in its containing document. This 'path' is used to find the node in a newly built document that is suitable for use by Saxon.
Using the 'path' that the treeIndex function produces assumes that it starts at the document node.

However, some nodes do not have a containing document. Specifically, in org.exist.dom.memtree.NodeImpl.getParentNode():

if (parent.getNodeType() == DOCUMENT_NODE && !((DocumentImpl) parent).isExplicitlyCreated()) {
            /*
                All nodes in the MemTree will return an Owner document due to how the MemTree is implemented,
                however the explicitlyCreated flag tells us whether there "really" was a Document Node or not.
                See https://github.com/eXist-db/exist/issues/1463
             */
            return null;

In this case, the 'path' returned by treeIndex does not contain the index (0) for the root node, and org.exist.xquery.functions.fn.transform.TreeUtils.xdmNodeAtIndex(XdmNode, List<Integer>) will return null when a node is expected.

The proposed fix tests for this case. Going up the ancestor chain, when a node that is not a document node and that does not have a parent is found, the index 0 for this root element is inserted in the 'path'.

The PR also adds support for using maps and arrays as parameters in a XSLT transformation.

Testing

The NullPointerException happened in a rather complicated XQuery with documents composed from several sources.
I have not yet been able to make a simple test case.

Also adds support for using maps and arrays as parameters in a XSLT transformation.
@dizzzz
Copy link
Member

dizzzz commented Oct 4, 2025

I recognize that under some conditions there is not a "DocumentNode"; I need to recall the context, it is some time ago.

@dizzzz dizzzz requested review from wolfgangmm and a team October 4, 2025 16:10
Copy link
Member

@reinhapa reinhapa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small note...

@duncdrum duncdrum added needs develop port needs XQSuite test XQSuite test required to reproduce labels Oct 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs develop port needs XQSuite test XQSuite test required to reproduce
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants