XML document, element, attribute, parser-result, and serialization helpers.
Core schemas
XMLDocument: document record with fieldroot. The XML declaration is not stored.XMLAttribute: attribute record with fieldsnameandvalue.XMLElement: element record with fieldsname,attributes, andchildren.XMLVALUE_ELEMENTandXMLVALUE_CHARDATA: tags forXMLValue.XMLValue: tagged child value that stores one child element or one character-data string.XMLParserResult: parse result with fieldssuccess,finished,errorInfo, andxml.XMLParserPosition: parser position withcolumn,line,offset,currentSymbol, andcurrentCodepoint.XMLParserErrorInfo: error record withmessageandposition.
XMLParserResult
success:TRUEwhen lexical and structural parsing succeed andFALSEon invalid UTF-8 input or XML failure.finished:TRUEwhen the whole source is consumed after one successful document parse andFALSEwhen trailing input remains.errorInfo.message: parser failure description whensuccessisFALSE.errorInfo.position: parser position at failure, or the first unparsed position whenfinishedisFALSE.xml: parsedXMLDocumentwhensuccessisTRUE. WhenfinishedisFALSE, it still contains the first parsed document.- On invalid UTF-8 input, only
errorInfo.position.offsetis meaningful; line and column are not derived.
Parser model
parseElementFromOpenTag: lower-level helper that parses one element after the opening<has already been consumed and the current parser position points to the first name character.parseStringToXMLImpl: lower-level helper underlyingparseStringToXML. It parses one document from one already split UTF-8 source and one current parser position into supplied result objects.- Attributes are stored in parse order, duplicate attribute names make parsing fail, and whitespace-only character data is discarded.
- The parser accepts one document root element, attributes, comments, character data, and one optional XML declaration.
- Comments are skipped and are not stored. The XML declaration is parsed when present but is not stored in
XMLDocument. - Named references handled by the module are
&,>,<, and". ', numeric character references, document type declarations, CDATA sections, and processing instructions in element content are unsupported.- Known source text can be parsed during compilation, and compile-time code can inspect root names, attribute counts, and child counts of the resulting tree.
parseStringToXML (source -- result)
Parses one accepted UTF-8 source into XMLParserResult.
- Accepted source forms are
StringView,Text, andString. - The source is normalized through
String.makeStringViewbefore UTF-8 splitting begins. - The result fields are described in
XMLParserResult.
saveXMLToString (xml -- string)
Serializes one XMLDocument to a formatted String.
- Element names and attributes are written in stored order.
- Child elements are indented by two spaces per depth level.
- Character data is emitted inline inside the parent element content.
- The serializer emits the stored
XMLElementtree only; the XML declaration and comments are not re-emitted. - The serializer does not escape character data or attribute values, so stored names and data must already be suitable for direct emission.
Examples
Example: successful parse and stored counts
Parsing <root a="b"><child/></root> succeeds completely and stores one attribute and one child, all known during compilation.
"Xml" use
"control" use
{} () {} [
result: "<root a=\"b\"><child/></root>" parseStringToXML;
@result.@success TRUE same printStack _:;
@result.@finished TRUE same printStack _:;
@result.@xml.@root.@attributes.size 1 same printStack _:;
@result.@xml.@root.children.size 1 same printStack _:;
] "main" exportFunction
Expected Output During Compilation
TRUE
TRUE
TRUE
TRUE
Example: trailing input and rejected or discarded content
Trailing text leaves finished FALSE, a duplicate attribute and an apostrophe in element content make the parse fail, and whitespace-only content adds no child.
"Xml" use
"control" use
{} () {} [
trailing: "<root/>x" parseStringToXML;
@trailing.@success TRUE same printStack _:;
@trailing.@finished FALSE same printStack _:;
duplicate: "<root a=\"1\" a=\"2\"/>" parseStringToXML;
@duplicate.@success FALSE same printStack _:;
spaces: "<root> </root>" parseStringToXML;
@spaces.@xml.@root.children.size 0 same printStack _:;
apos: "<root>'</root>" parseStringToXML;
@apos.@success FALSE same printStack _:;
] "main" exportFunction
Expected Output During Compilation
TRUE
TRUE
TRUE
TRUE
TRUE
Example: parse results at run time
At run time, the same document parses completely with one attribute and one child.
"String" use
"Xml" use
"control" use
{} Int32 {} [
result: "<root a=\"b\"><child/></root>" parseStringToXML;
("success=" result.success LF
"finished=" result.finished LF
"attrs=" result.xml.root.attributes.size LF
"children=" result.xml.root.children.size LF) printList
0
] "main" exportFunction
Expected Output
success=TRUE
finished=TRUE
attrs=1
children=1