O'Reilly logo

Sams Teach Yourself XML in 10 Minutes by Andrew H. Watt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accessing Attributes

Selecting attributes is another important use of XPath. You learned earlier that you can use the @ character as an abbreviation for attribute::. You can use the following short XML document to illustrate how to select attributes.

<book edition="1st" language="English"> 
<introduction>Some introduction text</introduction> 
<chapter number="1"> 
Some Chapter 1 text. 
</chapter> 
<chapter number="2"> 
Some Chapter 2 text. 
</chapter> 
<chapter > 
Some Chapter 3 text. 
</chapter> 
<appendix designation="A"> 
Appendix A's content 
</appendix> 
</book> 

If you want to select the edition attribute on the book element, you can write this:

/book/@edition 

It might help you understand this to look at the unabbreviated form:

 /child::book/attribute::edition ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required