2017-06-30 103 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的工作原理,我不知道他们为什么以这种方式实现它。但是,例如,使用它可以获得与当前节点完全无关的某些值(例如,某些页面数量)并将其用于当前节点。我认为这是主要原因。 –