XPath SelectNodes in .NET

前端 未结 4 1746
太阳男子
太阳男子 2020-12-28 17:29

   
     
      
    
  
  
   
    
   
            


        
相关标签:
4条回答
  • 2020-12-28 18:06

    //C is all C nodes in the entire document

    /E//C would be only C nodes under E

    /C would be only the root C node

    See the xpath syntax reference

    0 讨论(0)
  • 2020-12-28 18:09

    Simply: a leading // means "at any level" in the same document as the selected node.

    From the spec:

    • //para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
    • .//para selects the para element descendants of the context node
    0 讨论(0)
  • 2020-12-28 18:12

    In the XPATH Specification you will find under 2.5 the following statement:

    //para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node

    i.e. the behaviour you observe is valid. You should do something like "/E//C"

    0 讨论(0)
  • 2020-12-28 18:15

    Specifying .//C will achieve what you want, otherwise, the XPath starts from the document root rather than the current node.

    The confusion is in the definition of // from the XPath standard as follows:

    // is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.

    Because // is short for /descendant-or-self::node()/ it starts at the document level unless you specify a node at the start.

    0 讨论(0)
提交回复
热议问题