簡體   English   中英

在同一XML中查詢多個XML屬性

[英]Querying for multiple XML attributes in the same XML

我有要查詢的XML文檔。 在下面的示例XML中,當用戶具有密碼時, $Match = TRUE ,並且AND STATUS =“ a”和ACTION =“ h”。 這些都在相同的XML中。

<BusinessUnitList>
  <User id="1407210" loginName="1407210" password="Password1" statusCode="a">


<WorkStatusList>
  <WorkStatus start="2016-10-13" status="a" action="h" /> 

我可以查詢一個或另一個屬性:

$Match = (gc "c:\file.xml | Select-Xml -XPATH "//User" | select -ExpandProperty node | where {$_.password})
$Match2 = (gc "c:\file.xml | Select-Xml -XPATH "//WorkStatus" | select -ExpandProperty node | where {$_.status -eq "a" -and $_.action -eq "h"}

但是我不知道如何將兩者結合起來,因此只有當所有三個值都存在和/或存在時,條件才為TRUE。

我嘗試將腳本與-and組合使用,但無濟於事。

$Match = (gc "c:\file.xml | Select-Xml -XPath "//User" | select -ExpandProperty node | where {$_.password}) -and
         (gc "c:\file.xml | Select-Xml -XPath "//WorkStatus" | select -ExpandProperty node | where {$_.status -eq "a" -and $_.action -eq "h"})

我喜歡將文檔讀入XML對象開始。 這樣,您就不必多次迭代文件。 然后,可以在SelectNodes()函數內部使用XPath表達式。 只需-and兩個不同的SelectNode()調用,就可以得到結果。 像這樣:

$xml = [XML] (gc c:\file.xml)
$Match = $xml.SelectNodes("//WorkStatus[@status='a'and @action='h']").Count -gt 0 -and $xml.SelectNodes("//User[@password]").Count -gt 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM