Reputation: 96
I want to know the number of persons with "Lot"
in their addres, from this XML doc using Xpath.
<?xml version="1.0" encoding="UTF-8"?>
<personnes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="personne.xsd">
<personne id="n1" pays="MA">
<nom>Ayoubi</nom>
<prenom>Nezard</prenom>
<dat_naiss>1999-02-05</dat_naiss>
<telephone>+21266666666</telephone>
<adress>
Lotxxx Num: 38
<ville>Rabat</ville>
</adress>
</personne>
<personne id="n11" pays="Fr">
<nom>Karimi</nom>
<prenom>Hamdani</prenom>
<dat_naiss>2000-05-07</dat_naiss>
<telephone>+21266666666</telephone>
<adress>
rue xxx Num: 18
<ville>Grenoble</ville>
</adress>
</personne>
</personnes>
I Tested my Xpaths here : here
I tried so many Xpaths but i don't know how to apply the count function on this Xpath : //adress/contains(text()[1],"Lot")
that returns to me :
Boolean='true'
Boolean='false'
Upvotes: 1
Views: 46
Reputation: 111686
This XPath,
count(//personne[adress[contains(.,'Lot')]])
will count the number of personne
elements with an adress
child whose string-value contains a "Lot"
substring. This will include "Lot"
substrings wrapped within other markup.
This XPath,
count(//personne[adress/text()[contains(.,'Lot')]])
will do the same but exclude "Lot"
substrings wrapped within other markup.
Both XPath expressions work with XPath 1.0 and up.
Upvotes: 2
Reputation: 29042
In XPath-1.0, the expression would have to be
contains(//adress/text()[1],"Lot")
This would give you the result of true
. To get the result of true
and false
, you would have to iterate over the //adress/text()[1],"Lot"
nodes, preferably with an xsl:for-each
.
Following up, you can get the count of the personne
elements that contain the string Lot
in its adress
child by using the following expression:
count(contains(//personne/adress/text()[1],"Lot"))
Its result should be 1
.
Upvotes: 1