in reply to Want to fetch inner most child element first

XML::XPath is old and unmaintained. I prefer XML::LibXML, which has a similar API. In XML::XSH2, a wrapper around it, you can easily do
for &{ sort :d :k count(ancestor::*) //mml:msub } ls . ;

Output:

<mml:msub>swift</mml:msub> Found 1 node(s). <mml:msub> <mml:mrow> <mml:mi>poland</mml:mi> </mml:mrow> <mml:mrow> <mml:msub>swift</mml:msub> <mml:mi>a</mml:mi> </mml:mrow> </mml:msub> Found 1 node(s). <mml:msub> <mml:mrow> <mml:mi>China</mml:mi> </mml:mrow> <mml:mrow> <mml:msub> <mml:mrow> <mml:mi>poland</mml:mi> </mml:mrow> <mml:mrow> <mml:msub>swift</mml:msub> <mml:mi>a</mml:mi> </mml:mrow> </mml:msub> </mml:mrow> </mml:msub> Found 1 node(s). <mml:msub> <mml:mrow> <mml:mi>Canada</mml:mi> </mml:mrow> <mml:mrow> <mml:msup> <mml:mrow> <mml:mi>police</mml:mi> </mml:mrow> <mml:mrow> <mml:mi>bangalore</mml:mi> </mml:mrow> </mml:msup> </mml:mrow> </mml:msub> Found 1 node(s).

Directly in XML::LibXML:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::LibXML; my $xml = 'XML::LibXML'->load_xml(location => '/path/to/file.xml'); say for map $_->[0], sort { $b->[1] <=> $a->[1] } map [ $_, $_->findvalue('count(ancestor::*)')], $xml->findnodes('//mml:msub');
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