asTree
- Function, special function message / property of objects
-
The tree() or asTree() function can also be called using the special syntax as {a} tree.
-
This can be used to convert a value into a tree data structure. XML data can be loaded as a tree from a file or URL with a single statement:
put url "<a href="http://some.address/data.xml" class="l0">http://some.address/data.xml</a>" as a tree into myTree
-
When tree() or as a tree is called with a parameter that is a property list that has an asTree property, the value of that property is used. If the property list / object has an asTreeExpression property, the value of that property is evaluated as an expression (equivalent to calling the treeFromXML() function) to obtain the tree value. If the object has neither of these properties, an asTree function message is sent exclusively to the object and its helpers to obtain the tree value.
-
If the object doesn't supply a tree representation of itself in any of the above ways, it is taken to be a direct property list representation of a tree structure or a node. The property list may include these properties (and values): _tag or _element (tag name of an element); _attributes (attributes of an element node); _children (list of child nodes); _text (contents of a text node); _comment or "--" (contents of a comment node); _processingInstruction or _pi (contents of a processing instruction node); "?" followed by processing instruction name (body of a processing instruction node); _XMLinfo (property list of special XML document attributes).
put { _tag:book, _children:"The Rose" } as tree --> <book>The Rose</book>
-
A simplified format can also be used:
put {book:"The Rose"} as tree --> <book>The Rose</book>
-
A simple element node with attributes but no content:
put {_tag:"pg", _attributes:{id:43}} as tree --> <pg id="43"></pg>
-
A simplified format can also be used in this case:
put {_tag:"pg", id:43} as tree --> <pg id="43"></pg>
-
If the target is not an object (and is not already a tree), the target's text string value is evaluated as XML to obtain the tree value.
-
Related: tree, treeFromXML
-