XPath to count the child nodes based on complex filter
24,904
Solution 1
How about count(//ComRequest/root/component[count(compLine)>10])
?
Solution 2
@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.
![Vini](https://i.stack.imgur.com/Vhtpt.jpg?s=256&g=1)
Author by
Vini
FullStack Professional working mainly on Java/Spring/Angular/Mongo... Keen interest in learning and implementing new technologies.
Updated on June 08, 2020Comments
-
Vini about 4 years
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.
-
Vini about 13 yearsThat does not work unfortunately. I have to use the 'local-name()' in the XPath as the gateway where I am supposed to configure this does not handle the XML nodes if is has any attributes.
-
Bala R about 13 years@Vinnie, how about
count(//*[local-name()='ComRequest']/*[local-name()='root']/*[local-name()='component'][count(*) > 10])
? -
Vini about 13 yearsthe example works if I remove the node attributes. But with the attributes, the result is coming as '0'. I have taken this up with the team and waiting for them to response.
-
Vini about 13 yearsAppears that there was some issue with the XSLT processor on the gateway. Now its resolved and the XPath expression evaluates perfectly. Thanks a lot for your help Bala.
-
Vini about 13 yearsAs you expected, there was some problem at the XSLT processer end. Now it works fine. Thanks a lot for your time and help - appreciate it.
-
Vini almost 13 yearsHere is what the gateway SME change the XPath to- count(//*[local-name()='ComRequest]/*[local-name()='root']/*[local-name()='component'][count(*[local-name()='compLine') > 10])