XPath useage in the Whitebeam Environment
XPath is a query language
developed by the W3C organisation. The generic definition
referenced here is used as a base technology for a number of other W3C initiatives including
XSLT and XQuery.
In it's basic form XPath allows a 'query' or 'expression' to be appied to an XML tree or branch of
an XML tree. The expression can select portions of the tree based on tests applied to nodes within the tree,
or can simply answer questions such as 'does the <product...> tag have a 'state' attribute with
the value 'inStock'.
You can experiment with XPath here!
In the Whitebeam environment - XPath is accessed through two methods on XML elements withing an XML tree.
XmlComment and XmlPi. The methods are:
Evaluate an XPath expression to yield a set of XML elements.
Evaluate a generic XPath expression yielding any of the types that are valid XPath values. eg bool, string, number or XmlNodeSet.
|node set||instance of
|1||//child||Return a NodeSet containing ALL XML tags in the document with the name 'child'.|
|2||//child[@name='beta']||Return a NodeSet containing ALL XML tags in the document with the name 'child'
that have an attribute called 'name' with a value of 'beta'.|
|3||/parent/child[@name='beta']/@id||Look for all direct 'child' children of the 'parent' tag for which
the 'name' attribute has the value 'beta'. Having found this set - return
the values of all of the price attributes of each selected element.|
|4||/parent/child[@name='alpha' or @id=2]||Look for all direct 'child' children of the 'parent' tag that have
either a 'name' with value 'alpha' or an 'id' attribute
with value '2'. Return the results as a set of nodes. |
|5||count(/parent/child)||Count the number of 'child' tags that are direct chidren of a 'parent' tag.|
|6||/parent/child|parent/pet||Return the union of the set of 'children' and 'pet' tags.|
|7||//*[starts-with(name(),'p')]||Return all nodes - anywhere in the document - that have a name
with 'p' as the first letter.|
|8||//pet[contains(string(),'dog')]||Return the set of tags for which the string value contains the word
'dog'. Note that XPath 'string' value is not the same as the XML
parsers string value. XPath generates the string value of a tag as the
concatenation of all the text descendants of the target tag. In
this case the search looks at the content and NOT at the contents of
formatting and other markup. |
|9||/parent/(child | pet)/description||Take all children of 'content' called EITHER body OR head. From that
set - select all tags called 'rb:script'. |
|10||//*[@id>1 and @id<4]||Return the set of all tags that have an 'id' attribute and in which the
(numeric) value of that id is greater than 2 and less than 4. Note
that XPath numbers are double precision floating point numbers!|
|11||/parent/child/(@id>1 and @id<4)||Return 'true' if the value of the 'id' attribute of the first child of the parent is between 1 and 4. If
the attribute doesn't exist at all then the value evaluates to false.|
|12||/parent/*[3 to 5]||Return the third through 5th tags contained in the parent tag.
Note examples 9 amd 11 use XPath extensions borrowed from the XPath 2.0 wish list. In practice they make
processing of node and tree much simpler in some situations.
(loadtime : 16ms)