2013-09-27 147 views
3

我有以下代碼:如何使用Xpath定位此元素?

<g xmlns="http://www.w3.org/2000/svg" id="MainMenu1" display="block" transform="translate(1125, 0)"> 
    <g dp:id="Content" xmlns:dp="http://dreampark.se/extensions" width="375" height="624" id="MainMenuViewMainMenu_DynamicChild3Content" _shadowId="MainMenuViewMainMenu_DynamicChild3Content"> 
     <g transform="translate(0,0)" x="0" y="0" clip-path="url(#clipPath59)"> 
     <g display="block" transform=" translate(0,208)"> 
      <g id="RootMenuItem" width="376" height="50" display="block" buffered-rendering="static" transform="translate(0,0)"> 
       <g id="RootMenuItemIconHolder" transform="translate(25,2)" display="none"> 
        <image id="RootMenuItemIcon" opacity="1.0" width="40" height="40" display="block"/> 
       </g> 
       <foreignObject width="260" height="30" x="65" y="10" transform="null"> 
        <body xmlns="http://www.w3.org/1999/xhtml"> 
        <div xmlns="http://www.w3.org/1999/xhtml" style="font-size:22;color:#AAACAE" id="RootMenuItemText" original_x="null" fill="#AAACAE" font-size="22" width="298">Text 2</div> 
        </body> 
       </foreignObject> 
      </g> 
      <g id="RootMenuItem" width="376" height="50" display="block" buffered-rendering="static" transform="translate(0,52)"> 
       <g id="RootMenuItemIconHolder" transform="translate(25,2)" display="block"> 
        <image id="RootMenuItemIcon" opacity="1.0" width="40" height="40" display="block"/> 
       </g> 
       <foreignObject width="260" height="30" x="65" y="10" transform="null"> 
        <body xmlns="http://www.w3.org/1999/xhtml"> 
        <div xmlns="http://www.w3.org/1999/xhtml" style="font-size:22;color:#AAACAE" id="RootMenuItemText" original_x="null" fill="#1D212D" font-size="22" width="298">Text 1</div> 
        </body> 
       </foreignObject> 
      </g> 
     </g> 
     </g> 
    </g> 
</g> 

我想找到與id="RootMenuItemText"的元素,但我需要這一個元素爲元素的子誰是下一個元素的兄弟(這id"RootMenuItem"),有具有id="RootMenuItemIconHolder"的子女,"display"屬性中的值爲"block"

我需要找到的元素是:

<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:22;color:#AAACAE" id="RootMenuItemText" original_x="null" fill="#1D212D" font-size="22" width="298">Text 1</div> 

我已經試過此XPath:

//*[@id="RootMenuItemIconHolder"][@display='block']/following-sibling::*[last()] 

但它不是定位的元素。有誰知道哪個Xpath表達式可以幫助我找到元素?提前致謝。

回答

1

嘗試以下操作:

//*[@id="RootMenuItemIconHolder"][@display='block']/following-sibling::*[last()]/descendant::div[@id='RootMenuItemText'] 

您應該還記得,你必須設定適當的命名空間進行選擇的工作。這是因爲您的文檔在文檔的不同級別上使用默認的xmlns xml名稱空間。