This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: XPath in Java Q?
- From: "Michael Kay" <michael dot h dot kay at ntlworld dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Thu, 13 Dec 2001 22:26:21 -0000
- Subject: RE: [xsl] XPath in Java Q?
- Reply-to: xsl-list at lists dot mulberrytech dot com
> I am not sure if this is the right place for this post, but
> anyway I would
> like to ask if there is a way how to query xml document in
> Java using XPath
> (e.g. some free library etc.).
>
Quite a few products offer this capability.
You can do it in Saxon, though it's perhaps a little bit cumbersome:
Expression exp = Expression.make("2+2", new StaticContext());
Context cxt = ((Controller)transformer.newContext();
System.err.println(exp.evaluateAsString(cxt));
will output "4".
The "transformer" is obtained from JAXP in the normal way. Really, this is
intended primarily for use within XSLT extension functions, where the
"context" object will be supplied in the calling parameters. But you can
also use it in a freestanding application, as illustrated in the
ShowBooks.java sample application issued with the product.
Saxon's XPath processor will run against Saxon's own internal tree format,
or against a JDOM tree. It won't run against a third-party DOM, unless you
do a lot of interfacing work.
Mike Kay
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list