2013-12-23 66 views
-1

我需要從下面的代碼解析這個長數字:15-20001963-5-25580855-2-0-0-1-1-0-0-0-0-0-1-0-0-0-0,我不知道要使用的確切XPATH查詢。嵌套href的XPath查詢

有人可以寫一個XPath來提取值嗎?

<div id="menu"> 
    <div id="splash"> 
     <div id="menuItem_1" class="ScreenTitle">Darts</div> 
     <div id="menuItem_2" class="Title">In-Play</div> 
     <div id="subMenu_2"> 
      <div id="menuItem_3" class="Level2"> 
       <a href="../coupon/?ptid=0&amp;key=15-20001963-5-25580855-2-0-0-1-1-0-0-0-0-0-1-0-0-0-0"> 
        Adrian Lewis v Vincent van der Voort<br/> 
        <span class="Score">2-1</span> 
        <span class="Score">Set 4</span> 
       </a> 
      </div> 
      <input id="IPCD" type="hidden" value="1~~False~True" /> 
      <input type="hidden" id="refresh" value="no"> 
     </div> 
    </div> 
</div> 

回答

2

的XPath表達式得到href attiribute包含你的號碼<a>元素可以是例如:

id('menuItem_3')/a/@href 

這將讓你一個字符串值:

../coupon /?ptid = 0 & key = 15-20001963-5-25580855-2-0-0-1-1-0-0-0-0-0-1-0-0-0-0

然後你需要解析。如何獲取數字的具體實現取決於您正在使用的解析引擎和/或編程語言,但XPath無法幫助您。

+1

(或者說,是完全正確的,它可以幫助你在這裏,但我建議不要使用XPath子字符串'href'值。用你的編程語言的功能。) –

+0

什麼時候的XPath開始支持語法像'ID( 'menuItem_3')'?我認爲這相當於'// * [@ id ='menuItem_3']' – Phil

+2

@Phil從一開始。 [這是在XPath 1.0規範](http://www.w3.org/TR/xpath/#function-id)是的,它是同樣的東西,只是更短,並定義爲使用唯一的ID。 –

2

以下XPath將從提供的XML輸入中使用substring-after() function選擇值:15-20001963-5-25580855-2-0-0-1-1-0-0-0-0-0-1-0-0-0-0,以在href值中的「key =」之後選擇子字符串。

substring-after(//div[@id='menuItem_3']/a/@href,'key=')