2016-03-28 101 views
1

尽管我在线看到过这些示例,但似乎无法对PowerShell中的属性值执行XPath搜索。对PowerShell中属性值的XPath搜索

[xml]$xml = '<a><b><c foo="bar"></c></b></a>' 
$xml | select-xml -xpath //c[@foo] # This returns a node 
$xml | select-xml -xpath //c[@foo='bar'] # This does not 

我从来没有被如此简单的东西难住。 :-)我怎样才能使这个工作?

回答

1

如果引用的XPath也将正常工作:

[xml]$xml = '<a><b><c foo="bar"></c></b></a>' 
$xml | select-xml -xpath "//c[@foo='bar']" 

这可能是因为@ is the splat operator,所以它试图图示命名$foo一个(不存在的)变量。

其实这个解释是错误的。显然是因为单引号。

如果你试试这个:

Write-Host //c[@foo='bar'] 

你会看到输出是:

//c[@foo=bar] 

看来,这是因为PowerShell如何连接字符串。