XPath

1. What is XPath

XPath (XML Path Language) is an XML‑oriented path language. Purpose:
Use path expressions to query nodes, node text and node attributes within XML/HTML documents, with built‑in conditional filtering capabilities.

Key point: HtmlAgilityPack parses HTML documents into an XML‑style DOM, so you can directly use XPath to scrape webpage content.

2. Sample XML Reference

<?xml version="1.0" encoding="utf-8"?>
<WebInfos>
    <WebInfo>
        <Name>FoxDevelop</Name>
        <Domain>https://foxdevelop.com</Domain>
        <IP type="ipv4">127.0.0.1</IP>
        <Age>1</Age>
    </WebInfo>
    <WebInfo>
        <Name>google</Name>
        <Domain>https://www.google.com</Domain>
        <IP type="ipv4">95.0.0.1</IP>
        <Age>23</Age>
    </WebInfo>
    <WebInfo>
        <Name>microsoft</Name>
        <Domain>https://www.microsoft.com</Domain>
        <IP type="ipv4">192.186.0.1</IP>
        <Age>13</Age>
    </WebInfo>
</WebInfos>Code language: HTML, XML (xml)

3. XPath Syntax Breakdown

Basic Symbol Reference
SymbolMeaning
/Start searching from the root node; acts as hierarchy separator
//Search anywhere in the document, ignoring nesting levels
[]Filter conditions or index; indexes start at 1 (not 0)
@Match element attributes
Step‑by‑step Examples
  1. /WebInfos
    Retrieve top‑level root node <WebInfos>
  2. /WebInfos/WebInfo[1]
    Fetch the 1st <WebInfo> node → FoxDevelop record

⚠️ XPath indexes begin at 1

  1. /WebInfos/WebInfo[Age>5]
    Filter WebInfo nodes whose child <Age> value is greater than 5
    Matches: Google (23), Microsoft (13)
  2. /WebInfos/WebInfo[Age>5]/Name
    From the filtered set above, extract child <Name> nodes
  3. /WebInfos/WebInfo[last()]
    last() function: get the last WebInfo entry → Microsoft
  4. /WebInfos/WebInfo[last()-1]
    Get the second‑last WebInfo entry → Google
  5. /WebInfos/WebInfo[position()<3]
    position() returns current node index; match index less than 3 → first two entries (FoxDevelop, Google)
  6. //IP[@type='ipv4']
    // global search; filter <IP> nodes where attribute type equals ipv4
  7. //@type
    Fetch all attributes named type across the whole document
  8. //WebInfo
    Globally locate all <WebInfo> nodes regardless of nesting depth

4. Useful Extended Patterns

//Name/text()              # Get inner text from Name tag
//WebInfo[Name="FoxDevelop"]     # Match node by text content
//IP/@type                 # Read value of type attribute on IP tagCode language: JSON / JSON with Comments (json)

5. Usage with HtmlAgilityPack

var doc = new HtmlDocument();
doc.Load("demo.xml");

// Select names of sites where Age>5
var nodes = doc.DocumentNode.SelectNodes(@"/WebInfos/WebInfo[Age>5]/Name");
foreach(var node in nodes)
{
    Console.WriteLine(node.InnerText);
}Code language: PHP (php)

XPath

Leave a Reply

Your email address will not be published. Required fields are marked *