untitled

(ff) #1

8.1 XML Navigation Using XPath 177


//Chemical/*


  1. attribute. To select an attribute, use an@followed by the name of the
    attribute. Wild cards can also be used for attributes. To obtain every at-
    tribute of every Interview element, use this path:


//Interview/@*


  1. text. Text contained in an element can be explicitly selected by using the
    text()function. This is not usually necessary. If an element only con-
    tains text, then selecting the element will also select its text content.


One can navigate from one step to another in several ways. XPath calls this
theaxisof navigation. The most common are the following:



  1. child element. This is the normal way to navigate from one node to an-
    other. If no other axis of navigation is explicitly specified, then the path
    navigates to the child element.

  2. descendant element. A double slash will navigate any number of levels
    to a matching node. This is handy when one has a deep hierarchy.

  3. parent element. One can go up one level by using a double dot. To obtain
    the PubMed identifier (PMID) of every Chemical node, use this path:


//Chemical/../../PMID


  1. ancestor element. One can go up any number of levels by using the an-
    cestor:: axis. For example, to obtain the PMID of every Chemical node,
    even when it is located several levels above, use this path:


//Chemical/ancestor::PMID


  1. root element. A slash at the start of a path means to start at the highest
    level of the document. If a slash is not specified at the start of a path,
    then the path starts at the current element. This depends on the context in
    which XPath is used.

Free download pdf