Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath where clause

Tags:

xml

xpath

I am trying to build an xpath query on the following xml file:

<recordGroup>
    <records>
        <year>1985</year>   
        <album>
            <name> album1 </name>
            <artist>artist1 </artist>
            <label> labelA</label>
        </album>    
        <album>
            <name>album2 </name>
            <artist>artist2 </artist>
            <label> labelB</label>
        </album>    
    </records>      
    <records>
        <year>1986</year>   
        <album>
            <name>album3 </name>
            <artist> artist1 </artist>
            <label>labelC</label>
        </album>    
        <album>
            <name>album4 </name>
            <artist>artist2</artist>
            <label> labelA</label>
        </album>    
    </records>

</recordGroup>

I want to retrieve the following query: select all records (artist, name & year) where label = 'LabelA'.

The XML structure might not be appropiate to display this data but I am getting this stream from another software so, I cannot change it.

Any suggestions?

like image 628
Valkyrie Avatar asked Dec 26 '12 15:12

Valkyrie


People also ask

What does /* mean in XPath?

/* selects the root element, regardless of name. ./* or * selects all child elements of the context node, regardless of name.

Where is XPath used in?

XPath is mainly used in XSLT, but can also be used as a much more powerful way of navigating through the DOM of any XML-like language document using XPathExpression , such as HTML and SVG, instead of relying on the Document.

How do I get the last child in XPath?

Let us consider an example in which we will try to locate the last text field on the Google sign-up page i.e. "Confirm Password" field. Using XPath- last() method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath("(//input[@type='text'])[last()]"))


1 Answers

This will bring back the full records that contain an album with a label of labelA

//records[album/label='labelA']

You could also use

//records[album/label='labelA']/*[self::year or self::album[label='labelA']]

that will return only the year and the full album that matches the labelA

like image 132
Gabriele Petrioli Avatar answered Oct 10 '22 11:10

Gabriele Petrioli