1

I'm trying to figure this out in Whitebeam's testbed based on this other question but i just can't get it to work. I'm trying to select the "pet" element that has a "description" child with a "Large dog!" value.

This is my best bet but it doesn't work and i don't get why:

./pet[./description = 'Large dog!'] 

And here is the testbed's XML in case you dont want to open the link above.

<documentRoot>
<!-- Test data -->
<?value="2"?>
<parent name="data" >
   <child id="1"  name="alpha" >Some Text</child>
   <child id="2"  name="beta" >
      <grandchild id="2.1"  name="beta-alpha" ></grandchild>
      <grandchild id="2.2"  name="beta-beta" ></grandchild>
   </child>
   <pet name="tigger"  type="cat" >
      <data>
         <birthday month="sept"  day="19" ></birthday>
         <food name="Acme Cat Food" ></food>
      </data>
   </pet>
   <pet name="Fido"  type="dog" >
      <description>
         Large dog!
      </description>
      <data>
         <birthday month="feb"  day="3" ></birthday>
         <food name="Acme Dog Food" ></food>
      </data>
   </pet>
   <rogue name="is this real?" >
      <data>
         Hates dogs!
      </data>
   </rogue>
   <child id="3"  name="gamma"  mark="yes" >
      <!-- A comment -->
      <description>
         Likes all animals - especially dogs!
      </description>
      <grandchild id="3.1"  name="gamma-alpha" >
         <[CDATA[ Some non-parsable character data ]]>
      </grandchild>
      <grandchild id="3.2"  name="gamma-beta" ></grandchild>
   </child>
</parent>
</documentRoot>

Any help or even just pointing me at the right direction is much appreciated

1 Answers1

0

Whitespace and axis misunderstanding are thwarting your attempt. Use normalize-space() and the descendant-or-self:: axis (//) to eliminate the problems...

This XPath

//pet[normalize-space(description) = 'Large dog!'] 

will select all pet elements whose description child element has a space-normalized string value of 'Large dog!'.

kjhughes
  • 89,675
  • 16
  • 141
  • 199