Problem in Java extension module

Friday, 18 April 2025 · 3 messages · last reply 24 Apr 2025
NR
Nico Verwer (Rakensi)18 Apr 2025 11:18 #
Greetings, BaseX creators and users! I am trying to make a custom XQuery function in Java. This works fine in most cases, but it seems to lose namespaces on attributes. The Java code in the function converts the BaseX representation of an element into a org.w3c.dom.Element DOM; transforms the DOM, creating a new DOM; sends the resulting DOM element back to BaseX. The problem is quite subtle and hard to explain in a few sentences. Therefore I have made a project demonstrating it, which can be seen on [Github](https://github.com/nverwer/possible-bug-in-BaseX). The project includes Java code and a simple XQuery that demonstrates the problem. I am at the point where I think the problem could be a bug in BaseX, which is why I thought it would be a good idea to post this to the basex-talk mailing list. If someone with more knowledge of the inner workings of BaseX could help me, I would much appreciate. Best regards, Nico Verwer
CG
Christian Grün23 Apr 2025 18:04 #
Hi Nico, It’s time constraints that keep me from taking a look. Let us know if you manage to strip it down to a minimized self-contained example. Pull requests are welcome, too ;·) Best, Christian
Quoted text · Nico Verwer (Rakensi) · 24 lines
On Fri, Apr 18, 2025 at 11:19 AM Nico Verwer (Rakensi) <nverwer at rakensi.com> wrote: > Greetings, BaseX creators and users! > > I am trying to make a custom XQuery function in Java. This works fine in > most cases, but it seems to lose namespaces on attributes. > The Java code in the function converts the BaseX representation of an > element into a `org.w3c.dom.Element` DOM; transforms the DOM, creating a > new DOM; sends the resulting DOM element back to BaseX. > > The problem is quite subtle and hard to explain in a few sentences. > Therefore I have made a project demonstrating it, which can be seen on > [Github](https://github.com/nverwer/possible-bug-in-BaseX). The project > includes Java code and a simple XQuery that demonstrates the problem. > > I am at the point where I think the problem could be a bug in BaseX, > which is why I thought it would be a good idea to post this to the > basex-talk mailing list. > If someone with more knowledge of the inner workings of BaseX could help > me, I would much appreciate. > > Best regards, > Nico Verwer >
N
nverwer24 Apr 2025 14:25 #
Hello Christian, Thank you for your response. Because of it, I made a more simplified example. Then I discussed it with a colleague, who asked some questions that lead me to a solution. The simplified example is:
  @Requires(Permission.NONE)
  @Deterministic
  @ContextDependent
  public Value element() throws QueryException, ParserConfigurationException
  {
    // Make a DOM document.
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document domDocument = builder.newDocument();
    // Construct an element <test xmlns:test="http://test" test:attr="test"/>.
    Element outputElement = domDocument.createElement("test");
    outputElement.setAttributeNS("http://test", "test:attr", "test");
    // The namespace declaration must be set as a (pseudo-)attribute.
    outputElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:test", "http://test");
    // Returning `outputElement`, or converting it to BaseX first, loses the attribute namespace URI.
    //return JavaCall.toValue(inputElement, queryContext, null);
    // Make the new element the root element of the document. This is needed to get attribute namespaces.
    domDocument.appendChild(outputElement);
    // Converting the root element to BaseX does not work:
    //Value bxResult = JavaCall.toValue(domDocument.getDocumentElement(), queryContext, null);
    // Converting the document to BaseX and taking the root element works:
    ANode bxResult = (ANode)JavaCall.toValue(domDocument, queryContext, null);
    // Return the root element.
    return bxResult.childIter().next();
  }
As stated in the comment, returning a DOM document (or converting it to a BaseX node first) will lose the attribute namespace URI. My colleague asked if the namespace was present in the document node, and from there I developed the above code, which retains the namespace URI. The comments show the steps I took and what does not work. Best regards, Nico

⚡Generated with XQuery