xml_path
Extracts the text content of the first element matching an XPath expression in an XML string. If the matching element contains child elements, the full XML of that element is returned instead of just its text.
Syntax
xml_path(xml_string, xpath)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| xml_string | string | Yes | The XML string to search. |
| xpath | string | Yes | The XPath expression specifying which element to extract. |
Returns
Type: string or None
The text content of the first matching element, or the full XML string of that element if it has child elements. Returns None if no element matches the XPath.
Examples
Extract text from a simple element:
xml_path([xml_data], "//item")
If xml_data is "<root><item>Hello</item></root>", the result is "Hello".
Extract an element that has children (returns full XML):
xml_path([xml_data], "//item")
If xml_data is "<root><item><sub>Text</sub></item></root>", the result is "<item><sub>Text</sub></item>".
Extract with a specific path:
xml_path([payload], "//record/sys_id")
If payload is "<response><record><sys_id>abc123</sys_id><name>Test</name></record></response>", the result is "abc123".
Notes
- Only the first matching element is returned. Use
xml_path_multito retrieve all matches. - The XPath expression follows standard XPath 1.0 syntax as supported by the
lxmllibrary. - If the matching element has child elements, the full serialized XML of that element is returned, not just the concatenated text.
- Returns
None(not an error) when no elements match the XPath.
Comments
0 comments
Please sign in to leave a comment.