I have an XML Like the following, and i want select just the elements “b” and “c” childrens of “a” using xpath filtering in xslt
<a>
<b>data_1</b>
<c>data_1</c>
<d>data_1</d>
<e>data_1</d>
</a>
<a>
<b>data_2</b>
<c>data_2</c>
<d>data_2</d>
<e>data_2</d>
</a>
<a>
<b>data_3</b>
<c>data_3</c>
<d>data_3</d>
<e>data_3</d>
</a>
Suggestions?
First, do notice that you haven’t provided a well-formed XML document. By definition a well-formed XML document must have a single top element.
I assume in this answer, that all
aelements are children of this single top element, that isn’t shown in the question.Use:
This selects any element that is either
borcand that is a child of anyaelement that is a child of the top element of the XML document.Do note that the currently-accepted answer is incorrect:
Not only it supposes that there is a single
atop element (and there isn’t such), but given a specific XML document, it would select, besides the wanted elements, also anyaelement that is a child of the top elementa.The XPath expression that I recommend above:
is more efficient than another, correct XPath expression that is proposed in one of the other answers:
This requires evaluatiing separately
/a/b/and/a/cand then performing the set-union of the results of the two evaluations.The XPath expression that I recommend needs only a single scan of the document, requires no union and can be used even in streaming mode over an unlimited in size, huge XML document.