pheromix
pheromix

Reputation: 19347

How to get node having a child node having a specified text?

There is a xml :

<mgns1:Champ_supplementaire>
  <mgns1:CODE_CS>1</mgns1:CODE_CS>
  <mgns1:VALEUR_CS>2</mgns1:VALEUR_CS>
</mgns1:Champ_supplementaire>
<mgns1:Champ_supplementaire>
  <mgns1:CODE_CS>2</mgns1:CODE_CS>
  <mgns1:VALEUR_CS>M</mgns1:VALEUR_CS>
</mgns1:Champ_supplementaire>
<mgns1:Champ_supplementaire>
  <mgns1:CODE_CS>3</mgns1:CODE_CS>
  <mgns1:VALEUR_CS>LOC</mgns1:VALEUR_CS>
</mgns1:Champ_supplementaire>

I want to get the node mgns1:Champ_supplementaire having a child mgns1:CODE_CS which text's is 2. How to do that ?

I tried

NodeList nodeliste_cs2 = (NodeList) xpath.evaluate( "//mgns1:Champ_supplementaire[//mgns1:CODE_CS=2]//mgns1:VALEUR_CS",doc, XPathConstants.NODESET);

Upvotes: 1

Views: 26

Answers (1)

Andersson
Andersson

Reputation: 52685

//node_foo[//node_bar=2]

means select first found node_foo if there is a node_bar with value 2 anywhere in DOM

//node_foo[node_bar=2]

means select first found node_foo if it has its own child node_bar with value 2

So you need

"//mgns1:Champ_supplementaire[mgns1:CODE_CS=2]/mgns1:VALEUR_CS"

Upvotes: 1

Related Questions