Xpath Nodes in XML
XPath Nodes in XML
What are Nodes in XPath?
In XPath, an XML document is seen as a tree of nodes. XPath expressions work by selecting these nodes.
Types of Nodes in XPath
| Node Type | Description | Example |
|---|---|---|
| Element Node | Represents an XML element (tag). | <book>...</book> |
| Attribute Node | Represents an attribute of an element. | category="fiction" in <book category="fiction"> |
| Text Node | Represents the text content inside an element. | Text "The Hobbit" inside <title>The Hobbit</title> |
| Namespace Node | Represents the namespace declarations. | xmlns:x="..." |
| Processing Instruction Node | Special instructions for applications. | <?xml-stylesheet ... ?> |
| Comment Node | Comments in XML. | <!-- This is a comment --> |
| Document Node | The root node of the XML document tree. | The entire XML document |
Examples of Selecting Nodes with XPath
Given this XML:
<book category="fiction"> <title lang="en">The Hobbit</title> <author>J.R.R. Tolkien</author> <!-- Popular fantasy novel --></book>| XPath Expression | Selects | Result Node Type |
|---|---|---|
/book | The <book> element node | Element Node |
/book/@category | The category attribute | Attribute Node |
/book/title/text() | Text inside <title> | Text Node |
/book/comment() | XML comment | Comment Node |
/processing-instruction() | Processing instructions (if any) | Processing Instruction Node |
Summary
XPath deals with different node types in an XML document.
You can select elements, attributes, text, comments, and more.
Understanding node types helps in writing precise XPath expressions.
If you want, I can help you with XPath queries targeting specific node types in your XML!