XML package guide
Read, change, save and validate XML session documents with XPath.
Version 2.17.1. Installed versions can differ. Examples are illustrative and not tested.
Prerequisites
Install the package on the Device with a compatible Mercury. It works on local XML data and needs no desktop application or network service. The Run account needs file access to read or save. Relative paths start in the Run working folder. XML and XSD files over 64 MB are refused, and a parsed document over 16,777,216 characters fails. DTDs and external entities are refused. DTD validation is not supported (the choice is listed but fails).
Start session with a name that is not empty, use the same session name in every step, Save session data if you need a file, then End session. Sessions exist only in the Run. Opening the same name again replaces its document. Edits change the document in memory, not the input file: nothing is written until Save session data.
Before each example, open a new session named sample with Data Source=text and this Text:
<bookstore><book id="1"><title>Palatine</title></book><book id="2"><title>Minerva</title></book></bookstore>XPath positions start at 1, unlike JSON array paths. /bookstore/book[2] selects the second book.
For attribute inputs, create Dictionary $attributes$ with String kind=issn or the other key and value in the example. For schema input, create List $schemas$ whose String items are local XSD paths, such as samples/books.xsd. Bind the Dictionary or List itself; a String that holds JSON is a different type.
Shared fields
| Input | Editor field | Type; default | When / required | Use |
|---|---|---|---|---|
session | Session name | Stringdefault "" | Open session required. | Name of the open document in this Run, such as sample; not a credential. Set it on each session step. An unknown or empty name fails. |
x | XPath expression | Stringdefault "" | Required. | XPath 1.0, such as /; positions start at 1. Only Insert node registers prefixes. For a document with namespaces, use local-name() and namespace-uri(), such as /*[local-name()=; names without a prefix match only elements without a namespace. Invalid XPath or an unknown prefix fails. |
attribute | Attribute | Stringdefault "" | - | Blank uses the node itself; a name such as id reads or deletes that attribute. A missing attribute reads as an empty string. |
attributes | Attributes | Dictionarydefault {} | - | Dictionary of Strings, such as $attributes$ with kind = issn. Insert node sets them on the new element; Update node uses them only when updateAttribute=true. |
Actions
Delete node (xmlDeleteNode)
Remove matching nodes or one attribute. Save only a deletion you want.
Inputs
Uses the shared fields: sessionName, xPath, attributeName.
Outputs
No outputs.
Example
Set Session name = sample, XPath expression = /bookstore/book[1], Attribute = id. To remove the second book itself, run again with XPath expression = /bookstore/book[2] and Attribute blank.
Expected result
The first run removes id from the first book. The second removes the second book and everything in it. A missing match or attribute changes nothing.
End session (xmlEndSession)
Close the session. It does not save.
Inputs
Uses the shared fields: sessionName.
Outputs
No outputs.
Example
After you save sample (if needed), set Session name = sample.
Expected result
Later steps on sample fail until Start session opens it again.
Execute XPath function (xmlExecuteXPath)
Run an XPath function or path to count nodes, test values or join selected text.
Inputs
Uses the shared fields: sessionName, xPath.
Outputs
| Output | Type | Meaning |
|---|---|---|
result | String | The function result, or the matched values joined with line breaks. Even count() returns a String. |
Example
Set Session name = sample, XPath expression = count(//book). Bind result to String $countText$. A second call with //title returns title text.
Expected result
The count is String 2, not Number. A true or false expression returns true or false. A path joins node values with line breaks, so //title returns Palatine and Minerva on separate lines. No match gives an empty string.
Get multiple nodes (xmlGetMultipleNodeV2)
Read all XPath matches into a Table with one column. Zero matches gives zero rows.
Inputs
Uses the shared fields: sessionName.
| Input | Editor field | Type; default | When / required | Use |
|---|---|---|---|---|
x | XPath Expression | Stringdefault "" | Required. | As x in shared fields. |
type | Get each node | Xmldefault Text | - | Text for the node text, Xpath for each node's full XPath with positions, or Attribute for the named attribute. Each match becomes one String row. |
attribute | attributeName | Stringdefault "" | - | The attribute to read; needed only with Attribute. A missing attribute gives an empty cell. |
Outputs
| Output | Type | Meaning |
|---|---|---|
result | Table | One String column named Column1, one row per XPath match. |
Example
Set Session name = sample, XPath Expression = //book, Get each node = AttributeName, attributeName = id. Bind result to Table $books$.
Expected result
The Table has String column Column1 and rows 1, 2. TextValue with //title returns Palatine and Minerva. XpathExpression for the second title gives /bookstore[1]/book[2]/title[1]. An empty attributeName with AttributeName fails.
Get single node (xmlGetSingleNodeV2)
Read the text or an attribute of the first XPath match. A match is required.
Inputs
Uses the shared fields: sessionName, xPath, attributeName.
Outputs
| Output | Type | Meaning |
|---|---|---|
result | String | The text of the first match, or the attribute value. |
Example
Set Session name = sample, XPath expression = /bookstore/book[2]/title, Attribute blank. Bind result to String $titleText$. To read the book id, use /bookstore/book[2] and Attribute = id.
Expected result
The title is Minerva; the attribute example returns 2. With Attribute blank, the text includes all text inside the node. An XPath with no match fails. With many matches, only the first is read.
Insert node (xmlInsertNode)
Insert one child per matched parent, with text, attributes and an optional namespace.
Inputs
Uses the shared fields: sessionName, attributes.
| Input | Editor field | Type; default | When / required | Use |
|---|---|---|---|---|
x | XPath Expression | Stringdefault "" | Required. | As x in shared fields. |
node | Node name | Stringdefault "" | Required. | Enter the new element name, such as isbn. Use a valid XML name. Existing children are compared by exact name (with prefix), not by text or XPath. |
node | Node value | Stringdefault "" | - | Enter text such as 42. It is set as text, so XML markup is escaped, not added as elements. Blank creates an element without text. |
insert | If node name is present then | Xmldefault insert | - | Choose insert to add another child even if one has that name, skip to leave a parent unchanged if a matching child exists, or overwrite to replace the first matching child. With no matching child, a new child is added. |
position | Insert node location | Xmldefault last | - | Choose first or last among the selected parent's children. before uses beforeRefChildName; after uses afterRefChildName. An overwrite keeps the replaced child's position and ignores this setting. |
before | Specified child node name | Stringdefault "" | - | With position=before, enter the exact child Name, such as title. The first matching child is the reference. Blank or missing reference fails the action. |
after | Specified child node name | Stringdefault "" | - | With position=after, enter the exact child Name, such as title. The first matching child is the reference. Blank or missing reference fails the action. |
default | Default namespace | Stringdefault "" | - | Enter the namespace URI for the new element, such as https://, or leave blank for no namespace. This does not register XPath prefixes. |
namespaces | Namespace | Dictionarydefault {} | - | For the parent XPath only: a Dictionary of prefix-to-URI Strings, for example c = https://, then use /. It does not add namespace declarations to the new element. |
Outputs
No outputs.
Example
Create Dictionary $attributes$ with String kind = issn. Set Session name = sample, XPath Expression = /bookstore/book[1], Node name = isbn, Node value = 42, If node name is present then = insert, Insert node location = first, Attributes = $attributes$. Leave namespace inputs empty.
Expected result
The first book starts with <isbn kind="issn">42</isbn> before title. skip keeps the existing matching child; overwrite removes the first matching child, with its old attributes and content, and puts the new child there. No parent match changes nothing. With many parents, if a later parent fails, earlier parents can already be changed.
Save session data (xmlSave)
Return the current document as XML text and, if you want, write it to a file.
Inputs
Uses the shared fields: sessionName.
| Input | Editor field | Type; default | When / required | Use |
|---|---|---|---|---|
write | Write XML data | Booleandefault false | - | False returns the serialized XML only. True also writes File path, creating parent folders. |
file | File path | Stringdefault "" | - | When Write XML data is true, enter a destination such as samples/. Relative paths use the Run working folder. |
overwrite | Overwrite | Booleandefault false | - | False refuses an existing destination. True replaces it. It matters only when writeToFile=true. |
pretty | Use Prettyprint | Booleandefault false | - | False returns the XML without added formatting. True indents with two spaces. Existing whitespace can stay; the output is not canonical XML. |
Outputs
| Output | Type | Meaning |
|---|---|---|
result | String | The current XML, returned also when a file is written. |
Example
Set Session name = sample, Write XML data = true, File path = samples/saved.xml, Overwrite = false, Use Prettyprint = false. Bind result to String $xmlText$. Use a new output path.
Expected result
The output String and the file contain the bookstore document from prerequisites. The file is UTF-8 without a byte-order mark. With Write XML data=false, only the String is returned and no file is touched. An existing path with overwrite off fails. Prettyprint adds indentation; then a kept XML declaration can say UTF-16 even though the file is UTF-8.
Start session (xmlStartSession)
Load XML into a named session before other steps. Opening a name again replaces its document.
Inputs
| Input | Editor field | Type; default | When / required | Use |
|---|---|---|---|---|
session | Session name | Stringdefault "" | Name not empty. | The name for the new session, such as sample; not a credential. Later steps use the same name. |
input | Data Source | Xmldefault file | - | Choose file to read File path or text to parse Text. Only the selected field is read; both fields stay visible. |
file | File path | Stringdefault "" | - | For file input, enter a readable path such as samples/. Size limits are in Prerequisites. |
data | Text | Stringdefault "" | - | For text input, an XML document that is not empty, for example the bookstore sample above, or bind a String variable containing it. DTD declarations are refused. |
remove | Remove text nodes that contain only white space | Booleandefault false | - | False keeps nodes with only whitespace. True removes them when loading. This changes the saved XML and child positions. |
Outputs
No outputs. The document is kept under Session name for later steps in this Run.
Example
Set Session name = sample, Data Source = text, Text = the bookstore document in prerequisites, Remove text nodes that contain only white space = false. Alternatively create samples/books.xml with that document and use Data Source = file, File path = samples/books.xml.
Expected result
Get single node at /bookstore/book[1]/title returns Palatine. Invalid XML, a DTD, an empty name or source, a file that cannot be read or a size over the limit fails.
Update node (xmlUpdateNode)
Replace the content of each match with text and, if you want, set attributes.
Inputs
Uses the shared fields: sessionName, xPath, attributes.
| Input | Editor field | Type; default | When / required | Use |
|---|---|---|---|---|
value | New value | Stringdefault "" | - | Enter replacement text such as Vulcan. The text is always set, also when attributes change; it replaces the existing content. |
update | Update attribute (s) | Booleandefault false | - | True also creates or replaces the attributes in Attributes on each matched element. False changes text only. Attributes not in the Dictionary stay. |
Outputs
No outputs.
Example
Create Dictionary $attributes$ with String lang = la. Open a fresh sample session. Set Session name = sample, XPath expression = /bookstore/book[1]/title, New value = Vulcan, Update attribute (s) = true, Attributes = $attributes$.
Expected result
The title becomes <title lang="la">Vulcan</title> in memory. No match changes nothing. Attributes are set only on elements, but the text is set on every selected node: selecting a parent removes its child elements.
Validate XML document (xmlValidate)
Check that the session document is well-formed, or check it against local XSD files. Use the returned status in a branch.
Inputs
Uses the shared fields: sessionName.
| Input | Editor field | Type; default | When / required | Use |
|---|---|---|---|---|
validation | Select validation type | Xmldefault Is | - | Choose Is to confirm that the open document parsed, or By for XSD validation. By is listed but always fails. |
schema | schemaFiles | Listdefault [] | - | For BySpecifiedSchemaFiles, a List (not empty) of XSD file paths, such as $schemas$ with one String item samples/. External schemas are not loaded. Each file has the same size limits as a document. |
Outputs
| Output | Type | Meaning |
|---|---|---|
result | String | Exact status VALID or INVALID. |
Example
Use a fresh sample session. For a format check, set Session name = sample, Select validation type = IsWellformed. Bind result to String $validationText$. For XSD, create samples/books.xsd with the schema below, create List $schemas$ with that path as its one String item, choose BySpecifiedSchemaFiles, and set schemaFiles = $schemas$.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore"/>
</xs:schema>Expected result
Both examples return VALID. IsWellformed always returns VALID for an open session, because Start session already checked it. The sample schema allows any content under bookstore; a schema that requires bookstore to be xs:string returns INVALID. Schema errors give INVALID. A file that cannot be read, an empty schema list, a broken XSD file or the DTD mode fails the step.