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..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 @@ -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:///intranet/xsl/template.xsl"); + assertEquals("file:///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:///intranet/xsl/template.xsl"); + assertEquals("file:///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);