polserver/testsuite/escript/filemod/xmlnode.src
2026-08-11 20:39:36 +02:00

94 lines
3.9 KiB
Text

use file;
// Navigating an XMLFile: subscripting, iteration and clonenode.
// filemod/xmltest.xml holds a declaration and a root element with the children
// Hello (with the text World), a comment and the empty element nodetwo.
program XMLNodeTest()
var xml := OpenXMLFile( "filemod/xmltest.xml" );
print( "--- subscripting the file" );
print( "index 1: " + xml[1] + " [" + xml[1].type + "]" );
print( "index 2: " + xml[2] + " [" + xml[2].type + "]" );
print( "by name: " + xml["root"] );
var root := xml[2];
print( "--- subscripting a node" );
print( "index 1: " + root[1] + " [" + root[1].type + "]" );
print( "index 2: " + root[2] + " [" + root[2].type + "]" );
print( "index 3: " + root[3] + " [" + root[3].type + "]" );
print( "by name: " + root["nodetwo"] );
print( "nested: " + root["Hello"][1] );
print( "--- children and siblings" );
print( "first: " + root.firstxmlchild() );
print( "named: " + root.firstxmlchild( "nodetwo" ) );
print( "sibling: " + root.firstxmlchild().nextxmlsibling() + " [" + root.firstxmlchild().nextxmlsibling().type
+ "]" );
print( "named sibl.: " + root.firstxmlchild().nextxmlsibling( "nodetwo" ) );
print( "text child: " + root.firstxmlchild().firstxmlchild() );
print( "--- a node tinyxml cannot classify" );
// filemod/xmlunknown.xml carries a doctype, which is neither an element nor a declaration
var unknown := OpenXMLFile( "filemod/xmlunknown.xml" );
print( "index 2: " + unknown[2] + " [" + unknown[2].type + "]" );
print( "--- iterating a node" );
// iteration returns the first child and then only the following *elements*, the comment
// between Hello and nodetwo is skipped although nextxmlsibling() reaches it
foreach child in root
print( " " + _child_iter + ": " + child + " [" + child.type + "]" );
endforeach
print( "--- iterating a text node" );
var count := 0;
var textnode := root.firstxmlchild().firstxmlchild();
foreach child in textnode
count := count + 1;
endforeach
print( "children: " + count );
print( "--- attributes" );
var nodetwo := root["nodetwo"];
print( "names: " + nodetwo.attributes.propnames() );
print( "i: " + nodetwo.attributes["i"] );
print( "j: " + nodetwo.attributes["j"] );
foreach attribute in ( nodetwo.attributes )
print( " " + _attribute_iter + ": " + attribute );
endforeach
print( "--- clonenode" );
var clone := cloned();
print( "clone: " + clone + " [" + clone.type + "]" );
print( "children: " + clone.firstxmlchild() );
print( "attributes: " + clone["nodetwo"].attributes.propnames() );
// the clone is detached, changing it does not change the file
print( "append: " + clone.appendxmlnode( "added" ) );
print( "in clone: " + clone["added"] );
print( "in file: " + OpenXMLFile( "filemod/xmltest.xml" )[2]["added"] );
print( "--- nodes outliving their file" );
// the XMLFile is only a temporary here, a node keeps the document alive on its own
var orphan := detached();
print( "node: " + orphan + " [" + orphan.type + "]" );
print( "subscript: " + orphan["Hello"][1] );
print( "child: " + orphan.firstxmlchild() );
print( "attributes: " + orphan["nodetwo"].attributes.propnames() );
foreach child in orphan
print( " " + _child_iter + ": " + child );
endforeach
foreach attribute in ( OpenXMLFile( "filemod/xmltest.xml" )[2]["nodetwo"].attributes )
print( " " + _attribute_iter + ": " + attribute );
endforeach
endprogram
// returns the root element, the XMLFile object it came from is gone by then
function detached()
return OpenXMLFile( "filemod/xmltest.xml" )[2];
endfunction
// returns a clone of the root element, the XMLFile object it came from is gone by then
function cloned()
var xml := OpenXMLFile( "filemod/xmltest.xml" );
return xml[2].clonenode();
endfunction