Powershell - XPath - SUM() and Count() Child Nodes - XPath Expression Only



New to Xpath, intermediate with powershell. Spent a good amount of time researching yesterday, I could not find a single example of xpath sum() or count() expressions within a powershell -XPath command. I'm writing this verbosely as my first stackoverflow question so that future people can find this article for a clear example answer from an XPert.


Objectives (2): 1. Count() the critical processes. 2. SUM() the transactions processed by critical processes.

Note: The output of the powershell XPath command should be the raw number.



<units>
<unit>
<unitName>Generic Process Unit</unitName>
<attribute>
<name>Process Type</name>
<value>Critical Process</value>
</attribute>
<attribute>
<name>Transactions Processed</name>
<value>10</value>
</attribute>
</unit>

<unit>
<unitName>Generic Process Unit</unitName>
<attribute>
<name>Process Type</name>
<value>Trivial Process</value>
</attribute>
<attribute>
<name>Transactions Processed</name>
<value>7</value>
</attribute>
</unit>

<unit>
<unitName>Generic Process Unit</unitName>
<attribute>
<name>Process Type</name>
<value>Critical Process</value>
</attribute>
<attribute>
<name>Transactions Processed</name>
<value>5</value>
</attribute>
</unit>
</units>


My Best attempt at the SUM function. Didn't even try the Count:



select-xml -path mine.xml -XPath '//attribute[value="Critical Process"]/../attribute[name="Transactions Processed"][sum(value)]' | Select-Object -ExpandProperty node | select Value


Errata: The reason it needs to be XPath only expressions, is that I'm only using powershell to construct and validate my XPath strings. The end goal is to put those strings into a non-powershell software that will actually use the XPath string. That software gives me no way to validate an XPath, and it takes 20 minutes to tell if it works after I put it in, so I'm using powershell as my testbed.


I've changed the item names, descriptions, and removed attributes to make the example simpler. However, I don't know if the "schema" words are important, so here is an example of an original "value" line.



<value xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xs="http://ift.tt/tphNwY" xsi:type="xs:string">Critical Process</value>


Issues:




  • It seems that a given XPath expression needs to be tweaked just a bit to work in powershell. For example, after -XPath '/xpath/node' you seem to always need to end with: '/text()' | Select-Object -ExpandProperty node | select Value to actually see the output you want.




  • Microsoft has XPath documentation, and Powershell documentation, but no "XPath Expressions in Powershell" documentation.




  • Most internet examples of expressions use "Powershell + XPath" do bare minimum XPath to get data object out, and then loop through using foreach in powershell.




  • It looks like the command syntax might have some dependency on XPath versions 1, 2, etc.




Thanks in advance.


No comments:

Post a Comment