0

I have a this xml:

<?xml version="1.0"?>
<catalog>
   <car>
      <id>0</id>
      <color>green</color>
      <color>red</color>
      <color>yellow</color>
      <vip>
        <user>Trump</user>
        <user>Obama</user>
        <user>Merkel</user>     
      </vip>
   </car>
   <car>
      <id>1</id>
      <color>green</color>
      <color>red</color>
      <color>yellow</color>
      <vip>
        <user>Putinski</user>
        <user>Orlovski</user>
        <user>Idiotski</user>       
      </vip>
   </car>
   <car>
      <id>2</id>
      <color>green</color>
      <color>red</color>
      <color>yellow</color>
      <vip>
        <user>Clooney</user>
        <user>Lopez</user>
        <user>Ford</user>       
      </vip>
   </car>
</catalog>

And I am fighting with some simple things:

a) count the "color" nodes from car id 0

b) retrieve Obama's car id

For a) I know how to identify car id 0

/catalog/car/id=0

gives me a TRUE - so this is the proof I am on the right track. But now how can I continue counting the "color" nodes based on car id 0? The solution postet here does not work, as well as the following-sibling results in an javax.xml.transformerException. Does anybody know how to solve this?

1 Answers1

0

To count the color nodes in car with id = 0 you can use

count(/catalog/car[id="0"]/color)

Returns 3

To get Obama's car id:

/catalog/car[.//user="Obama"]/id/text()

Returns 0

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Hello Andersson, many thanks for your reply. Indeed your count statement works and is a great help. Your second statement also worked after I replaced " with ' (this was necessary due to my xpath.jar I am using) Again, great and straight forward help! – Anton Grindplak May 22 '17 at 15:05