Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

xml - What is the difference between .// and //* in XPath?

While finding the relative XPath via Firebug : it creates like

  1. .//*[@id='Passwd']--------- what if we dont use dot at the start what it signifies?

  2. Just add //* in the Xpath -- it highlights --- various page elements ---------- what does it signify?

Below are XPaths for Gmail password fields. What is significance of * ?

  • .//*[@id='Passwd']

  • //child::input[@type='password']

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several distinct, key XPath concepts in play here...

Absolute vs relative XPaths (/ vs .)

  • / introduces an absolute location path, starting at the root of the document.
  • . introduces a relative location path, starting at the context node.

Named element vs any element (ename vs *)

  • /ename selects an ename root element
    • ./ename selects all ename child elements of the current node.
  • /* selects the root element, regardless of name.
    • ./* or * selects all child elements of the context node, regardless of name.

descendant-or-self axis (//*)

  • //ename selects all ename elements in a document.
    • .//ename selects all ename elements at or beneath the context node.
  • //* selects all elements in a document, regardless of name.
    • .//* selects all elements, regardless of name, at or beneath the context node.

With these concepts in mind, here are answers to your specific questions...

  • .//*[@id='Passwd'] means to select all elements at or beneath the current context node that have an id attribute value equal to 'Passwd'.
  • //child::input[@type='password'] can be simplified to //input[@type='password'] and means to select all input elements in the document that have an type attribute value equal to 'password'.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...