<document>
  <header>
    <product>resin</product>
    <title>XML Path Language (XPath)</title>
    <description>
      <p>Resin can use XPath, the XML Path 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 XPath
      language is exceptionally rich.  It can describe an incredible
      number of node selections. </p>
    </description>


  </header>

  <body>

<s1>
      <p>Java applications can use the com.caucho.xpath package to use
      XPath with an XML tree.</p>

      <p>The XML Path 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.</p>

      <p>The language selects sets of nodes.  Each operator in XPath
      selects a new set based on the old set of nodes.  For example,
      given a set of chapters, XPath can select all sections in those
      chapters, chapters with 'advanced' attributes, or grandchildren
      with 'color' attributes of 'blue'.</p>
</s1>

    <localtoc/>

<s1 name="Basic" title="Basic Patterns">

<p>The basic XPath patterns cover 90% of the cases that most stylesheets
will need.  Because the pattern language is based on familiar
filesystem paths, the most useful patterns should be easy.</p>

<s2 name="node" title="node" type="defun">
<p>Selects all child elements with nodeName of <var>node</var>.</p>

<example>
xml = caucho.xml.Xml.parseString(@&lt;&lt;END);
&lt;top&gt;
  &lt;a id='1'/&gt;
  &lt;b&gt;
    &lt;a id='2'/&gt;
  &lt;/b&gt;
  &lt;a id='3'/&gt;
&lt;/top&gt;
END

top = xml.documentElement

for (var node in top.select('a'))
  writeln(node.nodeName, ': ', node.attribute.id);
</example>
<results>
a: 1
a: 3
</results>

</s2>

<s2 name="star" title="*" type="defun">
<p>Selects all child elements.</p>
</s2>

<s2 name="attr" title="@attr" type="defun">
<p>Selects the attribute <var>attr</var>.</p>

<deftable>
<tr><td>chapter/@title
</td><td>All title attributes of chapters.

</td></tr><tr><td>//var[@name='keywords']/@content
    </td><td>The contents of all var keywords.
A web spider might use this.
</td></tr></deftable>
</s2>

<s2 name="starattr" title="@*" type="defun">
<p>Selects all attributes.</p>
</s2>

<s2 name="starattr" title="ns:*" type="defun">
<p>Selects elements in the given namespace. Namespace patterns
only make sense in the context of XSL, where the namespace
declarations have been made.</p>
</s2>

<s2 name="node" title="node()" type="defun">
<p>Matches an org.w3c.dom.Node.</p>
</s2>

<s2 name="text" title="text()" type="defun">
<p>Matches a org.w3c.dom.Text node.</p>
</s2>

<s2 name="comment" title="comment()" type="defun">
<p>Matches a comment.</p>
</s2>

<s2 name="pi" title="processing-instruction()" type="defun">
<p>Matches a org.w3c.dom.ProcessingInstruction node.</p>
</s2>
<!--
<defun name=er title='er()'>
<p>Matches an org.w3c.dom.EntityReference node.</p>
</defun>
-->

<s2 name="dot" title="." type="defun">
<p>Selects the current node.</p>

<p>The current node is primarily useful for descendant patterns.
for some filter patterns.
</p>

<deftable>
<tr><td>.//image</td>
    <td>Any image descendant.</td>

</tr><tr><td>.//image</td>
    <td>Any image descendant.</td>
</tr></deftable>

</s2>

<s2 name="parent" title=".." type="defun">
<p>Selects the parent of current node.</p>

<deftable>
<tr><td>../brother</td>
    <td>All brothers.</td>
</tr><tr><td>../../aunt</td>
   <td>All aunts.</td>
</tr><tr><td>//*[../@color='blue']</td>
    <td>All elements with blue parents.</td>
</tr></deftable>
</s2>

<s2 name="root" title="/" type="defun">
<p>Selects the document node.</p>

<p>Useful for finding constants in a document.</p>

<deftable>
<tr><td>/html</td>
    <td>The single root element.</td>
</tr><tr><td>/html/head</td>
    <td>The HTML head section.</td>
</tr><tr><td>/html/head/var</td>
    <td>All var tags.</td>
