XPath - Attribute
You have already learned how to select any element in an XML document, but how would you get that element's attribute? Those attributes values are within your reach once you learn how to use @ !
We will be using our lemonade2.xml file, which you can download.
XML Code, lemonade2.xml:
<lemonade supplier="mother" id="1">
<pop supplier="store" id="2">
<chips supplier="store" id="3">
XPath - @ is for Attribute!
After you have figured out how to select an element in your XML document, just take it one step further to get the attribute.
At the end of your XPath expression, which is normally the element you want to select, add the at sign "@" plus the name of the attribute you wish to select. The following XPath expression selects chips element.
If we wanted to select the supplier attribute of chips, we would add "@supplier" to our expression.
If we wanted to select the supplier attribute of pop, the XPath expression would have to be changed slightly.
Download Tizag.com's XML Book
If you would rather download the PDF of this tutorial, check out our
XML eBook from the Tizag.com store.
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!