[jdom-interest] does XPath implementation work correctly?

Robert Koberg rob at koberg.com
Sun Apr 29 10:44:40 PDT 2001

Thanks for the basic explanation. I am new to java... I am trying to see
what benefit I can achieve by replacing some things I currenlty do with XSLT
with java manipulations. I have been told that JDOM is the way to go (???).
There is definitely a steep learning curve...

Is there any documentation that will help me get through the 'basic'
questions for JDOM and XPath within JDOM? I can only find the API's and I
could really use some examples of what I need to do.

Two main questions I have are:

1. What do I need to do to get the value of the attribute after applying
this expression:
XPath xpath = new XPath("//*[@id='docs_000000001']");

2. After getting the result "[[Element: <section/>]]" using the expression:
XPath xpath = new XPath("//*[@id='docs_000000001']");

How can I add the following:

   <pointer fileref="foo.gif"/>

-- Is there a more direct way to insert this chunk of XML into the exact
position I need (without XPath)?

I will be glad to put your work through its paces if I can get over these
basic hurdles.

----- Original Message -----
From: "bob mcwhirter" <bob at werken.com>
To: "Robert Koberg" <rob at koberg.com>
Cc: <jdom-interest at jdom.org>
Sent: Sunday, April 29, 2001 10:08 AM
Subject: Re: [jdom-interest] does XPath implementation work correctly?

> 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
> > 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
> > first child section
> Which you are getting.  org.jdom.Element represents the elements which
> 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