Tools XML Inspector and XPath Generator/en
This tool presents an XML document's DOM tree elements. Its most useful function is the XPath input field and XPath generator.
Enter an XPath expression into the field to see which nodes are selected, or vice versa, select an element to get an XPath which finds that element.
In the following, a simple XML document is used as example input:
<DOC> <ITEM> <REVENUE_YEAR>2554-02</REVENUE_YEAR> <REGION>Central</REGION> <COST>100</COST> </ITEM> <ITEM> <REVENUE_YEAR>2552-02</REVENUE_YEAR> <REGION>Central</REGION> </ITEM> <ITEM> <REVENUE_YEAR>2552-03</REVENUE_YEAR> <REGION>Central</REGION> <COST>400</COST> </ITEM> <ITEM> <REVENUE_YEAR>2552-04</REVENUE_YEAR> <REGION>West</REGION> <COST>100</COST> </ITEM> <ITEM> <REVENUE_YEAR>2552-05</REVENUE_YEAR> <REGION>East</REGION> <COST>200</COST> </ITEM> <ITEM> <REVENUE_YEAR>2552-06</REVENUE_YEAR> <REGION>East</REGION> <COST>300</COST> </ITEM> </DOC>
Opening the XML Inspector
As described elsewhere, the inspector can be opened via the pin-value popup menu, programmatically or via an action from the standard library. It is also embedded into a FileBrowser when an XML document file is double clicked or selected inside a zip archive there.
Finally, the regular inspector also contains a DOM tab if the inspected string represents an XML document.
Programmatically, (try it in a workspace or a code editor, by selecting the expression and executing it with doIt):
|text| text := '<DOC> .... </DOC>'. text inspect
or:
'<DOC>...</DOC>' inspect
or in JavaScript:
var text; text = "<DOC> .... </DOC>". text.inspect()
or:
"<DOC>...</DOC>".inspect()

Or select the string only (incl. the quotes) and execute the "InspectIt" menu function,
Trying XPath Expressions
Here are a few examples on how to use XPath:
Selecting and Processing Nodes
To get all COST elements (as a set of nodes), use:
//COST
to get their contents (as strings):
//COST[text()]
to get all Nodes for a particular REGION:
//REGION[text() = "East"]
to get the COST elements for a particular REGION
//REGION[text() = "East"]/following-sibling::COST
and those values:
//REGION[text() = "East"]/following-sibling::COST[text()]
the maximum of them:
max(//REGION[text() = "East"]/following-sibling::COST[text()])
the sum of them:
sum(//REGION[text()]/following-sibling::COST[text()])
Programmatically
Using an elementary Smalltalk action, the above operations can be implemented with:
|xml xpath nodes|
xml := '<DOC>...</DOC>'. xpath := XML:XPathParser parse:'//REGION[text() = "East"]/following-sibling::COST[]'. nodes := xpath nodesMatchingInDocument:xml. nodes do:[:eachNode | "/ ... do something with eachNode ... "/ in this case, this will be each COST element; "/ extract its text, make it a number and show it Transcript showCR: (eachNode characterData asNumber). => prints 200 and 300 on the Transcript ].
or sum those values:
nodes sum:[:eachNode | eachNode characterData asNumber] => prints 500 on the Transcript
Back to Online Documentation.
Back to Tools.