Non Deterministic XPath Expression

I have an xml that has an element which is recursive. A category node has it’s own definition and also the ancestors of that category. Those ancestors repeat this structure till the top most category. To make things complicated, a product can have multiple category paths. In order to identify the top-most category, I used

//Category[position()=1]/descendant::Category[last()]/Name

This worked fine in Java. But when I tried the same in Perl (XML::XPath), it didn’t work. It turned out, the inner most Category happened to be the first one within the descendant::Category nodeset. So, using

//Category[position()=1]/descendant::Category[position()=1]/Name

it worked. But I wanted an xpath expression that is language independent.

So, I ended up using

//Category[position()=1]/descendant::Category[count(Ancestors)=0]/Name

I am not sure if the perl implementation of the xpath is incorrect or if the xpath specification is unclear on the ordering of the nodes for certain type of expressions.

Leave a comment

Filed under XPath

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s