xml_append_tag
Appends a new XML tag with a given name and text content to one or more parent elements in an XML string. This is useful for adding fields to XML data during a migration or transformation.
Syntax
xml_append_tag(xml_string, tag_name, content)
xml_append_tag(xml_string, tag_name, content, xpath)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| xml_string | string | Yes | The XML string to modify. |
| tag_name | string | Yes | The name of the new tag to append. |
| content | string | Yes | The text content for the new tag. |
| xpath | string | Optional | An XPath expression selecting the parent element(s) to append to. If not provided, the tag is appended to the top-level root element. |
Returns
Type: string
The updated XML string with the new tag(s) appended.
Examples
Append a tag to the root:
xml_append_tag([xml_data], "status", "migrated")
If xml_data is "<record><name>Test</name></record>", the result is "<record><name>Test</name><status>migrated</status></record>".
Append to a specific element using XPath:
xml_append_tag([xml_data], "note", "Added by PB", "//record")
If xml_data is "<root><record><id>1</id></record></root>", the result includes the new tag inside the <record> element: "<root><record><id>1</id><note>Added by PB</note></record></root>".
Append to multiple matching elements:
xml_append_tag([xml_data], "processed", "true", "//item")
If xml_data contains multiple <item> elements, a <processed>true</processed> tag is appended to each one.
Notes
- When no
xpathis provided, the new tag is appended as a child of the outermost element. - The XPath can match multiple elements, in which case the new tag is appended to each matching parent.
- The output is pretty-printed XML.
- The function wraps the input in a temporary root element internally, so XML fragments (multiple top-level elements) are supported.
Comments
0 comments
Please sign in to leave a comment.