InsertXML
less than a minute
The InsertXML converter inserts a value into an XML document at a specified XPath location.
Syntax: InsertXML(xml_string, xpath, value)
- xml_string: the bracket notation location of the XML string field
- xpath: the XPath location to insert the new value
- value: the value to insert
Input
{
"_type": "log",
"body": "<log><message>SYSTEM</message></log>",
"resource": {...},
"timestamp": 1733886431434
}
Statement
set(cache["body"], Decode(body, "utf-8"))
set(attributes["inserted_xml"], InsertXML(cache["body"], "/log", "<level>info</level>"))
See Working with the body and Working with a cache for more information.
Output
{
"_type": "log",
"attributes": {
"inserted_xml": "<log><message>SYSTEM</message><level>info</level></log>"
},
"body": "<log><message>SYSTEM</message></log>",
"resource": {...},
"timestamp": 1733886447480
}
The XML data initially stored as a byte array in the body field was first decoded into a UTF-8 string format using the Decode function and temporarily stored in a cache for manipulation. This decoded XML string was then modified by inserting a new element <level>info</level> at the specified XPath location /log using the InsertXML function. The modified XML, now containing the newly added element, was stored under the attributes field as inserted_xml.