Xpath Expressions

I need to get the element value <mathcolor>for the element <colorvalue>has a value of 2. Please correct the Xpath expression below:

Xpath expression:

/colors/child::color/child::mathcolor[colorvalue='2']

XML example:

<?xml version="1.0"?>
<colors>
    <color>
        <mathcolor>#007dc5</mathcolor>
        <textcolor>C=100 M=40 Y=0 K=0</textcolor>
        <colorvalue>2</colorvalue>
    </color>
    <color>
        <mathcolor>#ed1b34</mathcolor>
        <textcolor>C=0 M=100 Y=85 K=0</textcolor>
        <colorvalue>3</colorvalue>
    </color>
</colors>
+5
source share
2 answers

Almost try the following:

/colors/child::color[colorvalue='2']/child::mathcolor/text()

or simpler

/colors/color[colorvalue='2']/mathcolor/text()
+3
source

I prefer to orient the actual element you want first with // mathcolor. The rest can be said that if you continued down the structure, you would see the following name sibling node colorvalue with the text node = 2.

//mathcolor[./following-sibling::colorvalue[./text()='2']]
0
source

All Articles