XPath Attribute

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

XPath Expression:


If we wanted to select the supplier attribute of chips, we would add "@supplier" to our expression.

XPath Expression:


If we wanted to select the supplier attribute of pop, the XPath expression would have to be changed slightly.

XPath Expression: