How to use the FILTERXML function
What is the FILTERXML function?
The FILTERXML function extracts specific values from XML content by using the given xpath. The function was introduced in 2013 and you need Excel 2013 or a newer version to use the FILTERXML function.
What's on this page
1. Introduction
What is XML?
XML is an abbreviation for eXtensible Markup Language, it is a text format for storing and transporting data. It's used to structure, store, and transfer information between different systems or applications.
- XML uses tags to define elements, similar to HTML, but allows users to create custom tags.
- XML documents contain both data and information about what the data means.
- XML can be read by various applications across different platforms. XML is employed in various applications, including web services, configuration files, and data exchange formats.
- XML documents have a tree-like structure with nested elements.
- Unlike HTML, XML focuses on describing data structure, not presentation.
Here is an example of xml code:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="fiction"> <title>The Great Gatsby</title> <author>F. Scott Fitzgerald</author> <year>1925</year> <price>10.99</price> </book> <book category="non-fiction"> <title>A Brief History of Time</title> <author>Stephen Hawking</author> <year>1988</year> <price>14.99</price> </book> </bookstore>
How to use XML:
- Create an XML file: Use a text editor to write your XML document, following the proper syntax with tags and elements.
- Define structure: Use nested elements to represent hierarchical data relationships.
- Add attributes: Include additional information about elements using attributes within the opening tags.
- Validate: Use DTD (Document Type Definition) or XML Schema to ensure your document follows a specific structure.
- Parse: Use XML parsers in various programming languages to read and extract data from the XML file.
XML is particularly useful when you need a flexible, platform-independent way to structure and share data, especially when the data has a hierarchical nature. However, for simpler data structures or when performance is a critical factor, other formats like JSON might be preferred.
Introduction to XML - w3schools
What is a xpath?
XPath is a query language for selecting elements and attributes in XML data.
What is XPath? - w3schools
2. Syntax
FILTERXML(xml, xpath)
xml | Required. A value in XML format. |
xpath | Required. A value in XPath format. |
3. Example 1
The content in cell B3 is:
<movie><title>The Dark Knight</title> <title>Pulp fiction</title> <title>Inception</title></movie>
This is a simple XML structure representing movie titles. It contains:
- A root element <movie>
- Three <title> elements nested within the <movie> element, each containing a movie title:
- The Dark Knight
- Pulp fiction
- Inception
This XML snippet is what the FILTERXML function in cell B6 is processing to extract all the title contents.
Formula in cell B6:
This formula uses the FILTERXML function in Excel to extract data from the XML string stored in cell B3. Here is how:
- FILTERXML is an Excel function that allows you to query XML data using XPath expressions.
- The first argument (B3) refers to the cell containing the XML data.
- The second argument ("//title") is an XPath expression:
- "//" means "search anywhere in the document"
- "title" specifies that we're looking for all <title> elements
This formula searches the XML data in cell B3 for all <title> elements, regardless of their position in the XML structure, and returns their contents. In this particular case, it extracts:
- "The Dark Knight"
- "Pulp fiction"
- "Inception"
These results are displayed in cells B6, B7, and B8 respectively. The FILTERXML function is particularly useful for parsing and extracting specific data from XML structures within Excel, without needing to use more complex XML parsing tools or programming languages.
4. How to extract XML attribute?
An XML element may contain an attribute, the following formula demonstrates how to extract a given attribute from XML data.
What is an XML attribute?
An XML attribute is a name-value pair that provides additional information about an XML element.
XML attribute- w3schools
What is an XML element?
An XML element is a basic unit of an XML document that consists of a start tag, an end tag, and the content in between.
XML Elements - w3schools
Formula in cell B6:
Cell B3 contains the following XML:
<cities><NorthAmerica city="Vancouver" country="Canada"/><NorthAmerica city="Seattle" country="Usa"/></cities>
The formula in cell B6 returns {"Vancouver";"Seattle"} spilled to cell B7.
5. How to import XML data?
- Select a destination cell.
- Go to tab "Developer" on the ribbon.
- Press with left mouse button on the "Import" button, a file dialog box appears.
- Press with mouse on an XML file to select it.
- Press with left mouse button on the "Open" button to import the file.
Here is what the example XML data looks like:
6. Put XML data in different columns based on XML tags?
The formula in cell B6 concatenates // with the value in cell B5 and then extracts the corresponding data.
Formula in cell B6:
The ampersand character lets you concatenate values in an Excel formula.
Cell B3 contains the following xml:
<countries><Europe>France</Europe><Asia>China</Asia><Europe>Spain</Europe><Asia>Japan</Asia><Asia>Thailand</Asia><Africa>Kenya</Africa></countries>
Cell B6 returns {"France";"Spain"}.
Explaining formula in cell B6
Step 1 - Concatenate column header to build xpath arg
"//"&B$5
becomes
"//"&"Europe"
and returns "//Europe"
Step 2 - Extract XML data
FILTERXML($B$3,"//"&B$5)
returns {"France";"Spain"}.
7. FILTERXML function not working
- The FILTERXML returns a #VALUE! error if the XML is not valid.
- The FILTERXML returns a #VALUE! error if the XML contains a namespace with a prefix that is invalid.
'FILTERXML' function examples
This blog article describes how to split strings in a cell with space as a delimiting character, like Text to […]
The LIKE operator allows you to match a string to a pattern using Excel VBA. The image above demonstrates a […]
This article demonstrates formulas that rearrange values in a cell range to a single column. Table of Contents Rearrange cells […]
Functions in 'Web' category
The FILTERXML function function is one of 4 functions in the 'Web' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form