4️⃣ 🧭 XPath Tutorial
Estimated reading: 4 minutes 36 views

🌿 XPath Nodes – Understanding XML Node Types in XPath

🧲 Introduction – Why Learn XPath Nodes?

To master XPath, you must first understand what it’s selecting. XPath doesn’t just work with XML tags—it targets different node types like elements, attributes, text, comments, and more. Knowing how these XPath nodes function helps you craft precise and powerful queries in any XML document.

🎯 In this guide, you’ll learn:

  • The 7 node types in XPath
  • How XPath selects and filters node types
  • Common XPath queries using elements, attributes, and text
  • Real-world examples and best practices

🧱 What Are XPath Nodes?

In XPath, everything in an XML document is a node.

✅ XPath recognizes 7 types of nodes:

Node TypeXPath IdentifierExample Use
Element Nodeelement()<book>, <title>
Attribute Node@attrName@id, @lang in <title lang="en">
Text Nodetext()The inner content of <title>XML Guide</title>
Namespace NodeNot commonly usedNamespaces in XML (e.g., xmlns:prefix)
Comment Nodecomment()<!-- XML Note -->
Processing Instruction Nodeprocessing-instruction()<?xml-stylesheet?>
Root Node/The document root

📄 Sample XML for Practice

<library>
  <!-- A list of books -->
  <book id="101">
    <title lang="en">XML Guide</title>
    <author>Jane Doe</author>
    <price>499</price>
  </book>
</library>

🔍 XPath Node Selection Examples

ExpressionWhat It Selects
/library/bookAll <book> elements under <library>
//titleAll <title> elements in the document
//book/@idThe id attribute of all <book> elements
//title/@langThe lang attribute on <title> elements
//title/text()The text inside all <title> nodes
//comment()All comment nodes in the document
//processing-instruction()All processing instruction nodes

💡 XPath Node Functions

FunctionDescription
text()Selects only the text nodes
@attrNameSelects attributes of current node
node()Selects all child node types
comment()Selects comment nodes
processing-instruction()Selects processing instruction nodes

✏️ Real-World XPath Queries Using Nodes

//book/title/text()  

✅ Returns: XML Guide

//book[@id='101']/author  

✅ Returns: <author>Jane Doe</author>

//title[@lang='en']/text()  

✅ Returns: XML Guide

//comment()  

✅ Returns: <!-- A list of books -->


⚙️ XPath Node Navigation Recap

TargetSyntax ExampleDescription
Specific element//book/titleAll <title> inside <book>
Attribute value//book/@idGets the id attribute of <book>
Text content//book/title/text()Gets “XML Guide”
Any child node//book/node()Gets all children (element & text)

✅ Best Practices with XPath Nodes

  • ✔️ Use @ to access attributes precisely
  • ✔️ Use text() for values—not entire nodes
  • ✔️ Use node() sparingly (it returns all child nodes, including whitespace)
  • ✔️ Combine with filters ([condition]) for accuracy
  • ❌ Don’t confuse @attr with element names—it only matches attributes

📌 Summary – Recap & Next Steps

XPath works by selecting nodes from an XML document—and knowing the types of nodes it targets helps you write smarter, cleaner queries. Whether you’re pulling text, filtering by attributes, or accessing comments, XPath gives you full control.

🔍 Key Takeaways:

  • XPath nodes include elements, attributes, text, comments, and more
  • Use @, text(), node() to access specific parts of a node
  • Filter with conditions to narrow your node selection

⚙️ Real-world relevance: Used in data extractors, configuration file parsing, XML editors, API data validation, and more.


❓ FAQs – XPath Nodes

❓ What node types can XPath select?
✅ Elements, attributes, text, comments, processing instructions, and more.

❓ How do I get only the text content of a node?
✅ Use element/text().

❓ What’s the difference between node() and text()?
node() returns all child nodes (elements, text, etc.), while text() returns only text nodes.

❓ How do I select an attribute in XPath?
✅ Use @attributeName, e.g., //book/@id.

❓ Can I filter by node type?
✅ Yes. Use text(), comment(), processing-instruction() as functions.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

XPath Nodes

Or Copy Link

CONTENTS
Scroll to Top