| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Resin 3.1 Documentation Examples Changes Overview Installation Configuration Quercus SOA/IoC JSP Servlets and Filters Admin (JMX) EJB Amber Security Performance Hessian XML and XSLT Third-party Troubleshooting/FAQ Introduction JAXP XML Path Language (XPath) XSLT Filter XSLT XPath Functions FAQ Scrapbook |
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. Java applications can use the com.caucho.xpath package to use XPath with an XML tree. 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. 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'. Basic PatternsThe 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. nodeSelects all child elements with nodeName of .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. @attrSelects the attribute .
@*Selects all attributes. ns:*Selects elements in the given namespace. Namespace patterns only make sense in the context of XSL, where the namespace declarations have been made. node()Matches an org.w3c.dom.Node. text()Matches a org.w3c.dom.Text node. comment()Matches a comment. processing-instruction()Matches a org.w3c.dom.ProcessingInstruction node. .Selects the current node. The current node is primarily useful for descendant patterns. for some filter patterns.
..Selects the parent of current node.
/Selects the document node. Useful for finding constants in a document.
a[expr]Select only those nodes matching which also satisfy the expression .The expression
a[n]Selects the th matching node matching 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.The position filter is equivalent to
a/bFor each node matching , add the nodes matching 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,
a//bFor each node matching , add the descendant nodes matching to the result. The '//' operator selects all descendants matching . The '/' operator selects all children matching .
//bReturns elements in the entire document matching .This is equivalent to /.// , but less weird.
a|bAll nodes matching or .Some example interpretations,
expr/pathUses an expression as the start of a path. Usually,
Node Axeschild::aThe child axis selects children of the current node. It is entirely
equivalent to the usual slash notation. So
descendant::aThe descendant axis selects descendants of the current node. It is
equivalent to '//'. So
descendant-or-self::aSelects descendants including the current node. attribute::aSelects attributes of the current element. It is equivalent to @ .
following-sibling::aSelects nodes after the current node.
preceding-sibling::aSelects nodes before the current node.
following::aSelects 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 following, preceding, ancestor and self partition the document. preceding::aSelects 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. following, preceding, ancestor and self partition the document. parent::aSelects the parent if it matches. The '..' pattern from the core is equivalent to 'parent::node()'.
ancestor::aSelects matching ancestors. following, preceding, ancestor and self partition the document.
ancestor-or-self::aSelects ancestors including the current node. self::aSelects the current node. '.' is equivalent to 'self::node()'. following, preceding, ancestor and self partition the document.
ExpressionspathSelects nodes based on the .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.
numberNumbers have the same syntax as Java doubles. "string"String literals can use single or double quotes. a = bStandard comparisons. 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.
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.
a + bArithmetic expressions.
(expr)Parenthesized expressions. fun(arg1, ..., argn)Function calls. The function library is in the XPath function section. not(expr)Boolean not. a or bBoolean or. a and bBoolean and. $varThe 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.
|