DevOps | Scripts | Automation

PowershellXML

How to use XML xPath in PowerShell – Part-1?

In this article, I will explain how the xPath works in PowerShell to filter out the XML file to get the nodes and their attributes.

To summarize better we have compared the dot (.) method for properties and the xPath method and at the end of the article, you can understand how using xPath makes your life simple when you work with XML files.

Consider the Books XML file from the MS website for reference. We have just used a small portion of it as shown below.

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

Before going into detail, please read the below W3School document on the XPath cheat sheet to understand better how to apply it in PowerShell.

https://www.w3schools.com/xml/xpath_syntax.asp

First of all, we will check how we can access properties using the Dot(.) method and then with the xPath. Once we start using the xPath, you will understand how it is easy to access XML files with xPath and browse the nodes.

Alright, Now open PowerShell and load the XML file into the variable.

$booksXml = [xml] (Get-Content C:\Temp\books.xml)

Let’s check the $booksXml variable output.

Variable output

Dot (.) method

You can check these two properties are from the XML variable.

$booksXml | Get-Member -MemberType Property
Get-Member property

You can access the above properties with the dot method (.) shown below.

$booksXml.catalog
Accessing property with dot method.

Further accessing properties,

$booksXml.catalog.book
access properties

In the above output, there are multiple properties (id, author, title, etc) available for each book. If you need to filter out the data, let’s say you need the book name with id=bk102 then you need to use the where filter.

$booksXml.catalog.book | where{$_.id -eq 'bk102'}
Filter property

This seems little tedious work here because we need to know where exactly point we need to stop and search for properties. i.e. in the above example, we need to come down to “catalog.book” and then we had to filter the property.

Then what about the larger XML file, we need to check for the node we need to filter, and then we can select the property.

xPath Method

Here, the xPath comes into the picture. With the xPath method, we can easily browse through the nodes. Just we need to remember the below two functions to search properties.

  • SelectSingleNode()
  • SelectNodes()

The SelectSingleNode() function gets the Node and attributes of the single node while SelectNodes() function is used to filter multiple nodes and retrieve their attributes.

If you haven’t gone through the link yet, read it again for a better understanding of how to access the XML data using xPath.

https://www.w3schools.com/xml/xpath_syntax.asp

Dot (.) – Get the current Node

To get the property of the parent node, use the xPath dot (.) property.

$booksXml.SelectNodes('.')
Get the Current Node
$booksXml.catalog.SelectNodes('.')
Double Dot (..) – Get the parent Node

To get the parent node from the current node use the double dot (..) property.

In the previous example, if we use the double dot then it will give us the parent node.

$booksXml.catalog.SelectNodes('..')
Get the parent Node
Slash (‘/’) – Select the node from the root node

With the ‘/’ xPath method, you can select the node from the root node. For example,

$booksXml.SelectNodes('/')
$booksXml.SelectNodes('/catalog')
Select the single node.

The first example will get the parent node. The second example will select the catalog node from the root node. If we try to fetch the book node from the root node then there won’t be any output.

PS C:\> $booksXml.SelectNodes('/book')

To select the book node, you can use the command,

PS C:\> $booksXml.SelectNodes('/catalog/book')
book Node output

If you need only the one node, then use the SelectSingleNode property.

$booksXml.SelectSingleNode('/catalog/book')

By default the above command selects the first node. You can use the Array definition to select the specific node but in the xPath, array index starts from 1,2,3… so on.

$booksXml.SelectSingleNode('/catalog/book[1]')
$booksXml.SelectSingleNode('/catalog/book[2]')

We have continued with the other xPath methods in the next article.

https://theautomationcode.com/how-to-use-xml-xpath-in-powershell—part-2

Loading

2 thoughts on “How to use XML xPath in PowerShell – Part-1?

Comments are closed.