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 

Start with the root node as context node (as indicated by the initial / character). Then follow the child axis and apply a node test of book. This selects book element nodes, of which there is exactly one in this document. Using the node-set selected by the first location step, you then follow the attribute axis from that single book element node and apply a node test of edition.

When you select the nodes in the attribute axis, two attribute nodes are selected: the edition and language attribute nodes. When you apply the edition node test to that node-set, only the edition attribute node matches. Thus, the location path selects a single attribute node corresponding to the edition attribute on the book element.

Now that you have seen how to select an attribute node, let’s look at a way to select element nodes depending on the attribute node(s) that they possess. In the preceding example document, notice that the third chapter element has no number attribute. You can use that fact to select the first two chapter element nodes. The syntax to make that selection follows:

/book/chapter[@number] 

The syntax [@number] is a predicate used to filter chapter element nodes. The part of the location path before the predicate selects element nodes named chapter that are child element nodes of the book element node. You then apply the predicate [@number] to the node-set that contains chapter element nodes. Only the first two chapter element nodes in document order possess number attribute nodes, so the third chapter element node (which has no number attribute node) is filtered out of the node-set.

On the other hand, if you wanted to select number attribute nodes on chapter element nodes, we could write this:

/book/chapter/@number 

Follow the child axis from the root node to the single book element node and then the child axis to the chapter element nodes (of which there are three). Then, for each of the chapter element nodes, follow the attribute axis and apply a number node test. Only two of the chapter element nodes have a number attribute node, so only two number attribute nodes are selected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset