Bỏ qua

Structural operate operations

Warning

The current page still doesn't have a translation for this language.

But you can help translating it: Contributing.

<operate> applies checked operation-control rows to XML or JSON template content. Every operation comes from the runtime registry; this page gives the exact parser form for an op... cell.

Path rules

Each operation selects its targets with a path. XML templates use XPath. JSON templates use JSONPath.

XPath

  • The path is an XPath 1.0 expression. The lxml library evaluates it.
  • A path that starts with a name behaves like the same path with // before it. So item behaves like //item and matches at every depth. A position counts below each parent: item[1] is the first item of every parent. A path that starts with /, ., * or ( is read from the root element as written, so . is the root element and * selects its children.
  • The path must select elements. A path that selects an attribute, a text node, a comment, a processing instruction or a plain value (such as count(//a)) fails with an execution error. To change an attribute value, select its element and use upsertAttribute. To delete an attribute, use removeAttributes. To change text, select its element and use set.
  • An operation fails with an execution error when its value cannot be used, for example malformed XML in appendChild, replaceChildren or replaceNode, or a mergeAttributes value that is not only name="value" pairs, separated by spaces (single quotes work too, and a value without spaces, quotes, = or , needs no quotes; any other text fails the whole operation). delete and replaceNode never remove or replace the root element: a path that selects only the root fails, and for a path that also selects other elements the operation skips the root and applies to the rest. A failed operation leaves the template as it was.
  • Namespace prefixes come from the root element of the template. Only the prefixes that the root element declares are known, and the prefix xml, which needs no declaration, for example xml:lang. A prefix that only a nested element declares is an error.
  • upsertAttribute, mergeAttributes, removeAttributes and replaceAttribute accept attribute names with a known prefix, for example xsi:type. An undeclared prefix fails with an execution error. replaceAttribute reads each pair as old:new. In a pair with three parts, the first part belongs to the old name when it is a known prefix, for example xsi:nil:null. In a pair with four parts, both names have a prefix, for example xsi:nil:xsi:type.
  • A default namespace (xmlns="...") has no prefix. Select such an element with *[local-name()='name'].
  • upsertNode and upsertAttribute create the last step of the path when it matches nothing. The path before the last / must select the parent element, so a path such as status fails: write //order/status. If it selects nothing, the operation creates nothing and its outcome is not_matched. The last step is a name, with a prefix if the element needs one, for example //x:parent/x:child. The root element must declare the prefix. An undeclared prefix fails with an execution error. XPath 1.0 cannot select an element of a default namespace without a prefix, so select its parent with *[local-name()='parent'], for example //*[local-name()='order']/status. The created element has no prefix, so the written text puts it in the default namespace.

JSONPath

  • The jsonpath-ng library parses the path. These forms work: $.name, $['name'], $.list[0], $.list[-1], $.list[0:2], $.list[*], $.object.* and $..name.
  • Filter expressions such as [?(@.id > 1)] are not supported. They fail with an execution error.
  • The path $ selects the whole document. It cannot be changed.
  • A match must be an object property or an array item. A negative index counts from the end of the array in every operation. A negative index past the start of the array fails with an execution error, while a positive index past the end matches nothing. A JSONPath union (a | b) is not supported and fails with an execution error.
  • A JSON operation needs a target of the right type. mergeObject, clearObject and removeProperties need an object. replaceArray, appendItem, clearArray and removeItem need an array. upsertProperty needs an object property, not an array item. mergeObject needs a JSON object as value, and replaceArray needs a JSON array. If no matched target has the right type, or the value has the wrong type, the operation fails with an execution error and leaves the template as it was. If only some matched targets have the right type, the operation skips the others. Each target gets its own copy of a value that is an array or an object.
  • removeItem compares the JSON type and the value. The value true does not remove 1.
  • upsertProperty creates a missing property. The path must have the form $.name or $.name.name. A name has letters, digits and _ only. The template root must be a JSON object.
  • upsertProperty creates the missing objects on the way. It fails when a step exists and is not an object. Array indexes, quoted names, wildcards and .. are not allowed for creation.

Values

  • A value is a literal. The render never evaluates it, so text like {name} stays as written.
  • The control cell splits at the first comma. The path cannot contain a comma. The value can.
  • The parser reads a value as JSON when it can: numbers, true, false, null, arrays and objects. NaN, Infinity and a number too large for a double (1e999) are not valid JSON values: an operation with them cannot be parsed. Quote them, for example 'NaN', to write the text. In a descriptor run, a dynamic value ({expression}) that gives NaN or Infinity cannot be used: the cell is dropped with a warning, as every unparseable cell is, also with error. JSON text that mergeObject or replaceArray reads from a string follows the same rules, but there a value that breaks them is an execution error of the operation, not a parse error. Other text is a string. A matching pair of quotes around the text is removed.
  • In XML, true and false are written as the text true and false. Numbers are written in decimal form, so 5 stays 5 and 2.5 stays 2.5. null becomes an empty element. Arrays and objects are written as JSON text.
  • In JSON, a value keeps its type: true is a boolean and 'true' is a string.

Render semantics

The render applies the operations of one control row to one template, in the order of the control cells. For each operation it reports one outcome:

Outcome Meaning
applied The operation ran. This includes an operation on a target that is already in the requested state, for example clearArray on an empty array or removeAttributes of an attribute that is absent.
not_matched The path matched nothing. The operation changed nothing.
skipped_deleted_path An earlier delete in the same row removed the path. The render skips a set on that path.

The setting operation_not_matched_action decides what a not_matched or skipped_deleted_path outcome causes in the render API:

  • warn (default): The render returns the artifact. Each such outcome adds one warning with the operation index, the path and the reason.
  • error: The first such outcome fails the request. The error names the row and the operation index.
  • ignore: The render returns the artifact. It reports no warning. The outcomes stay in the result.

The render API (render_operate_rows) fails the request in these cases with every setting of operation_not_matched_action:

  • An operation that cannot be parsed, for example a value that is nested more than about 450 levels deep, a value with NaN or Infinity, or a path or value with a lone surrogate (an unpaired UTF-16 code unit such as \ud800), which the UTF-8 artifact cannot hold. The render never skips it.
  • An operation that fails to run, for example with an invalid path or a path that selects attributes or text. A render cannot run a function operation; it always fails with an execution error.
  • An operation that the template format does not support, for example an XML-only operation on a JSON template. This is an execution error.
  • A template that is not valid XML or JSON, or a JSON template that is nested more than about 900 levels deep.

A descriptor run (<operate>) reads operation_not_matched_action for each failed operation: a path that matches nothing, an invalid path, a path that selects attributes or text, a value that cannot be used (for example an empty value), an unsupported operation or a function operation. With error the row fails and counts as a failed row. With warn the run logs a warning and continues with the next operation. With ignore the run continues without a warning. With warn and ignore the failed operation keeps a failed trace. A skipped_deleted_path outcome is different: with every setting the descriptor run skips the set, logs a warning and continues, and the skipped operation keeps a failed trace.

The artifact is UTF-8 text. An XML artifact keeps the XML declaration of the template. A declared encoding other than UTF-8 becomes UTF-8. The render indents XML and JSON with two spaces. If no operation applies, the render returns the template text as written; it does not indent it. Only a declared encoding other than UTF-8 changes, as the rule above states. The same input gives the same output.

The XML parser reads no external entity, no external DTD and no network resource. This holds for templates, XML fragments and the XSD files in the schemas folder of a descriptor run. A template or an XML fragment that declares an entity in its DOCTYPE fails. A DOCTYPE without an entity declaration is allowed: a template keeps it and a fragment drops it. In the render API, such a template fails the request like a template that is not valid XML or JSON, and such a fragment is an execution error of its operation. In a descriptor run, a failed fragment follows operation_not_matched_action like any other failed operation. A rejected template follows operation_not_matched_action too: with error the row fails; with warn or ignore the run writes no artifact for the row and does not count the row as failed.

A number in a JSON template keeps its written form when no operation changes it: 100.00 stays 100.00, 1e2 stays 1e2, and a number with 20 digits keeps all its digits. A number that an operation writes has the normal form of its value, so the value 100.00 is written as 100.0. NaN and Infinity are not valid JSON. A template that contains them fails like a template that is not valid JSON.

json

appendItem

Append one supplied value to every array matched by a JSONPath.

Control-cell form: appendItem(jsonpath, value)

Supply a JSONPath followed by the operation-specific value.

Example

Template:

1
{"tags":["a","b"]}

Control cell: appendItem($.tags, c)

Result:

1
2
3
4
5
6
7
{
  "tags": [
    "a",
    "b",
    "c"
  ]
}

clearArray

Remove every item from each array matched by a JSONPath.

Control-cell form: clearArray(jsonpath)

Supply only the JSONPath; the matched container is cleared.

Example

Template:

1
{"tags":["a","b"]}

Control cell: clearArray($.tags)

Result:

1
2
3
{
  "tags": []
}

clearObject

Remove every property from each object matched by a JSONPath.

Control-cell form: clearObject(jsonpath)

Supply only the JSONPath; the matched container is cleared.

Example

Template:

1
{"customer":{"name":"Ada","city":"Bonn"}}

Control cell: clearObject($.customer)

Result:

1
2
3
{
  "customer": {}
}

delete

Delete every property or array item matched by a JSONPath.

Control-cell form: delete(jsonpath)

Supply only the JSONPath; this operation has no value argument.

Example

Template:

1
{"id":1001,"status":"new"}

Control cell: delete($.status)

Result:

1
2
3
{
  "id": 1001
}

function

Execute a custom function on JSON content.

Control-cell form: {functionName}

Supply a registered function expression in braces instead of an operation call.

Example

Template:

1
{"id":1001}

Control cell: {normalizeId}

Result: A render cannot run a function operation. It always fails with an execution error.

mergeObject

Merge the supplied JSON object into every object matched by a JSONPath.

Control-cell form: mergeObject(jsonpath, value)

Supply a JSONPath followed by the operation-specific value.

Example

Template:

1
{"customer":{"name":"Ada"}}

Control cell: mergeObject($.customer, {"city":"Bonn"})

Result:

1
2
3
4
5
6
{
  "customer": {
    "name": "Ada",
    "city": "Bonn"
  }
}

removeItem

Remove the first item equal to the supplied value from every matched array.

Control-cell form: removeItem(jsonpath, value)

Supply a JSONPath followed by the operation-specific value.

Example

Template:

1
{"tags":["a","b","c"]}

Control cell: removeItem($.tags, b)

Result:

1
2
3
4
5
6
{
  "tags": [
    "a",
    "c"
  ]
}

removeProperties

Remove a comma-separated set of property names from every matched object.

Control-cell form: removeProperties(jsonpath, value)

Supply a JSONPath followed by the operation-specific value.

Example

Template:

1
{"customer":{"name":"Ada","city":"Bonn","zip":"53111"}}

Control cell: removeProperties($.customer, city,zip)

Result:

1
2
3
4
5
{
  "customer": {
    "name": "Ada"
  }
}

replaceArray

Replace the contents of every matched array with the supplied JSON array.

Control-cell form: replaceArray(jsonpath, value)

Supply a JSONPath followed by the operation-specific value.

Example

Template:

1
{"tags":["a","b"]}

Control cell: replaceArray($.tags, ["x","y","z"])

Result:

1
2
3
4
5
6
7
{
  "tags": [
    "x",
    "y",
    "z"
  ]
}

set

Set the value of every property or array item matched by a JSONPath.

Control-cell form: set(jsonpath, value)

Supply a JSONPath followed by the operation-specific value.

Example

Template:

1
{"id":1001,"status":"new"}

Control cell: set($.status, shipped)

Result:

1
2
3
4
{
  "id": 1001,
  "status": "shipped"
}

upsertProperty

Set a property or create the final property on an existing object path.

Control-cell form: upsertProperty(jsonpath, value)

Supply a JSONPath followed by the operation-specific value.

Example

Template:

1
{"customer":{"name":"Ada"}}

Control cell: upsertProperty($.customer.city, Bonn)

Result:

1
2
3
4
5
6
{
  "customer": {
    "name": "Ada",
    "city": "Bonn"
  }
}

xml

appendChild

Append an XML fragment, or a text-wrapped item, below each matched element.

Control-cell form: appendChild(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><items><item>pen</item></items></order>

Control cell: appendChild(//items, <item>ink</item>)

Result:

1
2
3
4
5
6
<order>
  <items>
    <item>pen</item>
    <item>ink</item>
  </items>
</order>

clearAttributes

Remove every attribute from each element matched by an XPath.

Control-cell form: clearAttributes(xpath)

Supply only the XPath; the matched container is cleared.

Example

Template:

1
<order><item sku="A1" color="red">pen</item></order>

Control cell: clearAttributes(//item)

Result:

1
2
3
<order>
  <item>pen</item>
</order>

clearChildren

Remove all child nodes and text from each matched element.

Control-cell form: clearChildren(xpath)

Supply only the XPath; the matched container is cleared.

Example

Template:

1
<order><items><item>pen</item><item>ink</item></items></order>

Control cell: clearChildren(//items)

Result:

1
2
3
<order>
  <items></items>
</order>

delete

Delete every element matched by an XPath from its parent.

Control-cell form: delete(xpath)

Supply only the XPath; this operation has no value argument.

Example

Template:

1
<order><id>1001</id><status>new</status></order>

Control cell: delete(//status)

Result:

1
2
3
<order>
  <id>1001</id>
</order>

function

Execute a custom function on XML content.

Control-cell form: {functionName}

Supply a registered function expression in braces instead of an operation call.

Example

Template:

1
<order><id>1001</id></order>

Control cell: {normalizeId}

Result: A render cannot run a function operation. It always fails with an execution error.

mergeAttributes

Add or overwrite quoted name=value attributes on each matched element.

Control-cell form: mergeAttributes(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><item sku="A1">pen</item></order>

Control cell: mergeAttributes(//item, qty="2" color="red")

Result:

1
2
3
<order>
  <item sku="A1" qty="2" color="red">pen</item>
</order>

removeAttributes

Remove a comma-separated set of attribute names from each matched element.

Control-cell form: removeAttributes(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><item sku="A1" color="red">pen</item></order>

Control cell: removeAttributes(//item, color)

Result:

1
2
3
<order>
  <item sku="A1">pen</item>
</order>

removeChild

Remove child nodes selected by a relative XPath below each matched element.

Control-cell form: removeChild(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><items><item>pen</item><note>fragile</note></items></order>

Control cell: removeChild(//items, note)

Result:

1
2
3
4
5
<order>
  <items>
    <item>pen</item>
  </items>
</order>

replaceAttribute

Rename existing attributes using comma-separated old:new pairs.

Control-cell form: replaceAttribute(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><item sku="A1">pen</item></order>

Control cell: replaceAttribute(//item, sku:code)

Result:

1
2
3
<order>
  <item code="A1">pen</item>
</order>

replaceChildren

Replace all child nodes of each matched element with XML fragments or text.

Control-cell form: replaceChildren(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><items><item>pen</item><item>ink</item></items></order>

Control cell: replaceChildren(//items, <item>paper</item>)

Result:

1
2
3
4
5
<order>
  <items>
    <item>paper</item>
  </items>
</order>

replaceNode

Replace each matched element with one well-formed XML element.

Control-cell form: replaceNode(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><status>new</status></order>

Control cell: replaceNode(//status, <state>shipped</state>)

Result:

1
2
3
<order>
  <state>shipped</state>
</order>

set

Set the text content of every element matched by an XPath.

Control-cell form: set(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><id>1001</id><status>new</status></order>

Control cell: set(//status, shipped)

Result:

1
2
3
4
<order>
  <id>1001</id>
  <status>shipped</status>
</order>

upsertAttribute

Set a name=value attribute and create the final element when its parent exists.

Control-cell form: upsertAttribute(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><item>pen</item></order>

Control cell: upsertAttribute(//item, sku=A1)

Result:

1
2
3
<order>
  <item sku="A1">pen</item>
</order>

upsertNode

Set a matched node or create the final node below an existing parent XPath.

Control-cell form: upsertNode(xpath, value)

Supply an XPath followed by the operation-specific value.

Example

Template:

1
<order><id>1001</id></order>

Control cell: upsertNode(//order/status, shipped)

Result:

1
2
3
4
<order>
  <id>1001</id>
  <status>shipped</status>
</order>