2016-03-28 70 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如何連接字符串。