XML Attributes to Elements

Learn about the ConvertAttributesToElementsXML OTTL converter function.

Overview

The ConvertAttributesToElementsXML converter changes attributes of XML elements to nested child elements.

Syntax

ConvertAttributesToElementsXML(xml_string)

  • xml_string: the bracket notation location of the XML string field

Consider this log:

<log timestamp="1730511053177" level="info"/>

As it is ingested it is escaped into JSON and saved as a byte array. See Understand Escaping Characters and Working with the body. Suppose the log is decoded and saved in an attribute named decoded.

Examples

Input

{
	"_type": "log",
	"attributes": {
		"decoded": "<log timestamp=\"1730511053177\" level=\"info\"/>"
	},
	"body": "<log timestamp=\"1730511053177\" level=\"info\"/>",
	"resource": {...},
	"timestamp": 1733457562038
}

Statement

set(attributes["elements"], ConvertAttributesToElementsXML(attributes["decoded"]))

Output

{
	"_type": "log",
	"attributes": {
		"decoded": "<log timestamp=\"1730511053177\" level=\"info\"/>",
		"elements": "<log><timestamp>1730511053177</timestamp><level>info</level></log>"
	},
	"body": "<log timestamp=\"1730511053177\" level=\"info\"/>",
	"resource": {...},
	"timestamp": 1733457578941
}

In the new elements attribute, the timestamp and level are converted into child elements within the log element.