Xpath in SVG element

I’m trying to grab an element under SVG tag and get it text.
In HTML, I found that I have 2 elements have the same properties as below :

<text x="8" data-z-index="1" y="20" style="font-size:12px;color:#FFF;cursor:default;fill:#FFF;"> <tspan style="fill:#DDDF0D; opacity: 0.3">●</tspan> <tspan dx="0"> Emetteurs Zone 1: </tspan> <tspan style="font-weight:bold" dx="0"> min 1.0</tspan> <tspan dx="0"> - max </tspan> <tspan style="font-weight:bold" dx="0">1.0</tspan> </text>

<text x="8" data-z-index="1" y="20" style="font-size:12px;color:#FFF;cursor:default;fill:#FFF;"> <tspan style="fill:rgb(238,111,97)">●</tspan> <tspan dx="0"> Température ambiance Zone 1 (°C): </tspan> <tspan style="font-weight:bold" dx="0">18.5</tspan> </text>

I tried with “style” attribut of tag, but I return me 4 elements matched :
//*[local-name()="tspan" and @style="fill:#DDDF0D; opacity: 0.3"]/parent::*//following-sibling::*[local-name()="tspan"][1]

Is there some way to have a xpath to Emetteurs Zone 1: of the first element ?

Thank you

Assuming you know the text will be the above, then maybe you can use:
//*[text()="Emetteurs Zone 1:"]



and lastly, yours, but slightly different:

//*[local-name()="tspan" and @style="fill:#DDDF0D; opacity: 0.3"]//following-sibling::tspan[1]

I forgot to notice that Emetteurs Zone 1: is the text i want to compare, that why I’m trying to not use text() or contains().

Base on your last suggestion when added //following-sibling::tspan[1], I have no element matched

and no element matched for //text[count(//text/tspan)=5]/tspan[2] too

Using count was just an idea. Too bad. It worked on my copy of your code. It would probably be too dicey on a full page instead of just the snippet.


How about using the line below the one you want and then moving up?
//text/tspan[text()=" min 1.0"]/preceding-sibling::tspan[1]

or another

//text/tspan[@style="fill:#DDDF0D; opacity: 0.3"]/following-sibling::tspan[1]

The following XPath will NOT select a SVG tspan element:

Possibly, it should rather be:

//*[local-name()="tspan" and @style="fill:#DDDF0D; opacity: 0.3"]//following-sibling::*[local-name()="tspan" and position()=1]

I mean, you need to repeat writing “*[local-name="svg element name" ...” for every single layer of SVG element path.

Consequently any XPath expression for an element under the <svg xmlns="http://www.w3.org/2000/svg"> element embedded in a <html> will become very long, which is inevitable.

cool, that’ll work with this one //*[local-name()="tspan" and @style="fill:#DDDF0D; opacity: 0.3"]//following-sibling::*[local-name()="tspan" and position()=1]

I see what you say about the xpath but It’s OK, at least I understand how to rewrite this xpath.

Many thanks