ToRecord - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


XMLTools

  

ToRecord

  

format an XML tree as nested records

 

Calling Sequence

Parameters

Description

Examples

Compatibility

Calling Sequence

ToRecord(xmlTree, markcdata, decodeentities)

Parameters

xmlTree

-

Maple XML tree; XML element

markcdata

-

(optional) truefalse; separate _XML_CDATA

decodeentities

-

(optional) truefalse; decode entities

Description

• 

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.

Examples

withXMLTools&colon;

This example shows how repeated elements are put into a list, and the order they occurred can be deduced from the _order export.

xmlParseString<doc><a>1</a><b>2</b><a>3</a></doc>&colon;

rToRecordxml&colon;

Printr


  <doc>
    <a>1</a>
    <b>2</b>
    <a>3</a></doc>

r:-doc:-a1

1

(1)

r:-doc:-b

2

(2)

r:-doc:-a2

3

(3)

r:-doc:-_order

a&comma;b&comma;a

(4)

This example shows an instance where the _value export is introduced.

rToRecordParseString<doc><a>1</a>2</doc>&colon;

exportsr:-doc

a&comma;_value&comma;_order

(5)

r:-doc:-_value

2

(6)

This example also highlights the _value export, and also shows how eval() is needed to look at members that are assigned to a record.

rToRecordParseString<a>1<b>2</b></a>&colon;

evalr:-a

Record_value=1&comma;b=2&comma;_order=_value&comma;b

(7)

r:-a:-_value

1

(8)

r:-a:-b

2

(9)

This example shows a structure containing CDATA.

rToRecordParseString<a><![CDATA[the data]]></a>&colon;

evalr:-a

Record_XML_CDATA=the data&comma;_order=_XML_CDATA

(10)

r:-a:-_XML_CDATA

the data

(11)

rToRecordParseString<a><![CDATA[the data]]></a>&comma;markcdata=false&colon;

evalr:-a

the data

(12)

This example shows a structure containing attributes.

rToRecordParseString<a color=\"red\" size=\"10\"/>&colon;

evalr:-a:-_attributes

Recordcolor=red&comma;size=10

(13)

r:-a:-_attributes:-color

red

(14)

r:-a:-_attributes:-size

10

(15)

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:

rToRecordParseString<doc id=\"17\"><a>1</a><b>2</b><c>18<![CDATA[data]]></c><a>3</a></doc>

rRecorddoc=Record...&comma;_order=doc

(16)

dispr

doc:
  a:
    "1"
  b:
    "2"
  c:
    _value:
      18
    _value:
      data
  a:
    "3"
  _attributes:
    Record('id' = "17")

Compatibility

• 

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

XMLTools[ParseString]