2017-06-30 99 views
0
<multi-routing-engine-item> 

     <re-name>n</re-name> 

     <zones-information xmlns="http://xml48/juzones" j:s="de"> 
      <zones-security> 
       <zones-security-zonename>A</zones-security-zonename> 
       <zones-security-interfaces> 
        <zones-security-interface-name>reth2.66</zones-security-interface-name> 
        <zones-security-interface-name>2.68</zones-security-interface-name> 
       </zones-security-interfaces> 
      </zones-security> 
      <zones-security>   
       <zones-security-zonename>B</zones-security-zonename> 

問題1:LXML xpath.//和//區別

>>> response_zone.xpath("//zones-information/zones-security[//zones-security-interface-name[text()='reth2.66']]/zones-security-zonename/text()") 
    ['A', 'B', 'C'] 
    >>> 
    >>> response_zone.xpath("//zones-information/zones-security[.//zones-security-interface-name[text()='reth2.66']]/zones-security-zonename/text()") 
    ['A'] 

是.//之間//在這方面有什麼區別。有點困惑。

問題2:

>>> response_zone.xpath(".//zones-security[.//zones-security-interface-name[text()='reth2.66']]/zones-security-zonename/text()") 
['A'] 
>>> response_zone.xpath("//zones-security[.//zones-security-interface-name[text()='reth2.66']]/zones-security-zonename/text()") 
['A'] 
在問題2

,他們有相同的結果.....

我爲此感到困惑。需要幫忙。

+0

'.'指的是當前節點。如果查詢以'/'或'//'開頭,則與文檔的根目錄相關。 '// //遍歷所有的後代。把它們放在一起,你會得到什麼? –

回答

0

閱讀此爲XPATH https://en.wikipedia.org/wiki/XPath =)

. - S短爲self::node(),對當前節點

//參考 - 是短期的/descendant-or-self::node()/,所有節點內搜索所有層上

.// - 從當前節點搜索所有圖層

./從當前節點搜索1層以下

所以當你:

//something[.//another] - somethinganother內的任何層

//something[./another]上 - 在文件中something,但地方 - 有another爲孩子

//something[//another]somethinganother應該也是[不僅在something之內,而且在任何地方]

//something//another - another具有something作爲母體的任何層上

//something/another - something其直接父節點another

//something.//another - 錯誤,因爲語法不正確,僅使用//代替=)

時你只需開始定位器//.// - 作爲根文檔元素的起點沒有區別,因此無論如何它將在整個文件中搜索

+0

// something [// another]:如果它可以在某個地方找到另一個地方,xpath會響應下//所有的值,比如我得到的:response_zone.xpath(「// zones-information/zones-security [/zone-security-interface-name [text()='reth2.66']]/zones-security-zonename/text()「) ['A','B','C'] .Am I correct ? – Robbie

+0

我想明白爲什麼它響應所有區域 - security-zonename/text(),因爲您使用的是'',而不是從當前節點搜索,所以我使用\t // something [//另一個] – Robbie

+0

。//'instead =)這就是xpath的工作原理,我不知道他們爲什麼以這種方式實現它。但是,例如,使用它可以獲得與當前節點完全無關的某些值(例如,某些頁面數量)並將其用於當前節點。我認爲這是主要原因。 –