Skip to content

Commit 0ff8f69

Browse files
committed
[bugfix] fn:parse-xml-fragment#1 should return a document-node() when there is an XML document that has a XML declaration
1 parent 0294331 commit 0ff8f69

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/ParsingFunctions.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464

6565
import java.io.IOException;
6666
import java.io.StringReader;
67-
import java.nio.charset.StandardCharsets;
6867

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

@@ -134,7 +133,27 @@ private ValidationReport validate(String xmlContent, final SAXAdapter saxAdapter
134133
xmlContent = stripXmlBom(xmlContent);
135134
final String xml;
136135
if (isCalledAs("parse-xml-fragment")) {
137-
xml = "<" + FRAGMENT_WRAPPER_NAME + ">" + xmlContent + "</" + FRAGMENT_WRAPPER_NAME + ">";
136+
String declStr = xmlContent.toLowerCase();
137+
final int startIdx = declStr.indexOf("<?xml ");
138+
if (startIdx > -1) {
139+
140+
// 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
141+
142+
declStr = declStr.substring(startIdx);
143+
int endIdx = declStr.indexOf("?>");
144+
if (endIdx > -1) {
145+
endIdx += 2;
146+
}
147+
declStr = declStr.substring(0, endIdx);
148+
if (declStr.contains("standalone=")) {
149+
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");
150+
}
151+
152+
xml = xmlContent;
153+
154+
} else {
155+
xml = "<" + FRAGMENT_WRAPPER_NAME + ">" + xmlContent + "</" + FRAGMENT_WRAPPER_NAME + ">";
156+
}
138157
} else {
139158
xml = xmlContent;
140159
}

0 commit comments

Comments
 (0)