ainternet73
ainternet73

Reputation: 117

Counting special text of children in xpath

I have this xml document :

    <AAA> 
      <CCC> 
           <BBB>SALAM</BBB>
           <BBB>HALET KHUBE</BBB> 
           <BBB>HEY</BBB> 
           <TTT>SALAM</TTT>
           <GGG/>
           <ZZZ/>
      </CCC> 
      <DDD> 
           <BBB>GOOD</BBB> 
           <BBB>BOOO</BBB> 
           <GGG/>
           <GGG/>
           <ZZZ/>
      </DDD> 
      <EEE> 
           <CCC>TAKE</CCC> 
           <DDD>LEAVE</DDD>
           <ZZZ/>
           <ZZZ/>
           <ZZZ/> 
      </EEE> 
 </AAA>

I want to select nodes that have 2 children with text = "SALAM" ( that node can have any number of children but it must have 2 chidren with text = "SALAM") .For example in above xml ,the xpath query must return CCC tag. please help me. thanks

Upvotes: 2

Views: 977

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

Use:

/*/*[count(*[text() = 'SALAM']) = 2 ]

It may be more efficient to use the following:

/*/*
  [*[text() = 'SALAM'][2]
 and
   not([*[text() = 'SALAM'][3])
  ]

Always when possible try to avoid using the // pseudo-operator, because in many cases it can be very slow.

Upvotes: 1

choroba
choroba

Reputation: 241748

Use the count() xpath function:

//*[count(./*[text()="SALAM"])=2]

Upvotes: 1

Related Questions