PowerShell XML XPath Examples - 2

In the last article, we gave a simple introduction on Select-XML cmdlet  and its importance in querying XML data in Powershell. We explained this cmdlet with basic XPath example, with our sample XML. 

This tutorial focuses on some advanced examples of this cmdlet. We will use the same XML that we used in the last scenario. So, make sure you have a copy of the XML and you have loaded the XML in Powershell. You may not get the right results if you have not loaded the XML properly. So, make sure you follow the steps right. We will see two more interesting ways to use Select-XML cmdlet in this post.

Select-Xml Query Specific Node in PowerShell


In the previous example, we managed to get all the values listed by using foreach cmdlet. That throws an interesting question. How to get all department name (dname) in the XML as a list, instead of getting the full set of values? Is there a way this can be done using Select-Xml cmdlet? The simple answer is yes. And doing this is much simpler. Here the code fragment that selects only the “dname” tag across the full XML:
PS C:\> Select-Xml -Content $xmltest -XPath /Details/department|
>> foreach {$_.node.dname }
>>
Administration
Marketing
Purchasing
Human Resources
Shipping
PS C:\>

As expected, we were able to select specific node name across the full XML in the output. Handy!.

XML – Add Where Clause in PowerShell – Where-Object Cmdlet


We are going to discuss an advanced option here. How to get all the nodes in the XML where the revenue is greater than 50, in PowerShell? You can do this easily by using Where-Object cmdlet in PowerShell. Note that if you are a super fluent XPath user, you can do this in XPath itself. We will show both the methods now.

Option 1: Using Where-Object Cmdlet – Example

The syntax and an example output is provided below: [ “where” clause is highlighted in yellow. We check if revenue is greater than 50 by using –gt tag.

PS C:\> Select-Xml -Content $xmltest -XPath /Details/department|
>> foreach {$_.node} | where { [double] ($_.revenue) -gt 50 }
>>


ID       : 10
dname    : Administration
manager  : 200
location : 1700
revenue  : 77

ID       : 30
dname    : Purchasing
manager  : 114
location : 1700
revenue  : 68

Option 2: Using XPath in PowerShell

You can also get the same output by framing your XPath bit more smartly. Here is how to get the same result in XPath. Note that we have put the condition directly in the XPath in this example.
PS C:\> Select-Xml -Content $xmltest `
>> -XPath '/Details/department[revenue >50 ]' |
>> foreach {$_.node}
>>


ID       : 10
dname    : Administration
manager  : 200
location : 1700
revenue  : 77

ID       : 30
dname    : Purchasing
manager  : 114
location : 1700
revenue  : 68

That completes part 2 of the tutorial on Select-XML cmdlet in Powershell. We introduced you to some advanced data selection methods in Powershell to query XML data. In the next example, we will discuss some more data extract approaches.  Meanwhile, if you have a question on the samples provided here, post it as a comment back to us.

No comments:

Post a Comment