Article View: fr.comp.text.xml
Article #9793Re: shallow parsing : REX
From: Nicolas Chuche
Date: Mon, 05 Feb 2001 14:52
Date: Mon, 05 Feb 2001 14:52
86 lines
3755 bytes
3755 bytes
Cyrille Giquello <cyrille@ktaland.com> disait le
02/05/01 � 14:39:59 que :
> Est-ce que quelqu'un a une piste ?
> Pour parser du XML sans compiler un parser (xeres, expat ...).
> Afin de manipuler des p'tits fichier de config !!
Attention, �a n'est pas un parser, il ne fait que mettre tout dans un
jolie tableau. A toi de r�cup�rer toutes les donn�es ensuite.
Un exemple :
% cat rex.pl
#!/usr/bin/perl -w
# REX/Perl 1.0
# Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions",
# Technical Report TR 1998-17, School of Computing Science, Simon Fraser
# University, November, 1998.
# Copyright (c) 1998, Robert D. Cameron.
# The following code may be freely used and distributed provided that
# this copyright and citation notice remains intact and that modifications
# or additions are clearly identified.
use strict;
my $TextSE = "[^<]+";
my $UntilHyphen = "[^-]*-";
my $Until2Hyphens = "$UntilHyphen(?:[^-]$UntilHyphen)*-";
my $CommentCE = "$Until2Hyphens>?";
my $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+";
my $CDATA_CE = "$UntilRSBs(?:[^\\]>]$UntilRSBs)*>";
my $S = "[ \\n\\t\\r]+";
my $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
my $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
my $Name = "(?:$NameStrt)(?:$NameChar)*";
my $QuoteSE = "\"[^\"]*\"|'[^']*'";
my $DT_IdentSE = "$S$Name(?:$S(?:$Name|$QuoteSE))*";
my $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>";
my $S1 = "[\\n\\r\\t ]";
my $UntilQMs = "[^?]*\\?+";
my $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*>";
my $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail))|%$Name;|$S";
my $DocTypeCE = "$DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?>?";
my $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?";
my $PI_CE = "$Name(?:$PI_Tail)?";
my $EndTagCE = "$Name(?:$S)?>?";
my $AttValSE = "\"[^<\"]*\"|'[^<']*'";
my $ElemTagCE = "$Name(?:$S$Name(?:$S)?=(?:$S)?(?:$AttValSE))*(?:$S)?/?>?";
my $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|/(?:$EndTagCE)?|(?:$ElemTagCE)?)";
my $XML_SPE = "$TextSE|$MarkupSPE";
my $a = join "", <>;
my @a = ShallowParse($a);
for (0 .. $#a) {
print "$_ : $a[$_]\n";
}
sub ShallowParse {
my($XML_document) = @_;
return $XML_document =~ /$XML_SPE/g;
}
% perl rex.pl /home/nc/travail/pierre/final.xml | head -20
0 : <?xml version='1.0' encoding='ISO-8859-1'?>
1 :
2 : <Document id="1.1.1.">
3 :
4 : <titre_de_tableau>
5 : 1.1.1. VALEUR AJOUT�E DE LA BRANCHE TRANSPORTS (1)
6 : </titre_de_tableau>
7 :
8 : <tableau_source>
9 :
10 : <source_unite>
11 :
12 : <source>
Technical Details
Message-ID:
<m3elxd6v2x.fsf@gaspard.dafag.ritac.net>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!213.56.195.71!fr.usenet-edu.net!usenet-edu.net!oleane.net!oleane!setranews.setra.fr!minequip.setra.i2!not-for-mailReferences:
<3A7EAD2F.90237E86@ktaland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 114f60,2a2c137789640073 X-Google-Attributes: gid114f60,public X-Google-ArrivalTime: 2001-02-05 07:09:37 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!213.56.195.71!fr.usenet-edu.net!usenet-edu.net!oleane.net!oleane!setranews.setra.fr!minequip.setra.i2!not-for-mail From: Nicolas Chuche <nchuche@teaser.fr> Newsgroups: fr.comp.text.xml Subject: Re: shallow parsing : REX Date: 05 Feb 2001 15:52:22 +0100 Organization: home sweet home Lines: 86 Sender: nc@gaspard.dafag.ritac.net Message-ID: <m3elxd6v2x.fsf@gaspard.dafag.ritac.net> References: <3A7EAD2F.90237E86@ktaland.com> NNTP-Posting-Host: 10.74.79.23 Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: freyda.setra.i2 981384721 10470 10.74.79.23 (5 Feb 2001 14:52:01 GMT) X-Complaints-To: usenet@freyda.setra.i2 NNTP-Posting-Date: Mon, 5 Feb 2001 14:52:01 +0000 (UTC) Mail-Copies-To: never User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.0.95