Tinderbox v10 Icon

XML.each(pathStr){action}


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Data Type Returned: 

Operator First Added: 

Operator in Current Baseline: 

Operator Last Altered: 

 Function  [other Function type actions]

 Item  [operators of similar scope]

 Stream parsing  [other Stream parsing operators]

 iterator

 v9.1.0

 Baseline

 As at baseline


XML.each(pathStr){ action(s) }

This operator locates the xml object for each object at the pathStr (within the XML), generating a loop upon it. The .each(pathStr) invokes the action block with the xml item bound in turn to each book element. On completion, it restores the XML object to its previous state.

Consider a note "Source note" with this $Text:

<shelf>
	<book price="9.95">War and Peace</book>
	<book price="4.95">No et Moi</book>
	<audio price="14.95">Born To Run</audio>
</shelf>

Example usage of path:

$MyList("test") = "";
$Text("Source note").xml.each("/shelf/book"){
	$MyList("test2")+=xml["book"]+"\n";
};

which sets $MyList of note "test" to a list of children of the <shelf> element that are <book> elements, [War and Peace;No et Moi].

$MyList("test2") = "";
$Text("Source note").xml.each("/shelf/book"){
	$MyList("test2")+=xml["book[2]"]+"\n";
};

which sets $MyList of note "test" to the value of child element of the <shelf> element that represents the second book, "No et Moi". Other iterated elements return a blank. See 'Notes' below.

$MyList("test2") = "";
$Text("Source note").xml.each("/shelf/book"){
	$MyList("test2")+=xml["book@price"]+"\n";
};

witch sets $MyList of note "test" to a list of the price attribute of each book element, [9.95;4.95].

Notes:

Consider this source XML string stream:

<shelf>
<book price="9.95">War and Peace</book>
<book price="4.95">No et Moi</book>
<audio price="14.95">Born To Run</book>
</shelf>

Examples

Stream.xml.each("/shelf/book") {action} 

returns every 'book' object in the 'shelf' object and iterates over them, i.e. for each (path-matched) item it invokes the action block with the xml item bound in turn to each book element. In the example above this means the action is run 3 times, once for each of the 3 <book> elements under <shelf>.

Inside the action clause, action code can refer to the value of the iterated item as elementName[], book[]. Any attribute of that item can be addressed as elementName[@attribute] book[@price], i.e. here the 'price' attribute of the XML object in focus.

On completion, it restores the xml object to its previous state.


See also—notes linking to here: