FILTERXML function
Extracts specific values from XML text using an XPath expression; also a popular trick for splitting text into pieces.
Available in Excel 2013 and later for Windows, including Microsoft 365 for Windows; not available in Excel for the web or Excel for Mac
Excel 2013 and laterNot in Google Sheets
Syntax
FILTERXML(xml, xpath)| Argument | What it means | |
|---|---|---|
xml | Required | Valid XML text, for example an API response returned by WEBSERVICE or XML pasted in a cell. |
xpath | Required | An XPath query that says which elements to return, e.g. "//price" or "//item[sku='P-102']/qty". |
Returns: The matching values. Numbers come back as numbers; several matches spill down a column.
How FILTERXML works
FILTERXML reads XML - the tag-based format used by many older web services, RSS feeds and data exports - and returns the parts you ask for. You describe what you want with an XPath expression: //qty means "every qty element anywhere", and //item[sku='P-102']/qty means "the qty inside the item whose sku is P-102". If several elements match, the results spill down.
It was designed as the partner of WEBSERVICE, which downloads the XML. But FILTERXML became famous for a side use: by replacing a delimiter with closing and opening tags using SUBSTITUTE, any comma-separated text becomes XML, and FILTERXML can then split it into separate cells. In Microsoft 365, TEXTSPLIT does that job more simply and works on every platform.
The XML must be well formed. A stray & or < in the text, or an unclosed tag, produces #VALUE!. FILTERXML only exists in Excel for Windows.
Examples
1Pick one value with a condition
| A | B | |
|---|---|---|
| 1 | Order XML | |
| 2 | <order><item><sku>P-101</sku><qty>3</qty></item><item><sku>P-102</sku><qty>5</qty></item></order> | 5 |
Result: 5
The XPath finds the item element whose sku is P-102 and returns its qty. The result is a real number you can calculate with.
2List every SKU
| A | B | |
|---|---|---|
| 1 | Order XML | |
| 2 | <order><item><sku>P-101</sku><qty>3</qty></item><item><sku>P-102</sku><qty>5</qty></item></order> | P-101 |
| 3 | P-102 |
Two sku elements match, so the two codes spill into B2:B3.
3Split comma-separated text
| A | B | |
|---|---|---|
| 1 | Regions | |
| 2 | North,South,East | North |
| 3 | South | |
| 4 | East |
SUBSTITUTE turns each comma into a closing and opening tag, wrapping every region in its own element. FILTERXML then returns each element on its own row.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#VALUE! | The XML is not well formed (for example it contains a bare &), or the XPath is invalid or matches nothing. | Escape & as & with SUBSTITUTE, check the XPath, and test on a small sample. |
#NAME? | The workbook is open in Excel for the web or Excel for Mac. | Use Power Query (Data > Get Data) or TEXTSPLIT instead. |
Tips
- In the splitting formula, change the XPath from
"//s"to"//s[2]"to return only the second item, or"//s[last()]"for the last one. - For JSON APIs FILTERXML does not help; use Power Query to parse JSON.
Google Sheets
Use IMPORTXML(url, xpath) for XML or HTML on the web; for XML already in a cell use REGEXEXTRACT.
FILTERXML in other languages
In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.
| Language | Function name |
|---|---|
| Danish | FILTRERXML |
| Dutch | XML.FILTEREN |
| Finnish | SUODATA.XML |
| French | FILTRE.XML |
| German | XMLFILTERN |
| Hungarian | XMLSZŰRÉS |
| Italian | FILTRO.XML |
| Norwegian (Bokmål) | FILTRERXML |
| Portuguese (Brazil) | FILTROXML |
| Portuguese (Portugal) | FILTRARXML |
| Russian | ФИЛЬТР.XML |
| Spanish | XMLFILTRO |
| Swedish | FILTRERAXML |
| Turkish | XMLFİLTRELE |