Tools XML Inspector and XPath Generator/en: Unterschied zwischen den Versionen
Cg (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Cg (Diskussion | Beiträge) K Cg verschob die Seite Tools XML Inspector/en nach Tools XML Inspector and XPath Generator/en |
||
| (8 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 44: | Zeile 44: | ||
inspected string represents an XML document. |
inspected string represents an XML document. |
||
Programmatically |
Programmatically an inspector is opened by sending the "inspect" message to any object - in this case to a string (try it in a workspace or a code editor, by selecting the expression and executing it with ''doIt''): |
||
|text| |
|text| |
||
text := '<DOC> .... </DOC>'. |
text := '<DOC> .... </DOC>'. |
||
| Zeile 57: | Zeile 57: | ||
"<DOC>...</DOC>".inspect() |
"<DOC>...</DOC>".inspect() |
||
[[Datei: |
[[Datei:openXMLInspector1.png|thumbnail|200px|Opening Inspector in Workspace]] |
||
Or select the string only (incl. the quotes) and execute the "''InspectIt''" menu function, |
|||
[[File:XMLInspector1.png|thumbnail|200px|XML Inspector showing the DOM]] |
|||
== Trying XPath Expressions == |
|||
Here are a few examples on how to use XPath on the following XML string: |
|||
<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> |
|||
==== 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 Operating on a NodeSet ==== |
|||
In a real application, you would provide the XML document via an input pin; either as |
|||
a string (and parsed as shown below), or as a filename, or as an already constructed DOM object. |
|||
Notice, that the XML library already provides corresponding action blocks, |
|||
so the following is presented to give you some background details |
|||
(it may also be useful to process the nodeSet programmatically for performance reasons, |
|||
or because a corresponding XPath function is not available or the XPath expression becomes |
|||
too complex and unmaintanable). |
|||
Using an elementary Smalltalk action, the above operations can be implemented with: |
|||
|xml dom xpath nodes| |
|||
xml := '<DOC>...</DOC>'. |
|||
dom := XML::XMLParser parse:xml. |
|||
xpath := XML:XPathParser parse:'//REGION[text() = "East"]/following-sibling::COST[]'. |
|||
nodes := xpath nodesMatchingInDocument:dom. |
|||
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). |
|||
<small>=> prints 200 and 300 on the Transcript</small> |
|||
]. |
|||
or sum those values: |
|||
nodes sum:[:eachNode | eachNode characterData asNumber] |
|||
<small>=> prints 500 on the Transcript</small> |
|||
==== Programmatically Using XPath Functions ==== |
|||
You can also use XPath functions which operate on a NodeSet. |
|||
In this case, you will not get a NodeSet but a value such as a string or number from |
|||
the XPath evaluation. |
|||
For example: |
|||
... |
|||
xpath := XML:XPathParser parse:'max(//REGION[text() = "East"]/following-sibling::COST[])'. |
|||
maxValue := xpath xPathValueFor:dom. |
|||
<small>=> maxValue will be 300</small> |
|||
or: |
|||
... |
|||
xpath := XML:XPathParser parse:'sum(//REGION[text() = "East"]/following-sibling::COST[])'. |
|||
sum := xpath xPathValueFor:dom. |
|||
<small>=> sum will be 500</small> |
|||
Aktuelle Version vom 8. März 2026, 08:05 Uhr
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 an inspector is opened by sending the "inspect" message to any object - in this case to a string (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 on the following XML string:
<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>
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 Operating on a NodeSet
In a real application, you would provide the XML document via an input pin; either as a string (and parsed as shown below), or as a filename, or as an already constructed DOM object. Notice, that the XML library already provides corresponding action blocks, so the following is presented to give you some background details (it may also be useful to process the nodeSet programmatically for performance reasons, or because a corresponding XPath function is not available or the XPath expression becomes too complex and unmaintanable).
Using an elementary Smalltalk action, the above operations can be implemented with:
|xml dom xpath nodes|
xml := '<DOC>...</DOC>'. dom := XML::XMLParser parse:xml. xpath := XML:XPathParser parse:'//REGION[text() = "East"]/following-sibling::COST[]'. nodes := xpath nodesMatchingInDocument:dom. 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
Programmatically Using XPath Functions
You can also use XPath functions which operate on a NodeSet. In this case, you will not get a NodeSet but a value such as a string or number from the XPath evaluation. For example:
... xpath := XML:XPathParser parse:'max(//REGION[text() = "East"]/following-sibling::COST[])'. maxValue := xpath xPathValueFor:dom. => maxValue will be 300
or:
... xpath := XML:XPathParser parse:'sum(//REGION[text() = "East"]/following-sibling::COST[])'. sum := xpath xPathValueFor:dom. => sum will be 500
Back to Online Documentation.
Back to Tools.