liveBook · Manning

XPath

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

An XPath query is an expression defined by the XPath language for selecting nodes in an XML or HTML document tree.

XPath queries select nodes from an HTML document after ConvertFrom-Html turns a web response into a navigable object. They make web scraping practical when the needed data is buried in search results or detail pages rather than exposed by a public backend. section 13.3 section 13.3.1

Start from the browser’s inspected element, but do not rely on a copied XPath whose id changes for every result. Compare equivalent elements and build a query from attributes that remain shared, such as class and href. Pass that query to SelectNodes to return every matching link; use SelectSingleNode when one identified detail-page field is required. section 13.3.1

$xPath = '//*[' +
    '@class="contentTextItemSpacerNoBreakLink" ' +
    'and @href="javascript:void(0);"]'
$Search.SelectNodes($xPath) | Format-Table NodeType, Name, Id, InnerText
NodeType Name AttributeCount ChildNodeCount ContentLength InnerText
-------- ---- -------------- -------------- ------------- ---------
Element  a    4              1              144           ...
Element  a    4              1              148           ...
Element  a    4              1              148           ...

This query matches result links by their shared class and href instead of a result-specific id, returning all matching elements for later processing. section 13.3.1

A repeatable scraping flow is:

  • Build a search URL and convert its HTML response into a document object. section 13.3.1
  • Select matching result links with XPath, then extract and normalize each link id. section 13.3.1
  • Request each detail page and select fields such as architecture, supported products, or supersedence information. section 13.3.1
  • Traverse selected child elements when the desired values are nested, then turn the results into structured objects. section 13.3.1

Finding update supersedence

For a KB search with multiple results, select all result anchors by shared attributes, remove the _link suffix from each id, and use the resulting id to request its detail page. A node selected by supersededbyInfo can then be traversed through div and a elements to collect superseding KB titles and identifiers. section 13.3.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Demonstrates browser-assisted query design, SelectNodes, SelectSingleNode, and nested-element traversal without teaching XPath language syntax broadly.
Examples
Uses Microsoft Update Catalog KB4521858 results, dynamic link ids, architecture, supported products, and supersedence links.
What it assumes
Assumes PowerShell web-request usage while avoiding a prior requirement to write XPath queries.
What you can do after
Apply it via PowerHTML to select result links and detail-page values from converted HTML.
How it connects
Connects XPath with PowerHTML, ConvertFrom-Html, SelectNodes, SelectSingleNode, Invoke-WebRequest, and web scraping.

Also explained in

Compare how 3 books explain XPath →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.