2012-05-08 54 views
1

所以我們可以說我有一個簡單的XML文檔是這樣的:的XPath //操作不工作

<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元素而不必指定完整路徑?

+0

這就是所有你需要得到的c節點。 –

+5

http://huddledmasses.org/xpath-and-namespaces-in-powershell/ – mzjn

+1

你顯然忘記了命名空間;-) – Joey

回答

1

元素位於命名空間中,因此您需要在該命名空間中搜索它們。谷歌的「XPath默認命名空間」,這是數字1常見問題。