从特定的XML字段中检索值

时间:2019-08-06 11:42:32

标签: powershell xpath

我有一个从Invoke-RestMethod收到的xml文件:

$request = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -ContentType $contentType -Body $body

XML:

<Body>
  <Response>
    <result>
      <resources>
        <resource>
          <id>012345</id>
          <rev>
            <value>x1</value>
            <ignore>false</ignore>
          </rev>
          <type>
            <value>Zw379E01-2362-48A3</value>
          </type>
          <attributes>
            <attribute>
              <id>
                <value>1B2C-3D4E-5G6H</value>
              </id>
              <name>ID</name>
              <values>
                <value>5B3CD8E2-746A-54993</value>
              </values>
            </attribute>
            <attribute>
              <id>
                <value>77BB84E18-F048-350CCC-E040</value>
              </id>
              <name>Updated</name>
              <values>
                <value>08.2019</value>
              </values>
            </attribute>
            <attribute>
              <id>
                <value>1111-2222-3333</value>
              </id>
              <name>Name</name>
              <values>
                <value>TEST</value>
              </values>
            </attribute>
          </attributes>
        </resource>
        <resource>
        ...
        </resource>
        <resource>
        ...
        </resource>
      </resources>
    </result>
  </Response>
</Body>

有几个“资源”块。 我需要在attribute.attribute.values.value = TEST

的情况下提取“ id”(Body.Response.result.resources.resource.id)

我可以通过以下方式提取“ id”的所有值:

$request.SelectNodes("//id")

但是我无法按照以下示例进行过滤:

$request.SelectNodes("//id") | ?{$_./attributes/attribute/values/value -eq "TEST"}

我将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:3)

您要在此处选择的是一个resource节点,其中attributes部分包含一个值为{attribute的{​​{1}},然后从所述资源中获取ID 。内部选择子句进入XPath过滤器中的"TEST"

[]
相关问题