Need help with an xpath query!

Posted by gerrod on Stack Overflow See other posts from Stack Overflow or by gerrod
Published on 2010-04-21T13:31:24Z Indexed on 2010/04/21 13:33 UTC
Read the original article Hit count: 210

Filed under:

Can anyone help with the following XPath question? Given the node-set:

<table> 
  <rows>
    <row>
      <value column="Product">Coal</value>
      <value column="Quantity">10000</value>
    </row>
    <row>
      <value column="Product">Iron</value>
      <value column="Quantity">5000</value>
    </row>
    <row>
      <value column="Product">Ore</value>
      <value column="Quantity">4000</value>
    </row>
  </rows>
</table>

I want to query to find the node sub-set with a given product name. Note that the product name is being supplied by an attribute of the current node being processed (i.e. "@name"). So when the @name attribute has the value of "Coal" I would expect this to be returned:

<row>
  <value column="Product">Coal</value>
  <value column="Quantity">10000</value>
</row>

This is what I've come up with; I know it's wrong, because I don't get anything back.

$table/rows/row[value[@column='Product'][text()=@name]]

</code>

© Stack Overflow or respective owner

Related posts about xpath