XML Query



The XML Path Language (XPath) provides basic facilities for manipulation of strings, numbers, boolean values, and addresses in an XML document.

XPath defines an XML document as a tree of tags, or nodes. For example, the following XML fragment describes an organization, Acme, with one employee and one manager. "acme_org", "employee" and "name" are examples of tags.

<acme_org>
    <accounts_dept>
        <employee level="manager">
            <name>John Smith</name>
            <cubicle label="Welcome">227</cubicle>
            <extension>2145</extension>
        </employee>
    </accounts_dept>
    <engineering_dept>
        <employee>
            <name>Sue Jones</name>
            <extension>2375</extension>
        </employee>
    </engineering_dept>
</acme_org>

The standard XPath functions work by pattern matching the query with the XML document tree using a particular tag as a context. That is, the area of the search will be within the scope of that particular tag only. However, in all XML functions, the scope of a query is the entire XML tree. This tree is sent to XML functions as string in the "xml=string" argument.

An element is a sub-field within a tag. In the above example, the tag name is an element of employee. Each element has a value. An example of a name element's value is the string "John Smith".

Attributes

Elements may have optional attributes which provide further information about the element. You can also indicate element attributes in your queries. In this example, level is an attribute of employee.

<employee level="manager">

To find elements with a specific attribute name, use @attribute_name

To find elements with a known attribute name and value, use element[@attribute_name=\"value\"].

Note that the quotation marks are escaped to indicate that the search string contains quote marks. Were they not escaped, the quotes would indicate the end of the argument string and either the function would not compile, or the results would be unpredictable.

For example, the search string below matches any cubicle element with a sign attribute whose value is "Fidelity".

//cubicle[@sign=\"Fidelity\"]"

This search string matches a cubicle element like this one:

     "<employee>"

         "<name>Henry Crawford</name>"

         "<cubicle sign=\"Fidelity\">227</cubicle>"