OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: XPath calculation



At 15:42 09/02/2001 -0600, Vakulenko, Andrey V wrote:
>I am looking for Java implementation of an algorithm for finding the most
>generic and short XPath expression for a selected element in DOM (XML
>document). Any ideas and recommendations are welcomed.

You'd need to specify your requirements a little more. Do you mean an XPath
that is certain to match that element (and potentially others) or one that
matches that one and only that one ? I guess it's the latter because the
former is ultra-trivial (unless you have specific needs not stated here).

Also, is it possible that your DOM would change or is it for a DOM that
won't change ?

The first thing I'd do (I think) is check to see if that element has an id,
and if so use that. A properly optimized get_element_by_id() will get it
fast, and it certainly makes for the shortest (I think) XPath expression
that'll select that element. If your element doesn't have an idea but one
of it's parent's does, then you could start selecting from that parent.
Likewise if it has a sibling or even a child with an id.

Otherwise, a path listing all the parent elements + their position and the
element + it's position might be the most generic way.

If you have a schema for your document then you can probably apply specific
rules, it all depends on your needs and what you have at hand.

Amaya displays the path to the current element in the context bar. I don't
remember if it's expressed as XPath but it isn't far from it for sure. You
might want to take a look at it (though obviously it isn't Java).

-- robin b.
There are trivial truths and there are great Truths. The opposite of a
trival truth is obviously false. The opposite of a great Truth is also
true.  -- Neils Bohr