Excel Function: FILTERXML

Purpose

The FILTERXML function extracts data from an XML document. The data to be extracted are specified by the xpath argument.

Invocations of the FILTERXML function take the form:

FILTERXML(xml,xpath)

Limitations

Arguments

ArgumentArgument typeDescription
xmlMandatoryAn XML document from which data will be extracted.
xpathMandatoryAn XPath expression used to locate the data to be extracted.

Related Functions

FunctionExamplePurpose
WEBSERVICEFILTERXML(WEBSERVICE(url),xpath)Call a web service whose endpoint is at some URL, then extract data from the response.

Error Conditions

The FILTERXML function will respond with an error value if any of the following conditions are met.

Error valueCondition typeCondition
#VALUE!Bad argumentThe document supplied to the xml argument was not valid XML.
#VALUE!Bad argumentThe document supplied to the xml argument contained a namespace with an invalid prefix.
#VALUE!Bad argumentNo matches to the expression supplied to the xpath argument were found.
#VALUE!Bad argumentAn element was found and an attempt was made to extract its value, but it had none.

Notes

Examples: Traversing & Searching XML

ExampleDescriptionXMLFormulaResult
1Extract the root element’s value.
  • The root element is <a>
<a>a-node-value
    <b>b-node-value</b>
</a>
=FILTERXML(<xml>,"/a")a-node-value
2Extract a child element’s value.
  • “/a/b” isolates <b> elements which are children of <a> elements.
  • One <b> element matches the search criteria.
  • Its value is returned.
<a>a-node-value
    <b>b-node-value</b>
</a>
=FILTERXML(<xml>,"/a/b")b-node-value
3Extract the values from 2 child elements.
  • “/a/b” isolates <b> elements which are children of <a> elements.
  • Two <b> elements match the search criteria.
  • Their values are returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
</a>
=FILTERXML(<xml>,"/a/b")b1-node-value
b2-node-value
4Extract the values of elements based on an attribute’s value.
  • “/a/b[@id=’2′]” isolates <b> elements
    • which are children of <a> elements.
    • whose id attribute has a value of “2”.
  • One <b> element matches the search criteria.
  • Its value is returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
</a>
=FILTERXML(<xml>,"/a/b[@id='2']")b2-node-value
5Select an element and extract its parent’s value.
  • “/a/b/..”
    1. isolates <b> elements which are children of <a> elements.
    2. gets the values of those element’s parents.
  • One <a> element matches the search criteria.
  • Its value is returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
</a>
=FILTERXML(<xml>,"/a/b/..")a-node-value
6Select elements and extract their parent’s values.
  • “//b/..”
    1. isolates all <b> elements in the XML.
    2. gets the values of those element’s parents.
  • Two elements match the search criteria.
  • Their values are returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
    <c>c-node-value
        <b id="3">b3-node-value</b>
        <b id="4">b4-node-value</b>
    </c>
</a>
=FILTERXML(<xml>,"//b/..")a-node-value
c-node-value
7Extract the values from ‘b’ elements.
  • “//b” isolates all <b> elements in the XML.
  • Three elements match the search criteria.
  • Their values are returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
    <c>
        <b id="3">b3-node-value</b>
    </c>
</a>
=FILTERXML(<xml>,"//b")b1-node-value
b2-node-value
b3-node-value
8Filter the values extracted in example 7 based on attribute values.
  • “//b[@id=’3′]”
    1. isolates all <b> elements in the XML.
    2. retains only those whose id attribute has a value of “3”.
  • One element matches the search criteria.
  • Its value is returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
    <c>
        <b id="3">b3-node-value</b>
    </c>
</a>
=FILTERXML(<xml>,"//b[@id='3']")b3-node-value
9Extract all values from ‘b’ elements, where their id ≥ 2.
  • “//b[@id>=2]”
    1. isolates all <b> elements in the XML.
    2. retains only those whose id attribute has a value ≥ “2”.
  • Two elements match the search criteria.
  • Their values are returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
    <c>
        <b id="3">b3-node-value</b>
    </c>
</a>
=FILTERXML(<xml>,"//b[@id>=2]")b2-node-value
b3-node-value
10Extract values from ‘b’ elements that are children of ‘c’ elements.
  • “//c/child::b”
    1. isolates all elements that are children of <c> elements.
    2. retains only <b> elements.
  • One element matches the search criteria.
  • Its value is returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
    <c>
        <b id="3">b3-node-value</b>
    </c>
</a>
=FILTERXML(<xml>,"//c/child::b")b3-node-value
11Extract values of attributes of type ‘id’.
  • “//@id” isolates the values of all “id” attributes.
  • Three elements contain attributes which match the search criteria.
  • Their values are returned.
<a>a-node-value
    <b id="1">b1-node-value</b>
    <b id="2">b2-node-value</b>
    <c>
        <b id="3">b3-node-value</b>
    </c>
</a>
=FILTERXML(<xml>,"//@id")1
2
3