Master W3 XPath Axes: The Complete Guide

XPath, a fundamental part of web scraping and DOM manipulation, is like the language of the internet. It's a powerful tool that helpsdevelopers select specific elements, attributes, and values within an XML document or HTML page. At the heart of XPath lies a concept called axes. Think of axes as navigating roads that lead you from one node to another in the XML or HTML document tree.

two large axes sitting on top of a tree stump
two large axes sitting on top of a tree stump

XPath axes define the relationship between two nodes in the document tree. They guide your selection throughücktive, parent, child, ancestor, descendant, and more. Moreover, they form the backbone of XPath expressions, enabling you to traverse and query XML or HTML documents efficiently.

Green Dragon Bone Axe
Green Dragon Bone Axe

Types of XPath Axes

XPath axes come in several types. Each serves a unique purpose in navigating the document tree.

Cool Hatchet Designs, Cool Axes, Unique Axes, Cool Looking Axes, Cool Battle Axes, Metallic Axes Design, Epic Axes, Handcrafted Metal Axes, Handcrafted Metal Ax
Cool Hatchet Designs, Cool Axes, Unique Axes, Cool Looking Axes, Cool Battle Axes, Metallic Axes Design, Epic Axes, Handcrafted Metal Axes, Handcrafted Metal Ax

Let's delve into the primary axes and understand their roles:

Ancestor Axis

Fantasy Battle Axes, Greataxe Art, Axes, Crossed Fantasy Axes, Fantasy Ax, Metallic Axes Design, Two Fantasy Battle Axes, Fantasy Battle Axes Design, Fantasy Double Axes Design
Fantasy Battle Axes, Greataxe Art, Axes, Crossed Fantasy Axes, Fantasy Ax, Metallic Axes Design, Two Fantasy Battle Axes, Fantasy Battle Axes Design, Fantasy Double Axes Design

The ancestor axis moves up the tree, selecting all the ancestors of the context node. Consider the following HTML snippet:

<div> <p> This is a paragraph. </p> </div>

If you want to select the 'div' element, you'd use /div/preceding-sibling::p as the XPath expression.

Video Game Axes, Types Of Swords, Tactical Gear Loadout, Tactical Equipment, Cool Swords, Anime Dragon Ball Super, Knives And Swords, Medieval Fantasy, Fantasy Character Design
Video Game Axes, Types Of Swords, Tactical Gear Loadout, Tactical Equipment, Cool Swords, Anime Dragon Ball Super, Knives And Swords, Medieval Fantasy, Fantasy Character Design

Descendant Axis

Herein, you move down the tree, picking all the descendants of the context node. For example,

<div> <p> This is a paragraph. </p> </div>

Axe_Weapon, Prince Luthra
Axe_Weapon, Prince Luthra

To select the 'p' element, you'd use //div/p as the XPath expression.

Following Axis

