XMLTools
ToRecord
format an XML tree as nested records
Calling Sequence
Parameters
Description
Examples
Compatibility
ToRecord(xmlTree, markcdata, decodeentities)
xmlTree
-
Maple XML tree; XML element
markcdata
(optional) truefalse; separate _XML_CDATA
decodeentities
(optional) truefalse; decode entities
The ToRecord(xmlTree) command formats the XML element xmlTree as nested records instead of nested functions. Nested records use the name of the entities as members. The record members are assigned to the entity value, which could be another record. When the same entity name occurs multiple times at the same level, the values are contained in a list.
The special names _order, _value, _XML_CDATA, and _attributes occur in many situations.
_order is a list of strings that correspond to the member names in the order they occurred in the XML structure. For example, "<a>1</a><b>2</b><a>3</a>" maps to a record with exports a, b, and _order where a is a list, ["1","2"] and _order is a list, ["a","b","a"].
_value is the name that is used to hold the unnamed part of an element with mixed substructures. For example, "<a>1<b>2</b></a>" will have the member a:-_value assigned to "1" in addition to a:-b assigned to "2".
_XML_CDATA is used as one more level of indirection to indicate CDATA values. If you use the optional parameter 'markcdata' = false, the CDATA wrapper is removed and it appears as a regular text value. When converting back to XML string, the _XML_CDATA needs to be put back, or the "<![CDATA[ ]]>" wrapper applied directly to the string.
_attributes is used to store the attributes of the entity.
When 'decodeentities' = true the StringTools:-DecodeEntities command is used to convert each entity to UTF-8 encoding. Otherwise the entity is inlined as a string of the form "&#...;". The default is decodeentities = false.
A record-based XML structure is especially useful when building and modifying using procedural methods. Records are mutable data structures so entries can be changed deep inside a recursive program causing the entire tree to be affected by the change.
with⁡XMLTools:
This example shows how repeated elements are put into a list, and the order they occurred can be deduced from the _order export.
xml≔ParseString⁡<doc><a>1</a><b>2</b><a>3</a></doc>:
r≔ToRecord⁡xml:
Print⁡r
<doc> <a>1</a> <b>2</b> <a>3</a></doc>
r:-doc:-a1
1
r:-doc:-b
2
r:-doc:-a2
3
r:-doc:-_order
a,b,a
This example shows an instance where the _value export is introduced.
r≔ToRecord⁡ParseString⁡<doc><a>1</a>2</doc>:
exports⁡r:-doc
a,_value,_order
r:-doc:-_value
This example also highlights the _value export, and also shows how eval() is needed to look at members that are assigned to a record.
r≔ToRecord⁡ParseString⁡<a>1<b>2</b></a>:
eval⁡r:-a
Record⁡_value=1,b=2,_order=_value,b
r:-a:-_value
r:-a:-b
This example shows a structure containing CDATA.
r≔ToRecord⁡ParseString⁡<a><![CDATA[the data]]></a>:
Record⁡_XML_CDATA=the data,_order=_XML_CDATA
r:-a:-_XML_CDATA
the data
r≔ToRecord⁡ParseString⁡<a><![CDATA[the data]]></a>,markcdata=false:
This example shows a structure containing attributes.
r≔ToRecord⁡ParseString⁡<a color=\"red\" size=\"10\"/>:
eval⁡r:-a:-_attributes
Record⁡color=red,size=10
r:-a:-_attributes:-color
red
r:-a:-_attributes:-size
10
This example shows a procedure that walks the entries in order
disp := proc( val, lev := 0 ) local count, i, namei, elemi, v; if val::list then seq( procname(i,lev+1), i=val ); elif val::record and member(_order,val) then count := table(sparse); for i from 1 to numelems(val:-_order) do namei := val:-_order[i]; printf("%*s%s:\n",2*lev,"",namei); elemi := val[convert(namei,name)]; count[namei] := count[namei]+1; v := `if`(elemi::list,elemi[count[namei]],elemi); if namei = "_value" and v::string then printf("%*s%s\n",2*(lev+1),"",v); elif namei = "_order" then #skip elif namei = "_XML_CDATA" and v::string then printf("%*s<![CDATA[%s]]>\n",2*(lev+1),"",v); else procname(v,lev+1); fi; od; else printf("%*s%a\n",2*lev,"",eval(val)); fi; end:
r≔ToRecord⁡ParseString⁡<doc id=\"17\"><a>1</a><b>2</b><c>18<![CDATA[data]]></c><a>3</a></doc>
r≔Record⁡doc=Record⁡...,_order=doc
disp⁡r
doc: a: "1" b: "2" c: _value: 18 _value: data a: "3" _attributes: Record('id' = "17")
The XMLTools[ToRecord] command was introduced in Maple 2018.
For more information on Maple 2018 changes, see Updates in Maple 2018.
See Also
DocumentTools[Tabulate]
XMLTools[ParseString]
Download Help Document