xml - 比较Powershell中同一父节点的两个xml元素

标签 xml powershell

<department>
    <employee name="ABC">
    <salary value="basic">5000</salary>
    <salary value="allowance">8000</salary>
    </employee>
    <employee name="XYZ">
    <salary value="basic">6000</salary>
    <salary value="allowance">3000</salary>
    </employee>
    </department>

如何在Powershell中获得津贴额大于基本津贴的雇员姓名。

我在下面尝试过,但没有给我结果
$result= $xml |Select-Xml '//department'| 
ForEach{
$basic=$_.Node.employee.salary.value | where {$_.Node.employee.salary.value -eq 'basic'}
$allowance=$_.Node.employee.salary.value | where {$_.Node.employee.salary.value -eq 'allowance'}
if ($allowance -ge $basic)
{
$name=$_.Node.employee.name
}
}
echo $result

最佳答案

如果要使用Select-Xml,可以执行以下操作:

$node = $xml | Select-Xml -XPath "//department/employee[./salary[@value='basic'] < ./salary[@value='allowance']]"
$node.node.name # Output the employee name

有关xpath表达式示例,请参见XPath Examples。有关XPath运算符的更多信息,请参见Boolean, Comparison, and Set Expressions,包括在必要时转义<>

关于xml - 比较Powershell中同一父节点的两个xml元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56913630/

相关文章:

Android 应用程序始终保持横向

powershell - 使用 -File 参数调用时,如何让 powershell 返回正确的退出代码?

php - 在 PHP 中访问 SimpleXMLElement 中的 @attributes 数据

html - Rails 应用程序上的奇怪错误,仅在一个浏览器上,一个 url "Below is a rendering of the page up to the first error."

java - 通过 javax.xml.bind 进行 XML 序列化

python - 如何使用 ElementTree 注释掉 XML <Tag>

postgresql - 未提供密码/未找到位置参数 PostgreSQL - Powershell

ruby - ruby函数 'powershell'在哪里定义的?

sql-server - 如何使用powershell验证SQL server实例是否正确?

powershell - 从 PowerShell 打开 Notepad++