fn:parse-xml vs fn:parse-xml-fragment in XPath 3.0

XPath 3.0 introduces both fn:parse-xml and fn:parse-xml-fragment to convert string data into XML node trees directly within queries. While both functions parse serialized XML markup into an XDM (XPath and XQuery Data Model) document node, they differ fundamentally in the structural validity they require from the input string, how they handle top-level nodes, and their tolerance for empty or partial XML content.

Input Conformance and Well-Formedness

Top-Level Content Structure

Handling Empty Strings

Declarations and DTDs

Summary of Use Cases

Use fn:parse-xml() when you are processing complete, standalone XML documents where standard XML document rules must be strictly enforced. Use fn:parse-xml-fragment() when dealing with partial XML payloads, template snippets, multiple adjacent sibling elements, or strings that contain unwrapped text mixed with inline markup.