XPath //运算符不工作

时间:2012-05-08 15:48:43

标签: xml powershell xpath

所以,假设我有一个这样的简单XML文档:

<a>
  <b>
   <c>blah</c>
   <c>blah</c>
   <d>blargh</d>
  </b>
</a>

我想选择“c”元素。

以下是我认为应该有效的方法:

Select-Xml -Xml $XmlDoc -XPath '//c';

虽然这个例子本身有效,但是这个类似的概念并没有在这个XML文档中返回任何内容:

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Copyright>Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
  <BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri>
  <StatusCode>200</StatusCode>
  <StatusDescription>OK</StatusDescription>
  <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
  <TraceId>985236ecd4b04793ac26f72a218a3876|LAXM001505|02.00.127.100|</TraceId>
  <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
      <Resources>
        <TrafficIncident>
          <Point>
            <Latitude>41.82048</Latitude>
            <Longitude>-88.20378</Longitude>
          </Point>
          <IncidentId>1</IncidentId>
          <LastModifiedUTC>2012-05-01T16:17:15.663Z</LastModifiedUTC>
          <StartTimeUTC>2011-05-01T19:46:00Z</StartTimeUTC>
          <EndTimeUTC>2012-11-11T14:00:00Z</EndTimeUTC>
          <Type>Construction</Type>
          <Severity>Minor</Severity>
          <Verified>true</Verified>
          <RoadClosed>false</RoadClosed>
          <Description>description here</Description>
          <DetourInfo />
          <LaneInfo>lane blockages possible</LaneInfo>
          <CongestionInfo />
        </TrafficIncident>
      </Resources>
    </ResourceSet>
  </ResourceSets>
</Response>

有没有人知道选择TrafficIncident元素的正确语法而不必指定完整路径?

1 个答案:

答案 0 :(得分:1)

元素位于命名空间中,因此您需要在该命名空间中搜索它们。 Google为“XPath默认命名空间”,它是排名第一的常见问题解答。