[jdom-interest] does XPath implementation work correctly?

bob mcwhirter bob at werken.com
Sun Apr 29 10:08:47 PDT 2001


On Sun, 29 Apr 2001, Robert Koberg wrote:

> It does not return what I would expect after using XPath in writing many
> XSLT stylesheets.
> Here are some XPath examples (xml is at the bottom of the post):
> ----------------------------------------------------------------------------
> ---------------------
> Given an XPath expression of:
> XPath xpath = new XPath("/");
> returns...
> [[Document: [DocType: <!DOCTYPE section>] Root - [Element: <section/>]]]
> -- I would expect the entire document represented somehow

Um, it did return the entire document represented as an org.jdom.Document
object.  Did you expect differently?

> ------------------------------------
> given:
> XPath xpath = new XPath("/@id");
> returns...
> [[Attribute: id="root"]]
> -- I would expect this to return just 'root', but actually I would expect
> the next expression to return the 'root' attribute value
> -- there should be no value for this expression

You asked for the 'id' attribute on the root element.  Not the value,
but the entire Attribute object.

Though, I guess you're right, that the document root shouldn't have
attributes.  

> -------------------------------------
> given:
> XPath xpath = new XPath("//*[@id='docs_000000001']");
> returns...
> [[Element: <section/>]]
> -- I would expect this to return a representation of the child nodes of the
> first child section

Which you are getting.  org.jdom.Element represents the elements which have
an 'id' attribute of a particular value.

> -------------------------------------
> given:
> XPath xpath = new XPath("//*[@id='docs_000000001']/@folder-name");
> returns...
> [[Attribute: folder-name="docs"]]
> -- This seems correct but why doesn't it just return 'docs'?

Because the next step actually asks for the attribute 'folder-name'.

General thought:

You're familiar with XPath only in the context of XSLT, which 
does a lot of conversion to Strings for you.  XPath by itself
doesn't do that.  It returns the Nodes that you ask for, such
as Document, Element, or Attribute.

Please, re-read the W3C spec on XPath, and then comment if you
feel that werken.xpath doesn't conform.  Myself (the author) and
several others think that it seems to work-as-specified, for the
most part.

It seems fairly straightfoward to me what your XPaths are asking for
(a NodeSet of Elements, a NodeSet of Attributes, etc), but you seem
confused when you are given the results you asked for.

Just bear in mind that whatever is the last step in your expression
will typically tell you what kind of Node you're going to get back.

	/foo/bar/@baz

You're going to get org.jdom.Attribute objects

	/

You're going to get an org.jdom.Document object

	/foo/bar

You're going to get org.jdom.Element objects.

Am I missing something?

	-bob





More information about the jdom-interest mailing list