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
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
Types of XPath Axes
XPath axes come in several types. Each serves a unique purpose in navigating the document tree.
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
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
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
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 Knivesaxe 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 AxesWAR 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 RealZenithar's Axetwo wooden axes are sitting on top of a tree stumpThrowing axesSpecial 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 Axtwo large axes sitting on top of a wooden tabletwo large axes with wooden handles on themAnica kmet, custom axetwo large axes are laying on the floor next to each other with leather sheathsDecorative 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 Battleaxetwo large metal axes sitting next to each othertwo large axes are next to each otherHandcrafted Viking Double-Head Axe – Norse Celtic Engraved Battle AxeAxe 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 DesignTwo 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 WoodGreataxe Art, Dnd Axes, Dwarven Halberd, Viking Greataxe, Dwarven Axes, Dwarven Greataxe, Dwarven Battleaxe, Fantasy Axes, Fantasy Axes DesignRefurbished 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 AxesGreataxe 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!