This version is out of date, covering development from v9.5.0 to v9.7.3. It is maintained here only for inbound reference links from elsewhere. It is no longer actively updated.

Jump to the current version of aTbRef

Tinderbox v9 Icon

XML.each(pathStr){action}


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Last Altered: 

 Function  [other Function type actions]

 Item  [operators of similar scope]

 Stream parsing  [other Stream parsing operators]

 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.