From 9a313b7945759218aafa38e4b437cc6020d6fd3b Mon Sep 17 00:00:00 2001 From: Laurettta Date: Fri, 1 May 2026 10:03:48 +0100 Subject: [PATCH 1/2] [bugfix] base-uri() was returning empty string instead of the document path Closes https://github.com/evolvedbinary/elemental/issues/186 The xml:base lookup in getBaseURI() was returning empty string when the attribute didn't exist. Removed it so we fall through to getDocumentURI() which gives the correct result. --- .../org/exist/dom/memtree/DocumentImpl.java | 22 +++++--------- .../exist/dom/memtree/DocumentImplTest.java | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/exist-core/src/main/java/org/exist/dom/memtree/DocumentImpl.java b/exist-core/src/main/java/org/exist/dom/memtree/DocumentImpl.java index 9270b1652f..85553c1899 100644 --- a/exist-core/src/main/java/org/exist/dom/memtree/DocumentImpl.java +++ b/exist-core/src/main/java/org/exist/dom/memtree/DocumentImpl.java @@ -1658,26 +1658,18 @@ public XQueryContext getContext() { @Override public String getBaseURI() { - final Element el = getDocumentElement(); - if(el != null) { - final String baseURI = getDocumentElement().getAttributeNS(Namespaces.XML_NS, "base"); - if(baseURI != null) { - return baseURI; - } - } final String docURI = getDocumentURI(); if(docURI != null) { return docURI; - } else { - if(context!=null && context.isBaseURIDeclared()) { - try { - return context.getBaseURI().getStringValue(); - } catch(final XPathException e) { - //TODO : make something ! - } + } + if(context != null && context.isBaseURIDeclared()) { + try { + return context.getBaseURI().getStringValue(); + } catch(final XPathException e) { + //TODO : make something ! } - return XmldbURI.EMPTY_URI.toString(); } + return XmldbURI.EMPTY_URI.toString(); } @Override diff --git a/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java b/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java index 7fdb1113b9..e2dd89010e 100644 --- a/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java +++ b/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java @@ -206,6 +206,36 @@ public void testGetInScopePrefix() throws IOException, ParserConfigurationExcept assertEquals(Namespaces.XHTML_NS, namespaceUri); } + @Test + public void getBaseURI_withDocumentURI() throws IOException, SAXException, ParserConfigurationException { + final DocumentImpl doc; + try(final InputStream is = new UnsynchronizedByteArrayInputStream("".getBytes(UTF_8))) { + doc = parseExist(is); + } + doc.setDocumentURI("file:///C:/intranet/xsl/template.xsl"); + assertEquals("file:///C:/intranet/xsl/template.xsl", doc.getBaseURI()); + } + + @Test + public void getBaseURI_ignoresXmlBaseOnRoot() throws IOException, SAXException, ParserConfigurationException { + final DocumentImpl doc; + try(final InputStream is = new UnsynchronizedByteArrayInputStream( + "".getBytes(UTF_8))) { + doc = parseExist(is); + } + doc.setDocumentURI("file:///C:/intranet/xsl/template.xsl"); + assertEquals("file:///C:/intranet/xsl/template.xsl", doc.getBaseURI()); + } + + @Test + public void getBaseURI_noDocumentURI() throws IOException, SAXException, ParserConfigurationException { + final DocumentImpl doc; + try(final InputStream is = new UnsynchronizedByteArrayInputStream("".getBytes(UTF_8))) { + doc = parseExist(is); + } + assertEquals("", doc.getBaseURI()); + } + private Document parseXerces(final InputStream is) throws ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); From dd6bd425099af236bab4e096aae4d0f186e12572 Mon Sep 17 00:00:00 2001 From: Laurettta Date: Fri, 1 May 2026 12:47:28 +0100 Subject: [PATCH 2/2] [test] Make getBaseURI tests cross-platform --- .../test/java/org/exist/dom/memtree/DocumentImplTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java b/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java index e2dd89010e..f53cba862d 100644 --- a/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java +++ b/exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java @@ -212,8 +212,8 @@ public void getBaseURI_withDocumentURI() throws IOException, SAXException, Parse try(final InputStream is = new UnsynchronizedByteArrayInputStream("".getBytes(UTF_8))) { doc = parseExist(is); } - doc.setDocumentURI("file:///C:/intranet/xsl/template.xsl"); - assertEquals("file:///C:/intranet/xsl/template.xsl", doc.getBaseURI()); + doc.setDocumentURI("file:///intranet/xsl/template.xsl"); + assertEquals("file:///intranet/xsl/template.xsl", doc.getBaseURI()); } @Test @@ -223,8 +223,8 @@ public void getBaseURI_ignoresXmlBaseOnRoot() throws IOException, SAXException, "".getBytes(UTF_8))) { doc = parseExist(is); } - doc.setDocumentURI("file:///C:/intranet/xsl/template.xsl"); - assertEquals("file:///C:/intranet/xsl/template.xsl", doc.getBaseURI()); + doc.setDocumentURI("file:///intranet/xsl/template.xsl"); + assertEquals("file:///intranet/xsl/template.xsl", doc.getBaseURI()); } @Test