Use xmlstarlet inside an if loop

I have a XML file of little huge size. I have to build a logic to get the count of the tag

<capacity>

.

And have an if loop such that all the <capacity> blocks are captured one after the other.

sample input file - sample1.xml

<subcolumns><capacity><name>45.90</name>
<index>0</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
<capacity><name>57.09</name>
<index>1</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
<capacity><name>55</name>
<index>2</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
</subcolumns>

Please find the below snippet for achieving my goal

#!/bin/bash
set -x
sensor_count=`grep -c "<capacity>" sample1.xml`
for ((i=1;i<=$sensor_count;i++));
do
        xmlstarlet sel -t -c '//capacity["$i"]' -n sample1.xml
done

Now the problem is when I use the $i variable along the xmlstarlet command it is printing the entire file rather than
only the occurence.

I don't know about the xmlstarlet syntax, but at any rate, the use of single quotes is preventing the variable i from being expanded.

So try changing :

'//capacity["$i"]'

to

"//capacity[\"$i\"]"