🛠️ToolsShed

XPath Tester

Test XPath expressions against XML documents.

No matching nodes

Domande Frequenti

Implementazione del Codice

from lxml import etree

xml_text = """<?xml version="1.0"?>
<bookstore>
  <book category="cooking">
    <title>Everyday Italian</title>
    <price>30.00</price>
  </book>
  <book category="web">
    <title>Learning XML</title>
    <price>39.95</price>
  </book>
</bookstore>"""

root = etree.fromstring(xml_text.encode())

# Basic XPath queries
titles = root.xpath("//book/title/text()")
print("All titles:", titles)

# Predicate filter
web_books = root.xpath("//book[@category='web']/title/text()")
print("Web books:", web_books)

# Aggregate function
prices = root.xpath("//price/text()")
total = sum(float(p) for p in prices)
print(f"Total: {total:.2f}")

# Namespace example
NS = {"ns": "http://www.example.com/ns"}
# root.xpath("//ns:book", namespaces=NS)

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.