Reputation: 453
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!!
Upvotes: 13
Views: 18204
Reputation: 4858
In my case, the wanted node is far from the root element(the /html), so the accepted answer is not what I needed, after some search work, I find the child
axes instead of descendant
, I hope this may help someone who is use scrapy
to get some info from html.
Upvotes: 1
Reputation: 880717
Use an absolute XPath:
/Entity/Field
//
will match anywhere. If you use a single forwardslash, the match must be exact.
Upvotes: 26