REX, ported to PHP


The aproach Cameron follows here is to build one big regular expression that will match each part of a XML document, and thereby split the document into a list of XML elements and enclosed text.

I've made a rather direct 'port' of Cameron's JavaScript regexes, as the Perl version uses some modifiers that are not supported in PHP's pcre (perl-compatible regular expressions)

To understand the following example, please read the Paper "REX: XML Shallow Parsing with Regular Expressions" by Robert D. Cameron, that explains the various parts of the regular expression being put together.
esp. the Perl code in Appendix A: Shallow Parsing in Perl

A first working approach in PHP can be found here
the PHP version printed below (the sample XML file here is a snapshot RSS file downloaded from Mark Pilgrim's site on 24th September. Note: this file is an example of valid, wellformed XML, so it would give any other XML parser no trouble, it's just here for an eyample of XML data)
What can be seen clearly when executing the code is the 'shallow' characteristic of this aproach. We simply get a flat list of all 'tokens' in the order of their appearance. If we were to 'join' this list again, we would have the exact data we started with, with all whitespace, indentation and linebreak intact. (This is actually an important characteristic of this aproach)

The regular expression below finds 523 tokens in that RSS file. Here are the first 40, one per line, with whitespace made visible:

  0  <?xml version="1.0" encoding="utf-8"?>
  1  \r\n
  2  <rss version="2.0" \r\n    xmlns:dc="http://purl.org/dc/elements/1.…
  3  \r\n\r\n  
  4  <channel>
  5  \r\n    
  6  <title>
  7  dive into mark
  8  </title>
  9  \r\n    
 10  <link>
 11  http://diveintomark.org/
 12  </link>
 13  \r\n    
 14  <description>
 15  A lot of effort went into making this effortless.
 16  </description>
 17  \r\n    
 18  <dc:language>
 19  en-us
 20  </dc:language>
 21  \r\n    
 22  <dc:creator>
 23  f8dy@diveintomark.org
 24  </dc:creator>
 25  \r\n    
 26  <dc:rights>
 27  Copyright 2002
 28  </dc:rights>
 29  \r\n    
 30  <dc:date>
 31  2002-09-26T01:28:52-05:00
 32  </dc:date>
 33  \r\n    
 34  <admin:generatorAgent rdf:resource="http://www.movabletype.org/?v=2…
 35  \r\n    
 36  <admin:errorReportsTo rdf:resource="mailto:f8dy@diveintomark.org"/&…
 37  \r\n    
 38  <sy:updatePeriod>
 39  hourly
...
483 more tokens follow; the file has 523 in all.

Obviously this list is not very much as of yet, and doesn't really buy us anything. What to do with this list will be discussed in the next part.

test2.php code follows:

<code>
<?
/*
* The following code was adopted from
* (the Javascript and PHP impl of regex being more compatible)
' ################
* REX/Javascript 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.
' ################
* 'port' to PHP by Martin Spernau 2002
*/
function shallowParse($xmlstring) {
        $TextSE = "[^<]+";
        $UntilHyphen = "[^-]*-";
        $Until2Hyphens = "$UntilHyphen([^-]$UntilHyphen)*-";
        $CommentCE = "$Until2Hyphens>?";
        $UntilRSBs = "[^]]*]([^]]+])*]+";
        $CDATA_CE = "$UntilRSBs([^]>]$UntilRSBs)*>";
        $S = "[ \\n\\t\\r]+";
        $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
        $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
        $Name = "($NameStrt)($NameChar)*";
        $QuoteSE = "\"[^\"]*\"|'[^']*'";
        $DT_IdentSE = "$S$Name($S($Name|$QuoteSE))*";
        $MarkupDeclCE = "([^]\"'><]+|$QuoteSE)*>";
        $S1 = "[\\n\\r\\t ]";
        $UntilQMs = "[^?]*\\?+";
        $PI_Tail = "\\?>|$S1$UntilQMs([^>?]$UntilQMs)*>";
        $DT_ItemSE = "<(!(--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name($PI_Tail))|%$Name;|$S";
        $DocTypeCE = "$DT_IdentSE($S)?(\\[($DT_ItemSE)*]($S)?)?>?";
        $DeclCE = "--($CommentCE)?|\\[CDATA\\[($CDATA_CE)?|DOCTYPE($DocTypeCE)?";
        $PI_CE = "$Name($PI_Tail)?";
        $EndTagCE = "$Name($S)?>?";
        $AttValSE = "\"[^<\"]*\"|'[^<']*'";
        $ElemTagCE = "$Name($S$Name($S)?=($S)?($AttValSE))*($S)?\/?>?";
        $MarkupSPE = "<(!($DeclCE)?|\\?($PI_CE)?|\/($EndTagCE)?|($ElemTagCE)?)";

        $XML_SPE = "$TextSE|$MarkupSPE";
        
        // display regex, only for the brave ;)
        // echo htmlspecialchars($XML_SPE)."\n";
        
        preg_match_all("/$XML_SPE/", $xmlstring, $matches);
        return $matches;
}        

$testxml = join("", file("./diveintomark.rss.xml"));
$result = shallowParse($testxml);
while (list($count,$token)=each($result[0])) {
        echo "$count -> <b>".htmlspecialchars($token)."</b><br>\n";
}
?>
</code>


alles Bild, Text und Tonmaterial ist © Martin Spernau, Verwendung und Reproduktion erfordert die Zustimmung des Authors

Martin Spernau
© 1994-2026


amazon.de Wunschliste

Facebook me!
Google+

powered by Traumtank