Processing XML with PowerShell
Dan Sullivan did an in depth and interesting article about : Processing XML with PowerShell
for reference I wanted to add some simple examples using more interactive powershell functionality to do this in this post, ending with creating a simple 2 line function that works the some as Dan's first Xeval example, so we can do this :
invoke-XpathExpression -xml (type gl.xml) -exp "sum(GroceryList/Item/Price)"
to follow this blogpost it is handy to first read Dan's post, this examples are to show how you can do the interactive testing as shown with Xeval with only the XML adapter :
I start with making the same inputfile as Dan used and by doing it first interactively then turning it into a simple function :
# Make variable containing XML
$gl = @'
<GroceryList>
<Item>
<Dept>Produce</Dept><Name>Orange</Name><Price>3.20</Price>
</Item>
<Item>
<Dept>Meat</Dept><Name>Steak</Name><Price>13.20</Price>
</Item>
<Item>
<Dept>Produce</Dept><Name>Lettuce</Name><Price>1.34</Price>
</Item>
<Item>
<Dept>Meat</Dept><Name>Ham</Name><Price>11.41</Price>
</Item>
</GroceryList>
'@
# Convert to XML
$xgl = [xml]$gl
# Create Xpath navigator
$xn = $xgl.PSBase.CreateNavigator()
$xn.GetType()
# Evaluate Expression
$xn.Evaluate("sum(GroceryList/Item/Price)")
# Save the XML file for next example
Set-Content gl.xml $gl
# function to invoke an XpathExpression
Function invoke-XpathExpression ([xml]$xml,$expression) {
$xn = $xml.PSBase.CreateNavigator()
$xn.Evaluate($expression)
}
# Example using the function
invoke-XpathExpression -xml (type gl.xml) -exp "sum(GroceryList/Item/Price)"
The following is the result pasted into a console session :
PS I:\PowerShell> # Make variable containing XML
PS I:\PowerShell>
PS I:\PowerShell> $gl = @'
>> <GroceryList>
>> <Item>
>> <Dept>Produce</Dept><Name>Orange</Name><Price>3.20</Price>
>> </Item>
>> <Item>
>> <Dept>Meat</Dept><Name>Steak</Name><Price>13.20</Price>
>> </Item>
>> <Item>
>> <Dept>Produce</Dept><Name>Lettuce</Name><Price>1.34</Price>
>> </Item>
>> <Item>
>> <Dept>Meat</Dept><Name>Ham</Name><Price>11.41</Price>
>> </Item>
>> </GroceryList>
>> '@
>>
PS I:\PowerShell>
PS I:\PowerShell> # Convert to XML
PS I:\PowerShell>
PS I:\PowerShell> $xgl = [xml]$gl
PS I:\PowerShell>
PS I:\PowerShell> # Create Xpath navigator
PS I:\PowerShell>
PS I:\PowerShell> $xn = $xgl.PSBase.CreateNavigator()
PS I:\PowerShell> $xn.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
False False DocumentXPathNavigator System.Xml.XPath.XPathNavigator
PS I:\PowerShell>
PS I:\PowerShell> # Evaluate the Expression
PS I:\PowerShell>
PS I:\PowerShell> $xn.Evaluate("sum(GroceryList/Item/Price)")
29.15
PS I:\PowerShell>
PS I:\PowerShell> # Save the XML file for next example
PS I:\PowerShell>
PS I:\PowerShell> Set-Content gl.xml $gl
PS I:\PowerShell>
PS I:\PowerShell> # function to invoke an XpathExpression
PS I:\PowerShell>
PS I:\PowerShell> Function invoke-XpathExpression ([xml]$xml,$expression) {
>> $xn = $xml.PSBase.CreateNavigator()
>> $xn.Evaluate($expression)
>> }
>>
PS I:\PowerShell> # Example using the function
PS I:\PowerShell>
PS I:\PowerShell> invoke-XpathExpression -xml (type gl.xml) -exp "sum(GroceryList/Item/Price)"
29.15
Enjoy,
Greetings /\/\o\/\/