</tr></deftable>
</s2>

<s2 name="filter" title="a[expr]" type="defun">
<p>Select only those nodes matching <var>a</var> which also satisfy the
expression <var>expr</var>.</p>

<p>The expression <var>b</var> is a combination of <code>and</code>,
<code>or</code>, <code>not</code>, comparisons and XPath patterns.  An XPath
expression, e.g. <code>chapter/verse</code>, is true if at least one node
matches the pattern.
</p>

<deftable>
<tr><td>chapter[verse]</td>
    <td>Chapters with a verse.</td>

</tr><tr><td>chapter[not(verse)]</td>
    <td>Chapters with no verses.</td>

</tr><tr><td>chapter[not(verse) and not(section)]</td>
    <td>Chapters with neither verses nor sections.</td>

</tr><tr><td>*[@color='blue']</td>
    <td>Blue children.</td>

</tr><tr><td>*[@color='blue'][position()=last() - 1]</td>
    <td>second to last blue child</td>
</tr></deftable>
</s2>

<s2 name="nth-filter" title="a[n]" type="defun">
<p>Selects the <var>n</var>th matching node matching <var>a</var>
When a filter's expression is a number, XPath selects based on position.
This special case of the filter pattern treats selections as ordered
lists.</p>

<p>The position filter is equivalent to
<code><var>a</var>[position()=<var>n</var>]</code></p>

<deftable>
<tr><td>child[3]
    </td><td>third child

</td></tr><tr><td>child/grandchild[1]
    </td><td>first grandchild from every child (not only the first
grandchild).

</td></tr><tr><td>child[last()]
    </td><td>last child
</td></tr></deftable>

</s2>

<s2 name="compose" title="a/b" type="defun">
<p>For each node matching <var>a</var>, add the nodes matching <var>b</var>
to the result.</p>

<p>The following is almost a definition of a/b. 
<example>
for (var a in node.select('a')) {
  for (var b in a.select('b')) {
    // possible duplicates if a or b
    // are weird patterns.
  }
}
</example>

</p><p>Some example interpretations, </p>
<deftable>
<tr><td>chapter/verse</td>
    <td>grandchildren verse with parent chapter.</td>

</tr><tr><td>../span</td>
    <td>sibling span elements.</td>

</tr><tr><td>./span</td>
    <td>children span elements.</td>

