Get number via XPath?

There's a part of the page:

<p>
  349
  <span>
    $
  </span>
</p>

How to get "349"?

Upvotes: 1

Views: 6056

Answers (2)

kjhughes
kjhughes

Reputation: 111541

There are many XPaths that will select "349" from that XML. Here are a few:

  • Select the space-normalized text node children of p:

    normalize-space(/p/text())
    
  • Select the space-normalized substring before the $ in the string value of p:

    normalize-space(substring-before(/p, '$'))  
    
  • Select space-normalized, numeric text nodes anywhere in the document:

    normalize-space(//text()[number(.) = .])
    

All of these XPaths will select "349" as a string as requested. You could also wrap any of the above expressions with a number() function call if you actually want 349 as a number rather than as a string.

Upvotes: 3

OldProgrammer
OldProgrammer

Reputation: 12169

Since you don't show the complete page, we don't have a complete path, but try this:

 /p/text()

Upvotes: 0

Related Questions