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?
/* selects the root element, regardless of name. ./* or * selects all child elements of the context node, regardless of name.
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.
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()]"))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With