xml - 如何根据子项选择元素

标签 xml powershell xpath

我不是 PowerShell 或 XPath 方面的专家,但我在如何解决这个看似简单的问题上遇到了一些困难。假设我有这个 XML 文档:

<?xml version="1.0" encoding="utf-8"?>
<Cars>
  <Car>
    <Name>Car1</Name>
    <Colors>
      <Color>
        <Name>Indian yellow</Name>
        <Effects>
          <Effect>Blur</Effect>
          <Effect>Shadow</Effect>
        </Effects>
      </Color>
      <Color>
        <Name>Fireapple red</Name>
        <Effects>
          <Effect>Shadow</Effect>
        </Effects>
      </Color>
    </Colors>
  </Car>
  <Car>
    <Name>Car2</Name>
    <Colors>
      <Color>
        <Name>Indian yellow</Name>
        <Effects>
          <Effect>Blur</Effect>
          <Effect>Shadow</Effect>
          <Effect>Saturated</Effect>
        </Effects>
      </Color>
      <Color>
        <Name>Chrome black</Name>
        <Effects>
          <Effect>Saturated</Effect>
        </Effects>
      </Color>
    </Colors>
  </Car>
  <Car>
    <Name>Car3</Name>
    <Colors>
      <Color>
        <Name>Indian yellow</Name>
        <Effects>
          <Effect>Shadow</Effect>
          <Effect>Saturated</Effect>
        </Effects>
      </Color>
      <Color>
        <Name>Fireapple red</Name>
        <Effects>
          <Effect>Saturated</Effect>
        </Effects>
      </Color>
    </Colors>
  </Car>
</Cars>

如何使用 Select-Xml 来选择例如颜色效果为“饱和”的汽车名称?请注意,我需要独特的汽车收藏,例如即使两种颜色都具有“饱和”效果,Car2 也不得选择两次。

最佳答案

XPath /Cars/Car[Colors/Color/Effects/Effect = 'Saturated']/Name 应该可以。

关于xml - 如何根据子项选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360367/

相关文章:

xml - 如何在没有空格的情况下分割Xml文件中的长行?

xml - 使用TXMLTransformProvider时控制XML节点的顺序

java - Powershell启动进程Java PassThru

xpath:如何选择第 n 个选项?

Xpath 与单引号匹配吗?

java - 为 HashMap selenium java 收集数据 - xpath 是什么?

ios - 在另一个类的一个 ViewController 的 TableView 上调用 reloadData 的正确方法是什么?

java - 如何删除 SOAPElement 中的前缀和命名空间?

powershell - 功能中的IF不起作用(PowerShell)

powershell - 无法使用 PowerShell 从远程服务器访问环境变量