Nick J.
Nick J.

Reputation: 71

Use xpath or xquery to show text in title attribute

I'd like to use xquery (I believe) to output the text from the title attribute of an html element.

Example:

<div class="rating" title="1.0 stars">...</div>

I can use xpath to select the element, but it tries to output the info between the div tags. I think I need to use xquery to output the "1.0 stars" text from the title attribute.

There's gotta be a way to do this. My Google skills are proving ineffective in coming up with an answer.

Thanks.

Upvotes: 1

Views: 9753

Answers (2)

jwismar
jwismar

Reputation: 12258

XPath: //div[@class='rating']/@title

This will give you the title text for every div with a class of "rating".

Addendum (following from comments below):

If the class has other, additional text in it, in addition to "rating", then you can use something like this:

//div[contains(concat(' ', normalize-space(@class), ' '), ' rating ')]

(Hat tip to How can I match on an attribute that contains a certain string?).

Upvotes: 2

Navin Rawat
Navin Rawat

Reputation: 3138

You should use:

let $XML := <p><div class="rating" title="2.0 stars">sdfd</div><div class="rating" title="1.0 stars">sdfd</div></p>
for $title in $XML//@title
return
  <p>{data($title)}</p>

to get output:

<p>2.0 stars</p>
<p>1.0 stars</p>

Upvotes: -1

Related Questions