2017-06-30 7 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> 

question1: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'] 

この文脈で.//と//との違いは何ですか。ちょっと混乱しました。

question2:question2で

>>> 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'] 

、彼らは同じ結果を持っている.....

私はこれで混乱しています。助けが必要。

+0

'.'は現在のノードを指します。クエリが '/'または '/'で始まる場合、それはドキュメントのルートに相対的です。 '//'はすべての子孫を通ります。一緒に入れて、何を手に入れますか? –

答えて

0

)=たxPath https://en.wikipedia.org/wiki/XPathため

.これを読む - self::node()の略だ、現在のノードの参照

// - は、すべての層

.//上のすべてのノード内の検索/descendant-or-self::node()/ための短いです - すべてのレイヤー上の現在のノードからの検索

./下位レイヤーからの検索

ので、あなたが行うとき:

//something[.//another]からsomethingが、どこかのファイルで - 子としてanother

//something[//another]を持ってsomethingからsomethingどの層

//something[./another]の内部anotherを持っていますanotherも[somethingの中だけでなく、どこでも]

//something//another - 直接の親another

//something.//anotherことsomething - - 任意の層

//something/anotherに親としてsomethingを有するanotherエラー、構文が正しくないとして、=代わりだけ//を使用)

場合あなたはちょうど//または.//とロケータを開始する - 始点はルート文書要素であるので違いはないので、とにかくファイル全体を検索する

+0

// something [// another]:別の場所を見つけることができるなら、xpathは//何かの下のすべての値で応答します。私が取得したものと同じです:response_zone.xpath( "// zones-information/zones-security [//セキュリティゾーン名/テキスト() ") ['A'、 'B'、 'C​​']。正しい? – Robbie

+0

\t // something [// another] – Robbie

+0

を使用している場合は、現在のノードから検索しているので、なぜそれがすべてのゾーン(security-zonename/text()に応答するのか理解したいです。// '代わりに=)、それはxpathが動作する方法です、なぜ私はこれらの方法で実装したのか分かりません。しかし、これを使用すると、たとえば、現在のノードにはまったく関連しない値(たとえば、いくつかのページ量)を現在のノードで使用することができます。私はそれが主な理由だと思う。 –

関連する問題