views:

36

answers:

1

Hi,

I have two XPaths, for example:

/html/body/div[3]/div[4]/table/tbody/tr[2]/td[4] and
/html/body/div[3]/div[4]/table/tbody/tr[2]/td[3]/a[2]

How can I find the first common ancestor now? In this case it would be /html/body/div[3]/div[4]/table/tbody/tr[2]/.

I found this question but the solution is in ruby and doesn't really give me a clue how to do it in Javascript. Can anybody help me? Thank you.

A: 

This seems to be for JS and not XSLT, although the common factor being XPATHs of the elements.

Ravindra Sane
This is about an XPath expression, no matter what is the host language. If the answer depends on host language, then the question would be about an specific XPath engine and it should be retagged.
Alejandro
@Alejandro, I'm not sure I agree... this could be solved without using an XPath engine at all, depending on what we can assume about the input paths. E.g. best case, we could just take the longest common string prefix, and roll back to the last `/`. That's assuming that the XPath for a given node always follows a canonical form, which is sometimes true.
LarsH
@LarsH: I think the question is about to get a node set result. If the question ends up to be about strings, then yes, it couldn't be done in XPath 1.0, unless you provide an extension function for dealing with recursion. It would be simple with XSLT, of course.
Alejandro