Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to count the child nodes based on complex filter

Tags:

xml

xpath

I have an XML in the following format:

<ComRequest>
  <root lineId="1" creator="jumnix">
    <component lineId="101">
        <compLine lineId="1001">1</compLine>
        <compLine lineId="1002">2</compLine>
        <compLine lineId="1003">3</compLine>
        <compLine lineId="1004">4</compLine>
        <compLine lineId="1005">5</compLine>
        <compLine lineId="1006">6</compLine>
        <compLine lineId="1007">7</compLine>
        <compLine lineId="1008">8</compLine>
        <compLine lineId="1009">9</compLine>
        <compLine lineId="1010">10</compLine>
        <compLine lineId="1011">11</compLine>
    </component>
    <component lineId="102">
        <compLine lineId="1012">12</compLine>
        <compLine lineId="1013">13</compLine>
        <compLine lineId="1014">14</compLine>
        <compLine lineId="1015">15</compLine>
        <compLine lineId="1016">16</compLine>
        <compLine lineId="1017">17</compLine>
        <compLine lineId="1018">18</compLine>
        <compLine lineId="1019">19</compLine>
        <compLine lineId="1020">20</compLine>
        <compLine lineId="1021">21</compLine>
        <compLine lineId="1022">22</compLine>
    </component>
  </root>
</ComRequest>

I have a requirement to get the count of the 'component' nodes that have more than 10 'compLine' elements. Till now I have the following XPath query -

count(//*[local-name()='ComRequest']/*[local-name()='root']/*[local-name()='component']/*[local-name()='compLine' and count(self) gt 10])

But this does not work (gives a '0' result). Any help in getting this resolved is appreciated.

like image 637
Vini Avatar asked Jun 07 '11 19:06

Vini


2 Answers

How about count(//ComRequest/root/component[count(compLine)>10]) ?

like image 169
Bala R Avatar answered Sep 27 '22 15:09

Bala R


@Bala-R (+1) is correctly evaluated using a compliant XSLT 1.0 processor (Saxon):

count(//ComRequest/root/component[count(compLine)>10])

or, either

count(/*/*/*[count(compLine)>10])

Otherwise something is going bad in your tests, your context (different from the one provided in the question) or your xpath evaluator.

like image 40
Emiliano Poggi Avatar answered Sep 27 '22 17:09

Emiliano Poggi