xml_tag_prefix
Adds a prefix to XML tag names in an XML string. By default all tags are prefixed, but an optional XPath expression can limit which tags are affected.
Syntax
xml_tag_prefix(xml_string, prefix)
xml_tag_prefix(xml_string, prefix, xpath)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| xml_string | string | Yes | The XML string to modify. |
| prefix | string | Yes | The prefix to add to the beginning of each tag name. |
| xpath | string | Optional | An XPath expression selecting which tags to prefix. If not provided, all tags are prefixed. |
Returns
Type: string
The updated XML string with prefixed tag names.
Examples
Prefix all tags:
xml_tag_prefix([xml_data], "pb_")
If xml_data is "<root><child>text</child></root>", the result is "<pb_root><pb_child>text</pb_child></pb_root>".
Prefix only specific tags using XPath:
xml_tag_prefix([xml_data], "src_", "//child")
If xml_data is "<root><child>text</child></root>", the result is "<root><src_child>text</src_child></root>". Only the <child> tag is prefixed.
Use to namespace tags before merging XML from different sources:
xml_tag_prefix([source_xml], "sn_", "//record/*")
This prefixes all child elements of <record> with "sn_", useful for distinguishing fields from different systems.
Notes
- When no
xpathis provided, every tag in the XML (including the root) is prefixed. - Tags that already have the prefix are not double-prefixed.
- The output is pretty-printed XML.
- XML fragments (multiple top-level elements) are supported, as the function wraps input internally.
Comments
0 comments
Please sign in to leave a comment.