Home > database >  How to find element inside element and an attribute with XPath?
How to find element inside element and an attribute with XPath?

Time:07-01

I want to extract the following value from the following element

<div style="width: 15%" data-toggle="popover" data-placement="top" data-trigger="hover" data-html="true" role="button" data-original-title="" title="" data-content="<i>15%</i><b>Web Design</b>" ></div>

How to extract the 15 from this linedata-content="<i>15%</i><b>Web Design</b>" The selector is xpath selector.

CodePudding user response:

This answer is very specific in regard to your example, but you can filter out the "15" from your XML with the following XPath:

substring-before(substring-after(//div/@data-content,'&lt;i&gt;'),'%')

It extracts the string between <i> and %.


With XPath-3.0 you could parse the attribute value with fn:parse-xml-fragment:

translate(parse-xml-fragment(//div/@data-content)/i,'%','')

I guess that this is what you wanted; but below XPath version 3.0, you can merely apply string operations and have no real XML parsing.

CodePudding user response:

Your data is not XML (< may not appear in an attribute value) and should not be expected to work with any XPath.

The solution is to fix your XML at the source if you want to use XML tools such as XPath with it. Otherwise, see the link below, or write custom code to parse the (non-XML) data.

See also

  • Related