IMPORTXML function

IMPORTXML extracts and returns specific data from XML or HTML documents by evaluating an XPath query against the content at a URL.

=IMPORTXML(url, xpath_query)

Generate a IMPORTXML formula

Describe what you need. The generator will reach for IMPORTXML where IMPORTXML is the right tool, and tell you when it is not.

How to get a better answer
  • Name your columns by letter and by header: "column F (Net Value)" beats "the amount column".
  • State every condition, including the negatives — "not cancelled" changes the formula's shape.
  • Say where the data starts if it is not row 1, and whether it will grow.
  • Check the settings above match your spreadsheet: the wrong argument separator is a syntax error on your machine.

Arguments

How IMPORTXML reads its arguments
urlrequiredxpath_queryrequiredIMPORTXML
ArgumentRequiredDescription
urlRequiredA string containing the full HTTPS/HTTP URL of the XML or HTML document to query. If empty or invalid, IMPORTXML returns #REF!. If reachable but returns non-XML content, #VALUE! is returned.
xpath_queryRequiredA string containing the XPath 1.0 expression to evaluate against the document. If invalid syntax is used, #VALUE! is returned. If valid but no nodes match, #N/A is returned.

Returns

An array of strings, with each element containing one matching result from the XPath query, arranged vertically or in multi-dimensional format depending on query complexity.

Availability

Excel: Not available · Google Sheets: Supported

Worked examples

1. Extract high-volume orders

Order ID
ORD-002
ORD-004
=IMPORTXML("https://sales-api.example.com/orders.xml", "//order[units>10]/orderId")

Result: {ORD-002, ORD-004}

The XPath expression //order[units>10]/orderId selects all order IDs where units exceed 10. In the retail dataset, two orders qualify: ORD-002 (12 units) and ORD-004 (15 units). IMPORTXML returns these IDs as a vertical array.

2. Find a rep's sales region

South
=IMPORTXML("https://sales-api.example.com/orders.xml", "//order[rep='Sarah Johnson']/region[1]")

Result: South

The XPath predicate [rep='Sarah Johnson'] filters orders by that specific rep, and /region[1] returns the first matching region. Sarah Johnson's orders are all in the South region, so the formula returns that single value.

3. Get unit prices for January orders

Unit Price
25
50
75
30
=IMPORTXML("https://sales-api.example.com/orders.xml", "//order[substring(orderDate,1,7)='2024-01']/unitPrice")

Result: {25, 50, 75, 30}

The XPath substring() function extracts the year-month portion of each orderDate and compares to '2024-01'. Four orders match this criteria in the dataset, with unit prices of $25, $50, $75, and $30. The formula returns all prices as a vertical array.

Common errors

Which IMPORTXML error are you seeing?
IMPORTXML returned an error#N/A
Inspect the actual XML structure using browser Dev Tools (View Source or Network tab) and verify that element names, attribute names, and predicates match exactly. Adjust your XPath path to align with the actual document structure.
#VALUE!
Validate XPath syntax—ensure all brackets are balanced, use correct operators (= not ==), and verify functions like substring() are spelled correctly. Confirm the URL returns XML or HTML by opening it in a browser and inspecting the source.
#REF!
Verify any cell references in the url parameter point to existing cells with valid URLs. Alternatively, replace the cell reference with a hardcoded URL string in quotes: =IMPORTXML("https://example.com/data.xml", xpath_query).
ErrorWhy it happensHow to fix it
#N/AThe XPath query is syntactically valid but returns no matching elements because the queried path or condition doesn't exist in the XML/HTML document.Inspect the actual XML structure using browser Dev Tools (View Source or Network tab) and verify that element names, attribute names, and predicates match exactly. Adjust your XPath path to align with the actual document structure.
#VALUE!The XPath query contains invalid syntax (mismatched brackets, wrong operators, undefined functions), or the URL points to content that is not XML or HTML (such as plain text, PDF, or JSON).Validate XPath syntax—ensure all brackets are balanced, use correct operators (= not ==), and verify functions like substring() are spelled correctly. Confirm the URL returns XML or HTML by opening it in a browser and inspecting the source.
#REF!The url parameter contains a cell reference (e.g., =IMPORTXML(A1, xpath)) but that cell has been deleted or does not contain a valid URL string.Verify any cell references in the url parameter point to existing cells with valid URLs. Alternatively, replace the cell reference with a hardcoded URL string in quotes: =IMPORTXML("https://example.com/data.xml", xpath_query).

Tips and when to use something else

  • IMPORTXML evaluates the XPath query once when the sheet loads, not in real-time. If the source data updates frequently, consider IMPORTHTML or IMPORTRANGE for more predictable refresh behavior.
  • Use XPath predicates like [units>10] or [starts-with(rep, 'S')] to filter directly in the query, reducing the need for additional formulas downstream.
  • To debug #N/A errors, copy the URL into your browser, view the page source, and manually inspect the XML structure to ensure your XPath path matches actual element names.
  • For simpler tasks like extracting structured tables from web pages, IMPORTHTML is often faster and more reliable than hand-crafting complex XPath queries.

Frequently asked questions

How do I import data from an HTML table on a web page instead of writing XPath queries?
Use IMPORTHTML instead, which is designed for extracting HTML tables and lists without requiring XPath. IMPORTHTML is simpler for structured tabular data but less powerful for complex parsing tasks.
Does IMPORTXML automatically refresh if the source data changes, or do I need to manually refresh?
IMPORTXML pulls data when the spreadsheet loads and when you manually refresh (Ctrl+Shift+F9 on Windows, Cmd+Shift+F9 on Mac). It does not auto-update on a schedule. To refresh periodically, use Google Apps Script with a time-based trigger.
What is the difference between IMPORTXML and IMPORTDATA?
IMPORTDATA imports flat, unstructured data from files in standard formats (CSV, TSV, tab-separated text) and arranges them in rows and columns. IMPORTXML parses XML or HTML and uses XPath queries to pinpoint and extract specific elements, offering much finer control over what data you retrieve.
Why do I get #N/A when the URL works perfectly in my browser?
The raw XML/HTML source that IMPORTXML receives often differs from what the browser renders. JavaScript frequently loads data dynamically after the page loads, so IMPORTXML only sees the initial markup. Use View Source or the Network tab in browser Dev Tools to inspect the actual HTML/XML structure.

Need a different formula?

The full generator is not scoped to one function — describe any spreadsheet problem and it will pick.

Open the formula generator

Reviewed 2026-09-17