JXPath 2.0 is no longer using Xalan combined with a light-weight "DOM" struture for interpretation of XPaths. The old model has some limitations. As a result of this change, there are many more XPaths that will work properly now.
In addition to getting/setting values using XPaths, JXPath 2.0 can evaluate an XPath into a Pointer or a set of Pointers. A Pointer is a simple value accessor that can be used repeatedly to get/set value of an object in the graph. More importantly, it can produce a new XPath that precisely describes the object's location in the graph. For example,
Pointer ptr = ctx.locateValue("employees[$i]/addresses[$j]");
will produce a Pointer that has concrete values of $i
and $j
.
Therefore,
ptr.asPath()
will produce something like
"/employees[1]/addresses[3]"
One possible application of these concrete paths is for names of form elements
on dynamically generated HTML pages. When the POST with the values for these
elements arrives to the server, all we need to do is call JXPath.setValue()
with the concrete XPaths and the corresponding values.
JXPath 2.0 fully supports extension functions. Extension functions can be implemented directly or as Java classes or packages. You can now use method calls and create objects directly from XPath expressions. For example, here's how you can call a method:
"bestStudent($school, $grade)"
This is equivalent to
school.bestStudent(grade)
To create a new object use the keyword "new":
"'Current time is ' + java.util.Date.new()"
To call a static method:
"java.lang.System.getProperty('companyName')"
You can also register functions with namespaces, which allow you to use abbreviated syntax like:
"sys:getProperty('companyName')"
The new syntax for accessing dymanic properties (e.g. map values) is:
"map[@name = expression]"
You can still use the syntax introduced by JXPath 1.0, if the key is know upfront and is a proper XPath identifier:
"map/key"
Indexed JavaBeans properties are now supported in accordance with the JavaBeans specification. If indexed read/write methods are missing, JXPath attempts to modify the collection directly.
JXPath 2.0 is a complete re-write of the implementation. The dependency on Xalan has been eliminated. JXPath 2.0 features its own parser/compiler/interpreter that are specifically tailored to the needs of Java object graph traversal.
JXPath 2.0 accesses object fields directly by name rather than "searching" for them. For example if you have an object with a property that is a long array followed by a property that is an int, JXPath 1.0 would enumerate through the long array before it found the integer property. JXPath 2.0 jumps directly to that variable.
JXPath 2.0 caches results of compilation of XPaths. This way the overhead of calling the parser/compiler is significantly reduced.
JXPath 2.0 avoid multiple computations of the same intermediate expression.
For example, in "//person[name
= $firstName + ' ' + $lastName]"
the concatenation of the string
will only be performed once.