Friday, January 23, 2015

Eclipse xpath parser fails to search elements by the tag names

For those who encountered the same issue (when you try to access the element using xpath by the node name in Eclipse built-in parser) I would recommend either to remove namespace definition from the document at all or remove the default namespace.
Example:

<apprepo xmlns="asdasd">
<app name="good-app">
<appinfo>
<author name="John Smith"/>
<stores>
<store name="goo" price="1"/>
<store name="app" price="1.5"/>
</stores>
</appinfo>
</app>
<app name="another-good-app">
<appinfo>
<author name="James Smith"/>
<stores>
<store name="goo" price="2"/>
</stores>
</appinfo>
</app>
</apprepo>

Query: //appinfo returns "no matches"

Let's now change the namespace so that it is bound to some prefix (let's even not use that prefix in our document)

<apprepo xmlns:pref="asdasd">
<app name="good-app">
<appinfo>
<author name="John Smith"/>
<stores>
<store name="goo" price="1"/>
<store name="app" price="1.5"/>
</stores>
</appinfo>
</app>
<app name="another-good-app">
<appinfo>
<author name="James Smith"/>
<stores>
<store name="goo" price="2"/>
</stores>
</appinfo>
</app>
</apprepo>

Result: query returns all the appinfo nodes found

P.S. - the proper parsing happens even if you completely get rid of the namespace attribute. So Eclipse xpath parser is not working with the documents having the default namespace set.

No comments:

Post a Comment