--- title: XPath slug: Web/XPath tags: - XPath translation_of: Web/XPath ---

XPath 的意思是 XML 路径语言。它使用的一个非 XML 语法提供一种灵活地定位 XML 文档的不同部分的方法。它同时也可以用于检测文档中某个节点是否与某个模式(pattern)匹配。

XPath 主要被用于 XSLT,也可用于定位文档元素,像类 XML 语言文档(如HTML 和 XUL ) 通过 DOM 定位元素一样。替代 {{Domxref("document.getElementById")}} 方法、 {{Domxref("element.childNodes")}} 属性和其他DOM核心特性。

XPath 使用路径标识符通过层级结构来导航XML文档。它使用非XML语法,以致于它可以被用在URIs和XML属性值上。

注意:XPath 的浏览器支持差别很大;Firefox 支持地相当不错 (尽管没有计划进一步提高支持),而其他浏览器则较小程度地实现了它。如果你需要一个兼容主流浏览器的 XPath,你可以考虑试试 js-xpathwicked-good-xpath.

文档

在 JavaScript 中使用 XPath 的介绍
描述了 XPath 的一个非 XSLT 使用。
XPath:Axes
List and definition of the axes defined in the XPath specification. Axes are used to describe the relationships between nodes.
XPath:Functions
List and description of the core XPath functions and XSLT-specific additions to XPath.
Transforming XML with XSLT
XSLT uses XPath to address code segments in a XML document that it wishes to transform.
XPath snippets
JavaScript utility functions to use in your own code, based on DOM Level 3 XPath APIs.
What is XSLT?
This extensive introduction to XSLT and XPath assumes no prior knowledge of the technologies, and guides the reader through background, context, structure, concepts and introductory terminology.
JXON
JXON (lossless JavaScript XML Object Notation) is a generic name by which is defined the representation of JavaScript Objects using XML. There are some cases in which the whole content of an XML document must be read from the JavaScript interpreter (like for web-apps languages or settings XML documents, for example). In these cases JXON could represent the most practical way and a valid alternative to XPath.

工具

XPather
Feature rich XPath generator, editor, inspector, and simple extraction tool (FireFox Add-On).
FireXPath
XPath panel that integrates tightly into FireBug, providing an editor and inspector (FireFox Add-On).
XMLQuire (formerly known as SketchPath)
A Graphical XPath Builder/Debugger(.NET).
{{QuickLinksWithSubpages("/zh-CN/docs/Web/XPath")}}