Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath select one type of nodes only in direct child nodes

Tags:

xpath

Maybe someone can help me find a solution to my problem. I need to perform an XPath query in the xml below that pulls only the "Field" nodes that are direct child nodes. In the below example, the query should pull fields E1F1, E1F2 and E1F3. So far I am running the query: //Field, but I get all fields (including the ones that belong to E1_1 which I don't want).

<Entity id="E1">
  <Field id="E1F1"></Field>
  <Field id="E1F2"></Field>
  <Field id="E1F3"></Field>
  <Entity id="E1_1">
    <Field id="E1_1F1"></Field>
    <Field id="E1_1F2"></Field>
    <Field id="E1_1F3"></Field>
  </Entity>

Thank you!!

like image 587
gmesorio Avatar asked Apr 04 '13 17:04

gmesorio


1 Answers

Use an absolute XPath:

/Entity/Field

// will match anywhere. If you use a single forwardslash, the match must be exact.

like image 176
unutbu Avatar answered Sep 18 '22 03:09

unutbu