xml - 使用 powershell 查找 XML 属性
问题描述
需要在 powershell 中使用 select -xml查找作业节点的Valid属性为true且JobLocation不是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'
解决方案
一个 XPath 过滤器表达式(方括号中的文本)可以包含多个子句。要按子节点的值或属性进行过滤,只需将子节点或属性的相对路径作为第二个子句包含在过滤器表达式中,并使用适当的逻辑运算符连接子句。要按不包含特定子字符串的属性进行过滤,请使用not()
andcontains()
函数。
//Employee/job[@Valid="True" and jobdetails/JobLocation[not(contains(@location, "Country=England"))]]/@id
推荐阅读
- c++ - 在这种情况下如何添加计数器?
- flutter - Flutter“RangeError(索引):无效值:有效值范围为空:0与WidgetSpan
- javascript - 使用 JavaScript 根据 URL 预填充 HTML 表中的三列一起过滤
- azure-container-instances - 将映像从 Azure 容器注册表拉入 Azure 容器实例时,如何使用 SystemAssigned 标识?
- c# - CS1729 'MyNavigationPage' 不包含带有 1 个参数的构造函数 --> 在 Visual Studio 2019 社区版中
- django - Django 频道测试奇怪的数据库行为
- sql - 使用多个表的 SQL 选择命令问题
- pyspark - 在 (py)spark 上创建一个复杂的数据框
- c# - 如何使用 C# 检查 Google Cloud Storage 中是否存在对象?
- python-3.x - Systemd 脚本未使用 config.ini 运行 python 脚本