XPath Generator

Generate XPath expressions from XML and validate your own expressions against it

Generate XPath expressions from XML online and test your own XPath queries against any XML document. Select a node to get its XPath instantly, then validate expressions to see exactly which elements match. This free XPath generator and tester helps developers with web scraping, test automation, XSLT, and XML data processing.

XML Input
Generated XPaths

XPath expressions will appear here.
Press Ctrl+Enter or click Generate XPaths.

Namespace prefixes:
Output limited to the first 5000 expressions.
XPathType#value
Validate XPath
Result type:
Showing the first 200 matches.
NodeNameValue

About XPath Generator

This tool reads an XML document and lists an XPath expression for every element, attribute and text node it contains. You can then copy an expression, or type your own and press Validate to check that it is syntactically correct and see exactly which nodes it selects. Parsing is done on the server with DTD processing disabled, and your XML is not stored.

Key Features

  • Three styles — absolute paths such as /catalog/book/title, indexed paths such as /catalog[1]/book[2]/title[1], or short paths such as //title.
  • Attributes and text — include @id attributes and text() nodes, or leave them out.
  • Namespace aware — prefixes from the document are reused, a default namespace gets the prefix ns, or switch to local-name() expressions.
  • Validate button — checks the XML is well-formed, compiles your XPath and shows the match count, node types and values.
  • Filter, copy and download — search the list, copy one path or all of them, or save them as a text file.

How to Use

  1. Paste or upload your XML.
  2. Choose a style and what to include, then click Generate XPaths.
  3. Copy a path, or send it to the validator with the arrow button.
  4. Click Validate to test the expression against your XML.

Notes

  • The validator supports XPath 1.0, which is what .NET and most XML tooling implement.
  • In absolute and short styles, repeated nodes are merged and shown with a count in the # column.
  • Documents that contain a DOCTYPE are rejected, because DTD processing is disabled for safety.

Related Developer Tools

Format your document with the XML Formatter, explore it with the XML Tree Viewer, or create a schema with the XML to XSD Converter.

What Is XPath?

XPath is a query language for selecting nodes in an XML document. An expression describes a route through the document tree, much like a file path describes a route through folders. The expression /catalog/book/title starts at the root element and selects every title inside every book. XPath is used by XSLT stylesheets, XQuery, SOAP and web-service tooling, configuration systems, and test frameworks such as Selenium, so a correct expression is useful in many places. Writing paths by hand for a large or deeply nested document is slow and error-prone, which is the problem this generator solves.

Choosing a Path Style

Absolute paths start at the root and spell out every step. They are precise and easy to read, but they break if the document structure changes. Indexed paths add a position such as book[2], so each expression points to exactly one node. This is useful when you need one specific item in a repeated list. Short paths use // to find a node anywhere in the document. They survive structural changes but can match more nodes than you intend, so check the match count with the validator before you rely on them.

Quick XPath Reference

  • / selects from the root, // selects from anywhere in the document, and . and .. mean the current and parent node.
  • @name selects an attribute, and text() selects the text inside an element.
  • [1] picks the first matching node, and [last()] picks the last one. XPath positions start at 1, not 0.
  • [@id='bk101'] filters by an attribute value, and [price > 10] filters by a child value.
  • contains(title, 'XML') and starts-with(@id, 'bk') match on partial text.
  • count(//book) and sum(//price) return a number instead of a list of nodes.

Use Cases

  • Writing XSLT templates and xsl:value-of selections without counting levels by hand.
  • Reading values from SOAP responses, RSS and Atom feeds, sitemaps or configuration files in code.
  • Building locators for automated UI and API tests.
  • Learning XPath by editing an expression and seeing the matched nodes immediately.
  • Documenting an XML format by exporting a list of every path it contains.

Tips for Reliable Expressions

Prefer the shortest expression that still selects only what you want, and avoid long indexed paths in code that must survive changes to the data. Use attribute filters such as [@id='bk101'] instead of positions when an element has a stable identifier. When a document uses namespaces, always use the prefixes shown under the results, because XPath 1.0 treats an unprefixed name as having no namespace. Finally, run the expression through the validator against realistic data: an expression can be valid and still return zero nodes, and the match count makes that obvious.

Frequently Asked Questions

Why does my XPath fail on a document with a default namespace?

XPath 1.0 cannot match a default namespace without a prefix. Use the ns prefix shown under the results, or *[local-name()='name'].

Does Validate need an XPath that returns nodes?

No. Expressions returning a number, string or boolean, such as count(//book), are valid and the value is displayed.