12

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?

Ben Collins
  • 20,538
  • 18
  • 127
  • 187
  • 4
    If your "feature.xml" follows [this](http://msdn.microsoft.com/en-us/library/ms475601.aspx) documentation, you should use a prefix bound to the namespace URI `http://schemas.microsoft.com/sharepoint/` for that QName test. Otherwise: `//*[local-name()='Feature']` –  Nov 29 '10 at 23:35
  • @Alejandro: the feature xml just has an attribute xmlns="http://schemas.microsoft.com/sharepoint/". I'm no expert, but doesn't that set the default namespace? I thought if the default namespace was set, XPath would not need an additional specification for it. – Ben Collins Nov 30 '10 at 00:30
  • 4
    No. That is a FAQ: a QName test without prefix selects element under null (or empty) namespace URI. –  Nov 30 '10 at 00:56

2 Answers2

11

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
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
8

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.

Nat
  • 14,175
  • 5
  • 41
  • 64