Reputation: 5296
XPath newbie here.
Is there a way to select a list of attr1
and child3
pairs using XPath from the sample XML below?
In other words I need a list of a11, c13
, a21, c23
, etc.
Or I can only do //parent
and pick required values from the array of resulting nodes?
<parentList>
<parent attr1="a11" attr2="a12">
<child1>c11</child1>
<child2>c12</child2>
<child3>c13</child3>
<child4>c14</child4>
<child5>c15</child5>
</parent>
<parent attr1="a21" attr2="22">
<child1>c21</child1>
<child2>c22</child2>
<child3>c23</child3>
<child4>c24</child4>
<child5>c25</child5>
</parent>
</parentList>
Upvotes: 1
Views: 51
Reputation: 5905
XPath 1.0 solution to select the elements of interest :
//parent/descendant-or-self::*[position()=1 or position()=4]
But, to generate a list of data you should use the following one :
(//@attr1|//text()[normalize-space()])[parent::parent or parent::*[parent::parent][count(preceding-sibling::*)=2]]
Look for an attribute or text with : parent
element as parent or child of parent
as parent and two preceding siblings.
Output : a11,c13,a21,c23
Upvotes: 0
Reputation: 24930
This xpath 2.0 expression (and properly closing your sample html)
//parent/concat(@attr1,' ',child3/text())
should output:
a11 c13
a21 c23
Upvotes: 2