</>
Skip to content
XML lessons (28/29)

XML — XPath Reference

XPath Syntax

ExpressionDescription
nodenameAll nodes
/Root
//Any position
.Current
..Parent
@Attribute
*Any element
@*Any attribute

Predicates

PredicateDescription
[1]First element
[last()]Last element
[last()-1]Second to last
[position()<3]First two
[@attr='val']By attribute
[text()='val']By text
[element='val']By child

Axes

AxisDescription
ancestorAll ancestors
ancestor-or-selfAncestors + self
attributeAttributes
childChildren
descendantAll descendants
descendant-or-selfDescendants + self
followingFollowing nodes
following-siblingFollowing siblings
namespaceNamespaces
parentParent
precedingPreceding nodes
preceding-siblingPreceding siblings
selfCurrent node

Functions

FunctionDescription
count()Count nodes
sum()Sum values
string-length()String length
contains()Contains text
starts-with()Starts with
substring()Substring
normalize-space()Trim whitespace
translate()Translate chars

Mini Practice

  1. Write XPath with predicates
  2. Use different axes
  3. Apply functions
  4. Combine expressions

Up Next

Continue with Entity Reference — XML entity reference.

Related Topics

Frequently Asked Questions about XPath Reference

What is XPath Reference in XML?

XPath Reference is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn XPath Reference?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn XPath Reference.

Why is XPath Reference important in XML?

XPath Reference is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.