Fetching two attributes of a xml file using xpath

387 Views Asked by At

I have a xml file which has a tag as below:

<locator xlink:type="locator" xlink:href="https://www.google.co.in/" xlink:title="Google" xlink:show="replace" xlink:actuate="onRequest" xlink:role="website" rank="1"> </locator>

There are many locator tag in the xml file with different roles and rank .

I am able to get the role of the above tag using @*[local-name()='role'.

Now I need to get the rank attribute based on the role. Is there any way to fetch two attributes and there values together?

I am new to Xpath . Please help me with this.

2

There are 2 best solutions below

1
On

Well //locator[@xlink:role = 'website']/@rank (with a suitable binding of the prefix xlink to the namespace http://www.w3.org/1999/xlink) is an example of selecting the rank attributes of locator elements where the role is website.

7
On

I am able to get the role of the above tag using @*[local-name()='role'.

Now I need to get the rank attribute based on the role. Is there any way to fetch two attributes and there values together?

Use:

ExprStartYouDidntShow/@*[local-name()='role' or name()='rank']

where ExprStartYouDidntShow is the expression selecting the elemen(s) whose two attributes should be selected.

Or, if you need to get these two attributes only when the role attribute has a particular value -- say "wantedValue", use:

   ExprStartYouDidntShow/locator[@*[local-name() = 'role'] = 'website']
                         /@*[local-name()='role' or name()='rank']