It might be harder than it seems.
  1. XML can contain (among other things) attributes, comments, and processing instructions. Are you sure you want to include their contents into the statistics?
  2. ISO entities are not part of the XML. There probably is some DTD that defines them, but as they are not standard (in XML), it might be hard to process them properly (and the DTD might define them in a non-standard way). Moreover, the section mark can be also included in XML as § (or &#xA7, or §), and Art can be repesented as Art, for example.

See this example (using PRE instead of CODE to include the section mark):


#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use experimental qw( signatures );
use utf8;

use XML::LibXML;
use Encode qw{ encode };

sub create_xml($xml) {
    open my $out, '>:encoding(UTF-8)', $xml or die $!;
    print {$out} <<~'__XML__';
    <?xml version="1.0"?>
    <!DOCTYPE root [
        <!ENTITY sect "§">
    ]>
    <root link="Art.VV">
        A &sect; 1 A
        B Art.XVI B
        C §  9 C
        D &#xa7; 7 D
        E &#167; 6 E
        <!-- Should comments be included in statistics? Art.XXX -->
        <?print "Should processing instructions be included?" Art.2 ?>
    </root>
    __XML__
}

sub validate_xml($xml) {
    my $dom = 'XML::LibXML'->load_xml(location => $xml);
    print $dom;
}

sub generate_statistics($xml) {
    my @regexes = (qr/§\s*[0-9]/, qr/Art\.\s*[0-9IVX]/);

    open my $in, '<:encoding(UTF-8)', $xml or die $!;
    my $string = do { local $/; <$in> };
    my @tally;
    for my $i (0 .. $#regexes) {
        my $regex = $regexes[$i];
        ++$tally[$i] while $string =~ /$regex/g;
    }
    for my $i (0 .. $#regexes) {
        say encode('UTF-8', "$regexes[$i]:\t$tally[$i]");
    }
}

my $xml = '1.xml';
create_xml($xml);
validate_xml($xml);
generate_statistics($xml);
unlink $xml;

The output:
<?xml version="1.0"?>
<!DOCTYPE root [
<!ENTITY sect "§">
]>
<root link="Art.VV">
    A &#xA7; 1 A
    B Art.XVI B
    C &#xA7;  9 C
    D &#xA7; 7 D
    E &#xA7; 6 E
    <!-- Should comments be included in statistics? Art.XXX -->
    <?print "Should processing instructions be included?" Art.2 ?>
</root>
(?^u:§\s*[0-9]):	1
(?^:Art\.\s*[0-9IVX]):	4

You see? The section mark was not counted, the attribute, comment, and processing instruction were. Probably not what you want.

Update: Included &#xa7;.

Update 2: Print the XML to show how some representations of the section mark are equivalent.

Update 3: Added an attribute.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re^2: Entity statistics by choroba
in thread Entity statistics by LexPl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.