In XSLT and XPath, understanding node types is crucial because everything in XML is treated as a node.
1. Element Nodes
<book>
<title>1984</title>
</book>
2. Attribute Nodes
<book id="b1" genre="Fiction">
<title>1984</title>
</book>
3. Text Nodes
<title>1984</title>
4. Namespace Nodes
<book xmlns:ns="http://example.com/ns">
<ns:title>1984</ns:title>
</book>
5. Comment Nodes
<!-- This is a comment -->
6. Processing Instruction Nodes
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
7. Document (Root) Node
-
Definition: The top-most node in the XML tree.
-
Every XML document has a single root node (/) that contains the root element.
-
Example in XPath: / selects the root node.
Summary Table of Node Types
| Node Type |
Description |
XPath Example |
| Element |
XML tags |
/library/book |
| Attribute |
Attributes inside elements |
book/@id |
| Text |
Text inside elements |
book/title/text() |
| Namespace |
Namespace declarations |
namespace::* |
| Comment |
Comments |
comment() |
| Processing Instruction |
Special instructions |
processing-instruction() |
| Document/Root |
The root of the XML tree |
/ |
Key Point:
XSLT operates on these nodes, not raw text. Templates, match, apply-templates, and XPath all work by navigating and manipulating different node types.