Saltar al contenido
uencoder

Usar XPath a consulta XML documentos

XPath navigates XML trees con path expressions, predicates, y funciones. Aprender absolute y relative paths, wildcards, y común funciones.

  1. 1

    XPath expressions select nodes from an XML tree: /root/child selects all child elements, //element selects anywhere, [@attr="value"] filters by attribute, text() extracts text content, * is a wildcard. XPath 1.0 is universally supported; XPath 2.0+ adds typing and more functions.

  2. 2
    Consultas JSONPath →

    Use JSONPath (the JSON equivalent of XPath) to query JSON converted from XML. First convert XML to JSON, then use JSONPath expressions like $.root.child[?(@.attr=="value")]. This is a two-step alternative to direct XPath.

  3. 3
    Formateador XML →

    Formatear the XML to understand its structure before writing XPath expressions. Indentation reveals the hierarchy, making it easier to construct correct paths. Verify your XPath by checking which nodes it selects.

  4. 4

    Common XPath patterns: /bookstore/book[1] (first book), /bookstore/book[price>35] (filter by child value), //title (all titles anywhere), count(//book) (count books), //book/@category (attribute values). Use predicates [...] for filtering and functions like text(), contains(), starts-with().

Preguntas frecuentes

XPath vs CSS selectors?
CSS selectors work on HTML/XML elements (simpler, limited). XPath is more powerful: can select by text content, use functions, navigate to parent/ancestor, and handle namespaces. Use CSS selectors for simple HTML queries; use XPath for complex XML or when CSS is insufficient.
XPath 1.0 vs 2.0 vs 3.0?
XPath 1.0 is universally supported (browsers, libxml). XPath 2.0 adds sequences, richer type system, more functions — supported by Saxon. XPath 3.0 adds dynamic evaluation. For web/browser use, stick with 1.0. For server-side XML processing with Saxon, use 2.0+.

Flujos que resuelven esta tarea

Herramientas relacionadas