Here are some useful Xpath expression which were a little hard to figure out for a newbie like me so I am making an attempt to document them. Here are 2 of them,
I want to fetch or retrieve unique build numbers from above XML using xpath, it is very clear that i can not use a simple xpath expression like
This will return all the build numbers. So clearly I need to put some comparison logic to get unique build number values. Simple way is to use xpath axis called preceding. This axes will return all the preceding nodes that appear in that XML document before the current one,
But this may be overkill or may sometimes return results that you are not expecting as preceding will look through the entire document that appears before that node and not just at that level. If you want to query only on the current level, you should use another xpath axis called sibling, to be specific - following-sibling and preceding-sibling. The xpath will like this
The above expression seems to be complete but has a flow, if you run it over above XML, it will not return the build number for feature=devportal. Why? because that build number appears on both sides of that node. To save this, you can add another condition and your final expression will be,
Getting leaf nodes only using xpath can be done by yet another xpath axis called Child. This is how it can be done,
- Get unique values using xpath
- Get leaf node only using xpath
I want to fetch or retrieve unique build numbers from above XML using xpath, it is very clear that i can not use a simple xpath expression like
This will return all the build numbers. So clearly I need to put some comparison logic to get unique build number values. Simple way is to use xpath axis called preceding. This axes will return all the preceding nodes that appear in that XML document before the current one,
But this may be overkill or may sometimes return results that you are not expecting as preceding will look through the entire document that appears before that node and not just at that level. If you want to query only on the current level, you should use another xpath axis called sibling, to be specific - following-sibling and preceding-sibling. The xpath will like this
The above expression seems to be complete but has a flow, if you run it over above XML, it will not return the build number for feature=devportal. Why? because that build number appears on both sides of that node. To save this, you can add another condition and your final expression will be,
Getting leaf nodes only using xpath can be done by yet another xpath axis called Child. This is how it can be done,
( 0 Votes )
| Comments |
|
Only registered users can write comments!
Powered by !JoomlaComment 4.0alpha3



























