Skip to content

[7.x.x] Improve spec compliance of fn:parse-xml-fragment #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop-7.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions exist-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@
<include>src/test/resources/standalone-webapp/WEB-INF/web.xml</include>
<include>src/test/xquery/maps/maps.xqm</include>
<include>src/test/xquery/util/util.xml</include>
<include>src/test/xquery/xquery3/parse-xml.xqm</include>
<include>src/test/xquery/xquery3/serialize.xql</include>
<include>src/main/java/org/exist/Indexer.java</include>
<include>src/main/resources-filtered/org/exist/system.properties</include>
Expand Down Expand Up @@ -1007,6 +1008,7 @@
<exclude>src/test/xquery/xqsuite/xqsuite-assertions-inline.xqm</exclude>
<exclude>src/test/xquery/xqsuite/xqsuite-assertions.resources.xqm.ignore</exclude>
<exclude>src/test/xquery/xquery3/function-reference.xqm</exclude>
<exclude>src/test/xquery/xquery3/parse-xml.xqm</exclude>
<exclude>src/test/xquery/xquery3/postfix-expr.xqm</exclude>
<exclude>src/test/xquery/xquery3/serialize.xql</exclude>
<exclude>src/main/java/org/exist/Indexer.java</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import static org.exist.util.ByteOrderMark.stripXmlBom;

Expand Down Expand Up @@ -104,10 +103,6 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
return Sequence.EMPTY_SEQUENCE;
}
final String xmlContent = args[0].itemAt(0).getStringValue();
if (xmlContent.isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}

return parse(xmlContent, args);
}

Expand All @@ -134,7 +129,27 @@ private ValidationReport validate(String xmlContent, final SAXAdapter saxAdapter
xmlContent = stripXmlBom(xmlContent);
final String xml;
if (isCalledAs("parse-xml-fragment")) {
xml = "<" + FRAGMENT_WRAPPER_NAME + ">" + xmlContent + "</" + FRAGMENT_WRAPPER_NAME + ">";
String declStr = xmlContent.toLowerCase();
final int startIdx = declStr.indexOf("<?xml ");
if (startIdx > -1) {

// NOTE(AR) for parsing fragments the input must be an external entity, so validate that the declaration is a TextDecl (https://www.w3.org/TR/REC-xml/#NT-TextDecl) and not a full XMLDecl (https://www.w3.org/TR/REC-xml/#NT-XMLDecl) with standalone attribute

declStr = declStr.substring(startIdx);
int endIdx = declStr.indexOf("?>");
if (endIdx > -1) {
endIdx += 2;
}
declStr = declStr.substring(0, endIdx);
if (declStr.contains("standalone=")) {
throw new XPathException(this, ErrorCodes.FODC0006, "Input to fn:parse-xml-fragment must be a valid external entity, but 'standalone' attribute was detected in the declaration");
}

xml = xmlContent;

} else {
xml = "<" + FRAGMENT_WRAPPER_NAME + ">" + xmlContent + "</" + FRAGMENT_WRAPPER_NAME + ">";
}
} else {
xml = xmlContent;
}
Expand Down
40 changes: 0 additions & 40 deletions exist-core/src/test/xquery/xquery3/parse-xml.xq

This file was deleted.

147 changes: 147 additions & 0 deletions exist-core/src/test/xquery/xquery3/parse-xml.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
(:
: Elemental
: Copyright (C) 2024, Evolved Binary Ltd
:
: [email protected]
: https://www.evolvedbinary.com | https://www.elemental.xyz
:
: This library is free software; you can redistribute it and/or
: modify it under the terms of the GNU Lesser General Public
: License as published by the Free Software Foundation; version 2.1.
:
: This library is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
: Lesser General Public License for more details.
:
: You should have received a copy of the GNU Lesser General Public
: License along with this library; if not, write to the Free Software
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
:
: NOTE: Parts of this file contain code from 'The eXist-db Authors'.
: The original license header is included below.
:
: =====================================================================
:
: eXist-db Open Source Native XML Database
: Copyright (C) 2001 The eXist-db Authors
:
: [email protected]
: http://www.exist-db.org
:
: This library is free software; you can redistribute it and/or
: modify it under the terms of the GNU Lesser General Public
: License as published by the Free Software Foundation; either
: version 2.1 of the License, or (at your option) any later version.
:
: This library is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
: Lesser General Public License for more details.
:
: You should have received a copy of the GNU Lesser General Public
: License along with this library; if not, write to the Free Software
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
:)
xquery version "3.0";

(:~ Additional tests for the fn:parse-xml and fn:parse-xml-fragment functions :)
module namespace px="http://exist-db.org/xquery/test/parse-xml";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
%test:assertEmpty
function px:fragment-type-1() {
fn:parse-xml-fragment(())
};

declare
%test:assertTrue
function px:fragment-type-2() {
fn:parse-xml-fragment("") instance of document-node()
};

declare
%test:assertEmpty
function px:fragment-children-1() {
fn:parse-xml-fragment("")/node()
};

declare
%test:assertTrue
function px:fragment-type-3() {
fn:parse-xml-fragment(" ") instance of document-node()
};

declare
%test:assertTrue(" ")
function px:fragment-children-2() {
fn:parse-xml-fragment(" ")/node()
};

declare
%test:assertTrue
function px:fragment-type-4() {
fn:parse-xml-fragment("<alpha>abcd</alpha><beta>abcd</beta>") instance of document-node()
};

declare
%test:assertEquals("<alpha>abcd</alpha>", "<beta>abcd</beta>")
function px:fragment-children-3() {
fn:parse-xml-fragment("<alpha>abcd</alpha><beta>abcd</beta>")/node()
};

declare
%test:assertTrue
function px:fragment-type-5() {
fn:parse-xml-fragment("He was <i>so</i> kind") instance of document-node()
};

declare
%test:assertEquals(1)
function px:fragment-count() {
count(parse-xml-fragment("He was <i>so</i> kind"))
};

declare
%test:assertEquals(3)
function px:fragment-node-count() {
count(parse-xml-fragment("He was <i>so</i> kind")/node())
};

declare
%test:assertTrue
function px:fragment-xml-decl() {
fn:parse-xml-fragment('<?xml version="1.0"?><a/>') instance of document-node()
};

declare
%test:assertError("FODC0006")
function px:fragment-xml-decl-standalone-yes() {
fn:parse-xml-fragment('<?xml version="1.0" standalone="yes"?><a/>')
};

declare
%test:assertError("FODC0006")
function px:fragment-xml-decl-standalone-no() {
fn:parse-xml-fragment('<?xml version="1.0" standalone="no"?><a/>')
};

declare
%test:assertTrue
function px:fragment-xml-decl-encoding() {
fn:parse-xml-fragment('<?xml version="1.0" encoding="utf8"?><a/>') instance of document-node()
};

declare
%test:assertError("FODC0006")
function px:fragment-xml-decl-encoding-standalone-yes() {
fn:parse-xml-fragment('<?xml version="1.0" encoding="utf8" standalone="yes"?><a/>')
};

declare
%test:assertError("FODC0006")
function px:fragment-xml-decl-encoding-standalone-no() {
fn:parse-xml-fragment('<?xml version="1.0" encoding="utf8" standalone="no"?><a/>')
};
Loading