[jdom-interest] Element Reference from Attribute

Patrick Dowler Patrick.Dowler at nrc.ca
Fri Nov 17 09:03:56 PST 2000


On Fri, 17 Nov 2000, you wrote:
> > > We should have a way to get the Element reference from the Attribute.  I was
> > > trying to design DOM Level2 feature of Events incorporating in JDOM this
> > > requires all possible navigation capabilities from any node to root node.
> > > Has any one thought of implementing DOM Level2 for JDOM?  If we think DOM
> > > Level2 we must provide a way to get the total path to the root from any node
> > > including PI's, Attributes and even from CDATA sections.  Plz put forward
> > > your thoughts on this.
> > 
> > I guess I missed something early in this discussion, so this is probably a
> > dumb question. At what point in your code do you have a reference to 
> > an Attribute that you didn't get via
> > 
> > 	Attribute a = e.getAttribute(key);
> > 
> > That is, how is it that one can be navigating from an Attribute with no
> > knowledge of the Element with said Attribute?
> 
> Using my XPath library, for one.
> 
> 	XPath xpath = new XPath("/foo/bar/baz/@goober");
> 
> 	List results = xpath.applyTo( myDoc );

Ok. That is pretty straightforward. But then again, you did ask for the
"goober" Attributes, not the Elements with "goober"s :-)

> You're now holding a list of Attributes named 'goober', with no
> convenient way to find the parent.  (Which also means I can't implement
> easily the XPath of "/foo/bar/baz/@goober/..", which would automagically
> return the parents of all 'goober' attributes).

Aahh... Here you do want the Elements with "goober"s. When you say you 
cannot implement it, you are saying you can't implement it on top of 
XPath("/foo/bar/baz/@goober") - ie. get the attributes and then traverse - but 
you could implement it directly since you can keep both the Element and 
Attribute references handy while traversing... yes? namely,

	Element e = ...
	Attribute a = e.getAttribute("goober");
	if (a != null )
		addToList(e);

The question still reduces to "do we ever have only an Attribute reference
and want the Element?" 

-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list