Reputation: 187
I'm working with some XML that keeps all its data in attributes. I'm needing to extract that for processing the data. So we have something like:
<data>
<level1>
<level2 att1="1">
<level 3>
<level 4 att2="a" att3="b" att4="c" />
</level 3>
<level 3>
<level 4 att2="1" att3="2" att4="3" />
</level 3>
</level 2>
<level 2 att1="2">
So i'm wanting all attributes from the level 4 entries and their values, but ONLY from level2 entries where att1 = 1. I'm new to XPath, so I haven't worked it out yet. So far, the best I've come up with is:
/data/level1/level2[@att1='1']/level3/level4[@*]
but that is returning empty data. Any help would be appreciated.
Upvotes: 3
Views: 2909
Reputation: 29052
To get all the desired attribute values, you only have to make a little change to your XPath expression like this (remove the last predicate to directly access the attributes):
/data/level1/level2[@att1='1']/level3/level4/@*
Iterating over this expression's nodeset gives you all the desired values and, if wanted, their attribute's names.
Upvotes: 4