Sunday, 19 April 2015

Select elements from XML using Xpath



I have a huge XML file. Looks somthing like the following



<component name='comp1'>
<child> ... </child>
<child> ... </child>
<optional id='1'> ... </optional>
</component>

<component name='comp2'>
<child> ... </child>
<child> ... </child>
<optional id='1'> ... </optional>
<optional id='1'> ... </optional>
<optional id='2'> ... </optional>
</component>


I want to select optional elements based on their ids from each component without repeating any id within the same component.


Simply, I want the following output for comp2, for example:



Comp2
1
2


My problem is to select optional elements that has similar id one time only.


I am using Xpath with JET1 (java Emitter Template) I am iterating each component looking for optional elements and printing their text(). However, my loop is resulting in the following



Comp2
1
1
2


I want to eliminate duplicated results.


My JET temple:



<c:iterate select="//component/optional" var="Optional">
<c:get select="$Optional"/>
</c:iterate>

No comments:

Post a Comment