xml_path_multi
Extracts the text content of all elements matching an XPath expression in an XML string. For each matching element, if it contains child elements the full XML is returned; otherwise just the text content.
Syntax
xml_path_multi(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 elements to extract. |
Returns
Type: list[string]
A list of strings, one per matching element. Each entry is either the text content of the element or its full XML serialization if it has child elements.
Examples
Extract text from multiple elements:
xml_path_multi([xml_data], "//item")
If xml_data is "<root><item>Text1</item><item>Text2</item></root>", the result is ["Text1", "Text2"].
Extract elements that have children:
xml_path_multi([xml_data], "//record")
If xml_data is "<root><record><id>1</id></record><record><id>2</id></record></root>", the result is ["<record><id>1</id></record>", "<record><id>2</id></record>"].
Combine with join to flatten into a string:
join(", ", xml_path_multi([xml_data], "//name"))
If xml_data is "<people><name>Alice</name><name>Bob</name></people>", the result is "Alice, Bob".
Notes
- Returns an empty list if no elements match the XPath.
- The XPath expression follows standard XPath 1.0 syntax as supported by the
lxmllibrary. - Use
xml_pathinstead if you only need the first matching element.
Comments
0 comments
Please sign in to leave a comment.