count of element in XPath

107,982

Solution 1

You can try the following:

count(//element/Element1[namespace-uri()='mynamespace'])

Solution 2

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.

Share:
107,982
hudi
Author by

hudi

Updated on March 21, 2020

Comments

  • hudi
    hudi over 4 years
    ...
    <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()