$xW = [xml]@'
<W>
<X xID="1">
<Y yID="20">
<Z zID="201"/>
</Y>
<Y yID="30">
<Z zID="301"/>
<Z zID="302"/>
</Y>
</X>
</W>
'@
write-host First Y Node Z count = $xW.W.X.Y[0].Z.Count
write-host Second Y Node Z count = $xW.W.X.Y[1].Z.Count
The output from this is:
First Y Node Z count =
Second Y Node Z count = 2
I was expecting the first output line to indicate a count of 1.
What I'm trying to achieve is to output a csv file of the form
xID,yID,zID where zID is a null value if there are no Z child nodes,
or there are multiple xID,yID lines when more than one Z child node
exists in a Y node.
HC
See:
$xW.W.X.Y[0].Z.GetType()
$xW.W.X.Y[1].Z.GetType()
$xW.W.X.Y[1].Z | %{ $_.GetType() }
You can force it to be an array (remember to do it for index 1 if you
don't know how many children it has first):
write-host First Y Node Z count = ([Array]($xW.W.X.Y[0].Z)).Count
write-host Second Y Node Z count = ([Array]($xW.W.X.Y[1].Z)).Count
Or you can use Measure-Object:
write-host First Y Node Z count = ($xW.W.X.Y[0].Z | Measure-Object).Count
write-host Second Y Node Z count = ($xW.W.X.Y[1].Z | Measure-Object).Count
HTH
Chris
$xw.SelectNodes("/W/X/Y") | % { $_.createnavigator().evaluate("count(./Z)")}
1
2
And if you want the output labeled:
$xw.SelectNodes("/W/X/Y") | % { $i = 1 } { $count =
$_.createnavigator().evaluate("count(./Z)") ; "Y Node Z Count #{0} :
{1}" -f $i++,$count }
For some good reads on XML/XPATH & PowerShell:
http://www.pluralsight.com/community/blogs/dan/archive/2006/11/25/42506.aspx
http://www.pluralsight.com/community/blogs/dan/archive/2006/11/28/43561.aspx
http://powershell.com/cs/blogs/tobias/archive/2009/01/17/xml-part-1-playing-with-rss-feeds-and-xml-content.aspx
http://powershell.com/cs/blogs/tobias/archive/2009/02/02/xml-part-2-write-add-and-change-xml-data.aspx
HTH :)
--
v(^_^)~Clint
http://outputredirection.blogspot.com
The best thing I love about PowerShell is that it is such a great way to get
experience with concepts like XML/XPATH.
- Larry
PS> $xW.w.x.Y[0].haschildnodes
True
PS> $xW.w.x.Y[0].childnodes.count
1