
The `FILTERXML` function in Excel is used to extract specific data from an XML content using XPath queries. It’s particularly useful when working with data that is stored in XML format. However, it’s important to note that the `FILTERXML` function is available only on Windows and is not supported in Excel for Mac.
Here’s how you can use the `FILTERXML` function:
Syntax:
FILTERXML(xml, xpath)
- xml: This is the XML content that you want to parse. It must be in the text format and typically comes from a cell where XML data is stored or generated by another function like `WEBSERVICE`.
- xpath: This is the XPath query that specifies which parts of the XML data you want to extract.
Example Usage:
Suppose you have an XML string in cell A1:
<data>
<item>
<name>Item1</name>
<price>10</price>
</item>
<item>
<name>Item2</name>
<price>20</price>
</item>
</data>
To extract the names of the items, you can use the following formula:
=FILTERXML(A1, "//name")
To extract the prices of the items, use:
=FILTERXML(A1, "//price")
Key Points:
- XML Input: The XML data can be directly input into the formula or referenced from another cell. The content must be properly formatted as XML.
- XPath Queries: XPath is used to navigate through elements and attributes in the XML. Familiarity with XPath syntax is necessary to write effective queries.
- Multiple Rows: If the XPath matches multiple nodes, `FILTERXML` may return results in an array, displaying them in multiple cells if entered as an array formula in legacy Excel, or with `Ctrl + Shift + Enter`. Newer versions with Dynamic Arrays will spill the results in contiguous cells automatically.
Use Cases:
- Web Data: Often used in conjunction with `WEBSERVICE` to fetch and parse web data provided in XML format.
- Data Analysis: Extract structured data for analysis or reporting from XML sources.
Limitations:
- Not on Mac: `FILTERXML` is not available in Excel for Mac.
- No JSON Support: It only works with XML data, not with JSON or other formats.
- Complex XPath: Creating effective XPath queries may require some learning and understanding of XML structure and XPath syntax.
By mastering the `FILTERXML` function and XPath, you can effectively handle and analyze XML data directly within Excel.