How do I select child elements of any depth using XPath?

后端 未结 4 1398
难免孤独
难免孤独 2021-01-31 01:04

Suppose I have this (simplified):

相关标签:
4条回答
  • 2021-01-31 01:28

    You're almost there. Simply use:

    //form[@id='myform']//input[@type='submit']
    

    The // shortcut can also be used inside an expression.

    0 讨论(0)
  • 2021-01-31 01:30

    If you are using the XmlDocument and XmlNode.

    Say:

    XmlNode f = root.SelectSingleNode("//form[@id='myform']");
    

    Use:

    XmlNode s = f.SelectSingleNode(".//input[@type='submit']");
    

    It depends on the tool that you use. But .// will select any child, any depth from a reference node.

    0 讨论(0)
  • 2021-01-31 01:38

    Also, you can do it with css selectors:

    form#myform input[type='submit']
    

    space beween elements in css elector means searching input[type='submit'] that elements at any depth of parent form#myform element

    0 讨论(0)
  • 2021-01-31 01:41
    //form/descendant::input[@type='submit']
    
    0 讨论(0)
提交回复
热议问题