2

私は 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 を 2 回選択してはなりません。

4

3 に答える 3

1

XPath/Cars/Car[Colors/Color/Effects/Effect = 'Saturated']/Nameで行う必要があります。

于 2013-01-16T14:20:44.717 に答える
1

使用:

/*/Car[Colors/Color/Effects/Effect = 'Saturated'
     and
       not(Name = preceding-sibling::Car[Colors/Color/Effects/Effect = 'Saturated']/Name)
      ]
于 2013-01-16T14:21:09.283 に答える
0

私は自分でxpathを学ぼうとしているので、これは完璧ではないかもしれませんが、試すことができます:

$xml = [xml] (Get-Content C:\test.xml)
$names = $xml.SelectNodes('/Cars/Car[Colors/Color/Effects/Effect="Saturated"]') | Select-Object -ExpandProperty Name

またはSelect-XML、あなたが尋ねたように使用するには、代わりにこれを使用します:

$names = Select-Xml -Xml $xml -XPath '/Cars/Car[Colors/Color/Effects/Effect="Saturated"]/Name') | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty '#text'

名前を文字列として$names変数に抽出します。

于 2013-01-16T14:21:40.000 に答える