Resin can use XQL, the XML Query Language, to select nodes from
an XML tree. Scripts can select all 'table' children, or
even all 'table' elements in an entire HTML file. The XQL language is
exceptionally rich. It can describe an incredible number of node
selections.
The XML Query Language describes nodes in an XML tree. It's a mini-language
for specifying nodes patterns, like regular
expressions are a mini-language for specifying text patterns.
The language selects sets of nodes. Each operator in XQL selects
a new set based on the old set of nodes. For example, given a set of
chapters, XQL can select all sections in those chapters,
chapters with 'advanced' attributes, or grandchildren with 'color'
attributes of 'blue'.
XML Query Language (XQL)
|
node |
Selects all child elements with nodeName of node. |
* |
Selects all child elements. |
@attr |
Selects the attribute attr. |
@* |
Selects all attributes. |
node() |
Matches an element node. |
text() |
Matches a text node. |
comment() |
Matches a comment. |
pi() |
Matches a processing instruction node. |
. |
Selects the current node. |
.. |
Selects the parent of current node. |
/ |
Selects the document node. |
a[expr] |
Select only those nodes matching a which also satisfy the
expression expr. |
a[n] |
Selects the nth matching node matching a
|
a/b |
For each node matching a, add the nodes matching b
to the result. |
a//b |
For each node matching a, add the descendant nodes
matching b to the result. |
//b |
Returns elements in the entire document matching b. |
a|b |
All nodes matching a or b. |
Selects all child elements with nodeName of node.
xml = caucho.xml.Xml.parseString(@<<END);
<top>
<a id='1'/>
<b>
<a id='2'/>
</b>
<a id='3'/>
</top>
END
top = xml.documentElement
for (var node in top.select('a'))
writeln(node.nodeName, ': ', node.attribute.id);
|
a: 1
a: 3
|
Selects all child elements.
Selects the attribute attr.
chapter/@title |
All title attributes of chapters. |
//var[@name='keywords']/@content |
The contents of all var keywords.
A web spider might use this.
|
Selects all attributes.
Matches a comment.
Selects the current node.
The current node is primarily useful for descendant patterns.
for some filter patterns.
.//image |
Any image descendant. |
.//image |
Any image descendant. |
Selects the parent of current node.
../brother |
All brothers. |
../../aunt |
All aunts. |
//*[../@color='blue'] |
All elements with blue parents. |
Selects the document node.
Useful for finding constants in a document.
/html |
The single root element. |
/html/head |
The HTML head section. |
/html/head/var |
All var tags. |
Select only those nodes matching a which also satisfy the
expression expr.
The expression b is a combination of and ,
or , not , comparisons and XQL patterns. An XQL
expression, e.g. chapter/verse , is true if at least one node
matches the pattern.
chapter[verse] |
Chapters with a verse. |
chapter[not(verse)] |
Chapters with no verses. |
chapter[not(verse) and not(section)] |
Chapters with neither verses
nor sections. |
*[@color='blue'] |
Blue children. |
*[@color='blue'][position()=last() - 1] |
second to last blue child |
Selects the nth matching node matching a
When a filter's expression is a number, XQL selects based on position.
This special case of the filter pattern treats selections as ordered
lists.
The position filter is equivalent to
a[position()=n]
child[3] |
third child |
child/grandchild[1] |
first grandchild from every child (not only the first
grandchild). |
child[last()] |
last child |
For each node matching a, add the nodes matching b
to the result.
The following is almost a definition of a/b.
for (var a in node.select('a')) {
for (var b in a.select('b')) {
// possible duplicates if a or b
// are weird patterns.
}
}
|
Some example interpretations,
chapter/verse |
grandchildren verse with
parent chapter. |
../span |
sibling span elements. |
./span |
children span elements. |
*/* |
All grandchildren. |
*[color='blue']/verse |
All grandchildren verse elements
with blue colored parents. |
a/b/c |
Great grandchildren c, with
parent b and grandparent a. |
For each node matching a, add the descendant nodes
matching b to the result. The '//' operator selects all
descendants matching b. The '/' operator selects all children
matching b.
chapter//a |
All links contained in a chapter. |
.//image |
Any image descendant. |
Returns elements in the entire document matching b.
This is equivalent to /.//b, but less weird.
//image |
All images in the document |
//a[@href='http://www.caucho.com'] |
All links to caucho |
All nodes matching a or b.
Some example interpretations,
lion|tiger|bear |
Lions and tigers and bears.
|
Copyright © 1998-2000 Caucho Technology. All rights reserved.
Last modified: Thu, 16 Sep 1999 14:56:49 -0700 (PDT)
|