Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell select-xml xpath doesn't seem to work

I'm trying to use select-xml to root out a few things from a SharePoint solution. I have a solution directory that contains a number of features, and rather than open each feature.xml and select names of features by hand and put them in a list, I was hoping to do the equivalent using powershell and select-xml.

My attempt went something like this:

ls -recurse -filter feature.xml | select-xml "/Feature"

and I got nothing, so I tried this:

ls -recurse -filter feature.xml | select-xml "//*"

which seemed to do what it was supposed to do. I got a list of every XML node in all the feature.xml files in my solution.

I tried XPath expressions like "//Feature" and "Feature", none of which got any results.

It seems to me that my XPath expressions are right, but the behavior of select-xml is a bit bewildering. Anyone know why this might be happening?

like image 462
Ben Collins Avatar asked Nov 29 '10 22:11

Ben Collins


2 Answers

Even a "default" namespace has to be specified to Select-Xml e.g.:

$ns = @{dns = 'http://schemas.microsoft.com/sharepoint/'}
ls . -r feature.xml | Select-Xml '//dns:Feature' -Namespace $ns
like image 61
Keith Hill Avatar answered Nov 04 '22 22:11

Keith Hill


Maybe the issue is with the xml namespace. Try using the -namespace to get the correct xpath query.

Test this by removing the xmlns from a feature.xml file and running your command.

like image 29
Nat Avatar answered Nov 04 '22 22:11

Nat