untitled

(ff) #1

178 8 Query Languages


While directory paths and XML paths are very similar, there is a distinction
mentioned in table 1.1 which is important for navigation; namely, there can
be many child elements with the same name. A molecule, for example, can
have many atoms (see figure 1.6). To select a particular atom, such as the first
one, use this path:

/molecule/atomArray/atom[position()=1]

This path will select the first atom of every molecule. One can abbreviate the
path above to the following:

/molecule/atomArray/atom[1]

which makes it look like the array notation used in programming languages,
except that child numbering begins with 1, while programming languages
usually start numbering at 0. However, this notation is an abbreviation that
can be used in this case only, and it should not be used in more complicated
selection expressions.
XPath brackets are a versatile mechanism for selecting nodes. In addition
to selection by position, one can also select by attribute and node values. For
example, to select the nitrogen atoms in nitrous oxide, use this path:

/molecule[@name=’nitrous oxide’]//atom[@elementType=’N’]

XPath has many numerical and string operations. Some of these are shown in
table 8.1. Selection criteria can be combined, using the Boolean operators. For
example, if one would like the carbon and oxygen atoms in hydroquinone,
then use this path:

/molecule[@name=’hydroquinone’]
//atom[@elementType=’C’ or @elementType=’O’]

The XPath query above should have been on a single line, but it was shown
on two lines for typographical purposes. Other XPath queries in this chapter
have also been split to fit in the space available.
When using more complicated expressions, one cannot use abbreviations.
For example, if one would like the last atom of each molecule, but only if it
is a carbon atom, then use this path:

/molecule
//atom[@elementType=’C’ and position()=last()]
Free download pdf