snowflake.snowpark.functions.xpath¶

snowflake.snowpark.functions.xpath(col: Union[Column, str], path: str) → Column[source]¶

Extracts values from an XML column using an XPath expression.

Returns an array of strings containing all matches in document order. Returns an empty array if no matches are found.

Parameters:
  • col – Column containing XML data (string or parsed XML)

  • path – XPath expression string

Example:

>>> df = session.create_dataframe([['<root><a>1</a><a>2</a></root>']], schema=['xml'])
>>> result = df.select(xpath('xml', '//a/text()').alias('result')).collect()
>>> json.loads(result[0].RESULT)
['1', '2']

>>> # With attributes
>>> df2 = session.create_dataframe([['<root><item id="1">A</item><item id="2">B</item></root>']], schema=['xml'])
>>> result = df2.select(xpath('xml', '//item[@id="2"]/text()').alias('result')).collect()
>>> json.loads(result[0].RESULT)
['B']
Copy