首页 > 解决方案 > 使用 powershell 查找 XML 属性

问题描述

需要在 powershell 中使用 select -xml查找作业节点的Valid属性为trueJobLocation不是Country=England的所有作业名称

<Employee>
  <job id = "123" Name = "Teacher" Valid ="True">
    <jobdetails>
      <JobLocation location="City=London,Country=England" JobType="Permanent"/>
    </jobdetails>
  </job>
  <job id = "356" Name = "Doctor" Valid ="True">
    <jobdetails>
      <JobLocation location="City=Tokyo,Country=Japan" JobType="Permanent"/>
    </jobdetails>
  </job>
  <job id = "987" Name = "Banker" Valid ="True">
    <jobdetails>
      <JobLocation location="City=Manchester,Country=England" JobType="Permanent"/>
    </jobdetails>
  </job>
</Employee>

我尝试过的 XML 路径

$xml ='//Employee/job[@Valid="True"]/jobdetails*[@*[contains(.,!"Country=England")]]/job/@Id'



$xml ='//Employee/job[@Valid="True"]/jobdetails*[@*[contains(.JobLocation,!"Country=England")]]/job/@Id'

标签: xmlpowershellxpath

解决方案


一个 XPath 过滤器表达式(方括号中的文本)可以包含多个子句。要按子节点的值或属性进行过滤,只需将子节点或属性的相对路径作为第二个子句包含在过滤器表达式中,并使用适当的逻辑运算符连接子句。要按不包含特定子字符串的属性进行过滤,请使用not()andcontains()函数。

//Employee/job[@Valid="True" and jobdetails/JobLocation[not(contains(@location, "Country=England"))]]/@id

推荐阅读