Viking Axes, Viking Hatchet, Viking Armor, Viking Battle, New Year Art, Norse Symbols, Viking Symbols, Home Garden Design, Cool Knives
Viking Axes, Viking Hatchet, Viking Armor, Viking Battle, New Year Art, Norse Symbols, Viking Symbols, Home Garden Design, Cool Knives
axe Four Different Types Of Axes, How To Draw Axes, Axes Design, Fantasy Axes Design Drawing, Axes Drawing, Sketch Of Multiple Axes, Illustration Of Various Axes, Battle Ax Drawing, Assorted Fantasy Battle Axes
axe Four Different Types Of Axes, How To Draw Axes, Axes Design, Fantasy Axes Design Drawing, Axes Drawing, Sketch Of Multiple Axes, Illustration Of Various Axes, Battle Ax Drawing, Assorted Fantasy Battle Axes
WAR AXE COMMON Ancient Axes, Medieval Axes, Medieval Ax, Two Axes Warrior, Medieval Executioner Ax, Medieval Battle Axes, Ornate Medieval Axes, Ancient Viking Battle Ax, Medieval Battle Axes Real
WAR AXE COMMON Ancient Axes, Medieval Axes, Medieval Ax, Two Axes Warrior, Medieval Executioner Ax, Medieval Battle Axes, Ornate Medieval Axes, Ancient Viking Battle Ax, Medieval Battle Axes Real
Zenithar's Axe
Zenithar's Axe
two wooden axes are sitting on top of a tree stump
two wooden axes are sitting on top of a tree stump
Throwing axes
Throwing axes
Special Camping And Hiking Hatchet, Professional Viking Throwing Axes, Handmade Wooden Handled Axes, Custom Axes For Outdoor Use, Hand Forged Axes/tomahawks, Handmade Wooden Handled Ax, Metal Throwing Axes Set, Best Made Co. Axes, Handcrafted Wooden Handle Ax
Special Camping And Hiking Hatchet, Professional Viking Throwing Axes, Handmade Wooden Handled Axes, Custom Axes For Outdoor Use, Hand Forged Axes/tomahawks, Handmade Wooden Handled Ax, Metal Throwing Axes Set, Best Made Co. Axes, Handcrafted Wooden Handle Ax
two large axes sitting on top of a wooden table
two large axes sitting on top of a wooden table
two large axes with wooden handles on them
two large axes with wooden handles on them
Anica kmet, custom axe
Anica kmet, custom axe
two large axes are laying on the floor next to each other with leather sheaths
two large axes are laying on the floor next to each other with leather sheaths
Decorative Battle Axe, Fantasy Battle Axe, Fantasy Cosplay Ideas, Medieval Themed Decor Inspiration, Medieval Weapon Design, Anime Axes, Battle Ax Drawing, Big Axes, Concept Art Of Battleaxe
Decorative Battle Axe, Fantasy Battle Axe, Fantasy Cosplay Ideas, Medieval Themed Decor Inspiration, Medieval Weapon Design, Anime Axes, Battle Ax Drawing, Big Axes, Concept Art Of Battleaxe
two large metal axes sitting next to each other
two large metal axes sitting next to each other
two large axes are next to each other
two large axes are next to each other
Handcrafted Viking Double-Head Axe – Norse Celtic Engraved Battle Axe
Handcrafted Viking Double-Head Axe – Norse Celtic Engraved Battle Axe
Axe Stone Greataxe, Crossed Fantasy Axes, Fantasy Ax, Battle Ready Medieval Axes, Battle Ax Drawing, Metallic Axes Design, Two Fantasy Battle Axes, Fantasy Battle Axes Design, Fantasy Double Axes Design
Axe Stone Greataxe, Crossed Fantasy Axes, Fantasy Ax, Battle Ready Medieval Axes, Battle Ax Drawing, Metallic Axes Design, Two Fantasy Battle Axes, Fantasy Battle Axes Design, Fantasy Double Axes Design
Two Decorative Axes On Dark Background, Hand Forged, Leather Sheath, Southwestknives Etsy, Thoughtful Gifts For Him, Ax And Hatchet On Wood, Imran Khan, Diy Fashion Hacks, Decorative Viking Axes On Wood
Two Decorative Axes On Dark Background, Hand Forged, Leather Sheath, Southwestknives Etsy, Thoughtful Gifts For Him, Ax And Hatchet On Wood, Imran Khan, Diy Fashion Hacks, Decorative Viking Axes On Wood
Greataxe Art, Dnd Axes, Dwarven Halberd, Viking Greataxe, Dwarven Axes, Dwarven Greataxe, Dwarven Battleaxe, Fantasy Axes, Fantasy Axes Design
Greataxe Art, Dnd Axes, Dwarven Halberd, Viking Greataxe, Dwarven Axes, Dwarven Greataxe, Dwarven Battleaxe, Fantasy Axes, Fantasy Axes Design
Refurbished Double Bit with sheath Vintage Hatchet With Sheath, Double Bit Hatchet, Double Bit Axes, Two Axes With Leather Sheaths, Vintage Ax With Leather Sheath, Vintage Axes With Leather Sheaths, Crossed Axes With Leather Sheaths, Two Wooden Handled Axes, Double Sided Axes
Refurbished Double Bit with sheath Vintage Hatchet With Sheath, Double Bit Hatchet, Double Bit Axes, Two Axes With Leather Sheaths, Vintage Ax With Leather Sheath, Vintage Axes With Leather Sheaths, Crossed Axes With Leather Sheaths, Two Wooden Handled Axes, Double Sided Axes
Greataxe Drawing, Fantasy Greataxe, Cool Looking Axes, Big Axes, Giant Ax, Battle Ax Drawing, Sketch Of Two Fantasy Axes, Fantasy Great Axes Design, Metallic Axes Design
Greataxe Drawing, Fantasy Greataxe, Cool Looking Axes, Big Axes, Giant Ax, Battle Ax Drawing, Sketch Of Two Fantasy Axes, Fantasy Great Axes Design, Metallic Axes Design

Following axis moves forward but not upward in the document tree, selecting all nodes that come after the context node. Just like a path that leads from one point to another, without going back.

Preceding Axis

Preceding axis is the opposite of following axis. It selects all nodes that come before the context node in the document tree.

Attribute Access

XPath also allows you to access attributes of an element. This can be done using the @ symbol.

For instance, to select the 'href' attribute of an 'a' tag, you can use the expression: /a/@href.

Familiarizing yourself with these XPath axes and attribute access functionalities is essential. They form the bedrock of 90% of XPath expressions you'll encounter. Practice them, and you'll soon be navigating HTML and XML documents like a professional developer.

Lastly, never stop learning. The world of web scraping and DOM manipulation is vast and ever-evolving. Stay updated, and keep expanding your skillset. Happy coding!

Related Articles

Urllib Python 3 Example Is Python Interpreted Or Compiled Python 3 With Open File Python Opencv Tutorial For Beginners W3school Yaml Python 3.11 Vs Python 3.12 Wxpython Example Gui Which Function Is Used To Get User Input In Python 3 Python Online W3 School Upper () Python