Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

html - Testing text() nodes vs string values in XPath

I have a node as follows:

<span class="portal-text-medium">Office Hours</span>

For the XPath I use

//span[text()='Office Hours']

which should work, but it never does. I can use *contains(text(),'Office Hours')]* but that won't find an exact match and I have to verify there is no "*". This is not the only time it hasn't worked for me. I have seen it work before so I don't know what is wrong. Any idea?

Yes I can, and do, use starts-with but it is not quite the same.

Question&Answers:os

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

1 Answer

0 votes
by (71.8m points)

XPath text() = is different than XPath . =

(Matching text nodes is different than matching string values)

The following XPaths are not the same...

  1. //span[text() = 'Office Hours']

    Says:

    Select the span elements that have an immediate child text node equal to 'Office Hours`.

  2. //span[. = 'Office Hours']

    Says:

    Select the span elements whose string value is equal to 'Office Hours`.

In short, for element nodes:

The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.

Examples

The following span elements would match only #1:

  • <span class="portal-text-medium">Office Hours<br/>8:00-10:00</span>
  • <span class="portal-text-medium">My<br/>Office Hours</span>

The following span elements would match only #2:

  • <span class="portal-text-medium"><b>Office</b> Hours</span>
  • <span class="portal-text-medium"><b><i>Office Hours</i></b></span>

The following span element would match both #1 and #2:

  • <span class="portal-text-medium">Office Hours</span>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...