</tr><tr><td>*/*</td>
    <td>All grandchildren.</td>

</tr><tr><td>*[color='blue']/verse</td>
    <td>All grandchildren verse elements with blue colored parents.</td>

</tr><tr><td>a/b/c</td>
    <td>Great grandchildren c, with parent b and grandparent a.</td>
</tr></deftable>
</s2>

<s2 name="descendant" title="a//b" type="defun">
<p>For each node matching <var>a</var>, add the descendant nodes
matching <var>b</var> to the result.  The '//' operator selects all
descendants matching <var>b</var>.  The '/' operator selects all children
matching <var>b</var>.</p>

<deftable>
<tr><td>chapter//a</td>
    <td>All links contained in a chapter.</td>

</tr><tr><td>.//image</td>
    <td>Any image descendant.</td>
</tr></deftable>
</s2>

<s2 name="root-descendant" title="//b" type="defun">
<p>Returns elements in the entire document matching <var>b</var>.</p>

<p>This is equivalent to /.//<var>b</var>, but less weird.</p>

<deftable>
<tr><td>//image</td>
    <td>All images in the document</td>
</tr><tr><td>//a[@href='http://www.caucho.com']</td>
    <td>All links to caucho</td>
</tr></deftable>

</s2>

<s2 name="union" title="a|b" type="defun">
<p>All nodes matching <var>a</var> or <var>b</var>.</p>

<p>Some example interpretations, </p>
<deftable>
<tr><td>lion|tiger|bear</td>
    <td>Lions and tigers and bears.</td>
</tr></deftable>
</s2>

<s2 name="exprroot" title="expr/path" type="defun">
<p>Uses an expression as the start of a path.  Usually,
<var>expr</var> will be a special function call, like the <code>id()</code>
function.  <code>expr[expr]</code> is also allowed.</p>

<deftable>
<tr><td>id('314')/@color</td>
    <td>The color attribute of the element with id 314.</td>

</tr><tr><td>key('id', 'a')[@color='red']</td>
    <td>Selects the node returned by key if they have a red color.
    </td>
</tr></deftable>
</s2>

</s1>

<s1 name="Axis" title="Node Axes">

<s2 name="child" title="child::a" type="defun">

<p>The child axis selects children of the current node.  It is entirely
equivalent to the usual slash notation.  So <code>child::a/child::b</code>
is the same as <code>a/b</code>.</p>

<deftable>
<tr><td>child::chapter</td>
    <td>Select all chapter elements of the current node.</td>

</tr><tr><td>child::text()</td>
    <td>All text children.</td>

</tr><tr><td>child::comment()</td>
    <td>All comment children.</td>

</tr><tr><td>/child::h1</td>
    <td>Select all top-level h1 elements.</td>
</tr></deftable>
</s2>

<s2 name="descendant" title="descendant::a" type="defun">

<p>The descendant axis selects descendants of the current node.  It is 
equivalent to '//'.  So <code>child::a//child::b</code>
is the same as <code>a//b</code>.</p>

<deftable>
<tr><td>descendant::a</td>
    <td>Select all hyperlinks below the current node.</td>

</tr><tr><td>/descendant::table</td>
    <td>Select all tables in the document.</td>

</tr><tr><td>child::section/descendant::image</td>
    <td>Select all images in a section.</td>

</tr><tr><td>.[descendant::image]</td>
    <td>Selects the current node if it contains an image.</td>
</tr></deftable>
</s2>

<s2 name="descendant-or-self" title="descendant-or-self::a" type="defun">

<p>Selects descendants including the current node.</p>
</s2>

<s2 name="attribute" title="attribute::a" type="defun">

<p>Selects attributes of the current element.  It is 
equivalent to @<var>a</var>.</p>

<deftable>
<tr><td>attribute::width</td>
    <td>Selects the width of the current node.</td>

</tr><tr><td>image[attribute::width &gt; 10]</td>
    <td>Selects image children with a width attribute
greater than 10.</td>
</tr></deftable>
</s2>

<s2 name="following-sibling" title="following-sibling::a" type="defun">

<p>Selects nodes after the current node.</p>

<deftable>
<tr><td>following-sibling::chapter</td>
    <td>Selects following chapters.</td>

</tr><tr><td>h3/following-sibling::text()</td>
    <td>Following text nodes after an h3.</td>
</tr></deftable>
</s2>

<s2 name="preceding-sibling" title="preceding-sibling::a" type="defun">

<p>Selects nodes before the current node.</p>

<p><code>preceding-siblings::</code> counts positions backwards.
So preceding-sibling::a[1] selects the closest preceding sibling.</p>

<deftable>
<tr><td>preceding-sibling::chapter</td>
    <td>Selects chapters before chapter 3.</td>

</tr><tr><td>fun/preceding-sibling::comment()</td>
    <td>Selects the comments before a function.</td>
</tr></deftable>
</s2>

<s2 name="following" title="following::a" type="defun">

<p>Selects the first matching node following in document order, excluding
descendants.  In other words, the following axis will scan through
every node in document order, starting with <code>getNextSibling()</code>.</p>

<p>following, preceding, ancestor and self partition the document.</p>
</s2>

<s2 name="preceding" title="preceding::a" type="defun">

<p>Selects the first matching node preceding in document order, excluding
ancestors.  In other words, the preceding axis will scan through
every node in document order, starting with the root and ending in the
current node, but it will skip ancestors.</p>

<p>following, preceding, ancestor and self partition the document.</p>
</s2>

<s2 name="parent" title="parent::a" type="defun">

<p>Selects the parent if it matches.  The '..' pattern from
the core is equivalent to 'parent::node()'.</p>

<deftable>
<tr><td>parent::chapter</td>
    <td>Selects the parent if it's a chapter.</td>

</tr><tr><td>parent::*/@color</td>
    <td>Selects the color of the parent.</td>
</tr></deftable>
</s2>

