我有我正在查询的XML文档。从下面的示例$Match = TRUE中,当用户有密码时,状态= "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"}但我不知道如何将两者结合起来,只有当这三个值都存在和/或存在时,条件才是正确的。
我尝试过将脚本与-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"})发布于 2016-10-13 16:58:15
我喜欢将文档读入XML对象来启动。这样,您就不必多次迭代该文件。然后,可以在XPath函数中使用SelectNodes()表达式。只需对两个不同的-and调用进行-and,就可以得到结果。就像这样:
$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 0https://stackoverflow.com/questions/40025331
复制相似问题