Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count of element in XPath

Tags:

xml

xpath

...
<element>
    <e:Element1 xmlns:e="mynamespace" > ... </.. >
    <e:Element1 xmlns:e="mynamespace" > ... </.. >
    <e:Element1 xmlns:e="mynamespace" > ... </.. >
    <a/>
</element>
...

and this XPath:

//*[local-name()='element']/count(*) return 4 what is OK. but now I wanna know count of element1 what is 3. I try a lot of possibilities but with no succes. I have to use local-name and namespace-uri()

like image 922
hudi Avatar asked Sep 12 '11 20:09

hudi


People also ask

How do you find the XPath count?

int product_count = driver. findElements(By. xpath("//id('product')/x:tbody/x:tr[1]")). size();

What is count in XPath?

XSLT/XPath Reference: XSLT elements, EXSLT functions, XPath functions, XPath axes. The count function counts the number of nodes in a node-set and returns an integer.

How do I find the XPath of an element in Python?

To find the XPath for a particular element on a page:Right-click the element in the page and click on Inspect. Right click on the element in the Elements Tab. Click on copy XPath.

What is Python XPath?

Xpath is one the locators used in Selenium to identify elements uniquely on a web page. It traverses the DOM to reach the desired element having a particular attribute with/without tagname. The xpath can represented by the ways listed below − //tagname[@attribute='value'] //*[@attribute='value']


Video Answer


2 Answers

You can try the following:

count(//element/Element1[namespace-uri()='mynamespace']) 
like image 82
Anton Vidishchev Avatar answered Oct 24 '22 15:10

Anton Vidishchev


If you are using XPath from an environment such as Java or C#, you should first bind a prefix to the namespace, which depends on the API you are using, but will be something like

xpath.declareNamespace("f", "mynamespace") 

and then evaluate the XPath expression

count(element/f:Element1) 

I deliberately chose a different prefix from the one in your source document just to show that you can use any prefix you like, but of course your code is more readable if you are consistent in your choice of prefixes.

like image 38
Michael Kay Avatar answered Oct 24 '22 16:10

Michael Kay