views:

12

answers:

1

I'm having trouble figuring out how to select data using a compound XPath query so that I can essentially find the 5th column of row in my data. Here is my example XML:

<sample>
 <OBX>
   <field position="1">1</field>
   <field position="2">My Value</field>
 </OBX>
 <OBX>
   <field position="1">2</field>
   <field position="2">My other (and more important) value</field>
 </OBX>
</sample>

Basically all I need to do is find the value of [field @position='2'] when [field @position='1'] is equal to 2. I am trying:

OBX[./field[@position='1']='2']/field[@position='2']

but this doesn't come up with any value. What would be the proper way to get this accomplished?

Thanks,

Mike

+1  A: 

I have verified that this XPath expression:

/*/OBX[field[@position='1']='2']/field[@position='2']

when evaluated against the provided XML document:

<sample>
 <OBX>
   <field position="1">1</field>
   <field position="2">My Value</field>
 </OBX>
 <OBX>
   <field position="1">2</field>
   <field position="2">My other (and more important) value</field>
 </OBX>
</sample>

does select the required node

Here is a demonstration, using XSLT as the XPath hosting language:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/OBX[field[@position='1']='2']/field[@position='2']"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied against the provided XML document, the correct result is produced:

<field position="2">My other (and more important) value</field>
Dimitre Novatchev