🌿 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 Type | XPath Identifier | Example Use |
---|---|---|
Element Node | element() | <book> , <title> |
Attribute Node | @attrName | @id , @lang in <title lang="en"> |
Text Node | text() | The inner content of <title>XML Guide</title> |
Namespace Node | Not commonly used | Namespaces in XML (e.g., xmlns:prefix ) |
Comment Node | comment() | <!-- XML Note --> |
Processing Instruction Node | processing-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
Expression | What It Selects |
---|---|
/library/book | All <book> elements under <library> |
//title | All <title> elements in the document |
//book/@id | The id attribute of all <book> elements |
//title/@lang | The 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
Function | Description |
---|---|
text() | Selects only the text nodes |
@attrName | Selects 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
Target | Syntax Example | Description |
---|---|---|
Specific element | //book/title | All <title> inside <book> |
Attribute value | //book/@id | Gets 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 :