1

Say I have the following HTML:

<div class="instruction" id="scan-prompt">
    <span class="long instruction">Scan </span>
    <span id="slot-to-scan">A-2</span>
    <span class="long instruction">&nbsp;to prep</span>
</div>

And I'm trying to write an XPATH selector like this

//div[@id='scan-prompt' and normalize-space()='Scan A-2 to prep']

Is there a way to see what the normalize-space output actually is?

I know you can do $x("//div[@id='scan-prompt']) in chrome debugger but I don't know how to go from that to seeing the output of normalize-space.

Malcolm O'Hare
  • 4,879
  • 3
  • 33
  • 53

1 Answers1

2

Why can you not simply use the path expression

normalize-space(//div[@id='scan-prompt'])

to see what the normalized string value would look like? Other than that, what normalize-space() does exactly is:

  • Removing any leading or trailing whitespaces from the string argument
  • Collapsing any sequence of whitespace characters to just one whitespace character

If handed an element node as an argument (as is the case with your original expression), the function evaluates the string value of that element node. The string value of an element node is the concatenation of all its descendant text nodes.


The result of normalize-space(//div[@id='scan-prompt']) is, given the input you show (whitespace marked with "+"):

Scan+A-2+to+prep

Without invoking normalize-space(), for example string(//div[@id='scan-prompt']):

+
Scan+
A-2+
to+prep+
+

So, simply use path expressions that do nothing else than either giving back a string value or a normalized string value. With Google Chrome by using an XPath expression inside $x().

Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75