<s2 name="ancestor" title="ancestor::a" type="defun">

<p>Selects matching ancestors.</p>

<p>following, preceding, ancestor and self partition the document.</p>
<deftable>
<tr><td>ancestor::section</td>
    <td>Selects ancestor sections.</td>

</tr><tr><td>ancestor::*/@color</td>
    <td>Selects ancestor color attributes.</td>
</tr></deftable>
</s2>

<s2 name="ancestor-or-self" title="ancestor-or-self::a" type="defun">
<p>Selects ancestors including the current node.</p>
</s2>

<s2 name="self" title="self::a" type="defun">

<p>Selects the current node. '.' is equivalent to 'self::node()'.

</p><p>following, preceding, ancestor and self partition the document.</p>

<deftable>
<tr><td>self::chapter</td>
    <td>Selects the current node if it's a chapter.</td>
</tr></deftable>
</s2>

</s1>

<s1 name="expr" title="Expressions">

<s2 name="expr-path" title="path" type="defun">
<p>Selects nodes based on the <var>path</var>.</p>

<p>The path interpretation depends on the context.  When filtering
nodes, the path expression is a boolean.  Any matching node will
return true.  When the value is a string, as in the xsl:value-of, then
the textValue is used.</p>

<deftable>
<tr><td>boolean</td><td>True if <var>path</var> matches any nodes.
</td></tr><tr><td>string</td><td>The text value of the first matching node.
</td></tr><tr><td>number</td><td>The string value converted to a number.
</td></tr></deftable>
</s2>

<s2 name="number" title="number" type="defun">
<p>Numbers have the same syntax as Java doubles.</p>
</s2>

<s2 name="string" title="&quot;string&quot;" type="defun">
<p>String literals can use single or double quotes.</p>
</s2>

<s2 name="equals" title="a = b" type="defun">
<p>Standard comparisons.</p>

<p>XPath converts the arguments to a common type before comparison.  The
conversion priority is boolean, number, string.  In other words, if
either arg is a boolean, a boolean comparison is used.</p>
<ol>
<li>boolean
</li><li>number
</li><li>string
</li></ol>

<p>Node-sets, e.g. chapter/@color, are compared differently.  Each
node in the node set is compared.  If any matches, then the comparison
is true.</p>

<deftable>
<tr><td>a = b</td><td>True if a equals b.
</td></tr><tr><td>a != b</td><td>True if a is not equal to b.
</td></tr><tr><td>a &lt; b</td><td>True if a is less than b.
</td></tr><tr><td>a &lt;= b</td><td>True if a is less than or equal to b.
</td></tr><tr><td>a &gt; b</td><td>True if a is greater than b.
</td></tr><tr><td>a &gt;= b</td><td>True if a is greater than or equal to b.
</td></tr></deftable>
</s2>

<s2 name="equals" title="a + b" type="defun">
<p>Arithmetic expressions.</p>

<deftable>
<tr><td> - a</td><td>Unary minus
</td></tr><tr><td>a + b</td><td>Add
</td></tr><tr><td>a - b</td><td>Substract
</td></tr><tr><td>a * b</td><td>Multiply
</td></tr><tr><td>a div b</td><td>Divide
</td></tr><tr><td>a mod b</td><td>Floating point mod, like Java.
</td></tr></deftable>
</s2>

<s2 name="group" title="(expr)" type="defun">
<p>Parenthesized expressions.</p>
</s2>

<s2 name="fun" title="fun(arg1, ..., argn)" type="defun">
<p>Function calls.</p>

<p>The function library is in the <a href="xpath-fun.xtp">XPath function</a> section.</p>
</s2>

<s2 name="not" title="not(expr)" type="defun">
<p>Boolean not.</p>
</s2>

<s2 name="or" title="a or b" type="defun">
<p>Boolean or.</p>
</s2>

<s2 name="and" title="a and b" type="defun">
<p>Boolean and.</p>
</s2>

<s2 name="var" title="$var" type="defun">
<p>The value of a variable.  Variables, in general, only make
sense in a context like XSL.  Normal use of XPath outside of XSL will
not use variables.
</p></s2>

</s1>
  </body>
</document>
