février 17, 2003
Proposed XML structure for comments

Here's the proposed XML structure for comments to NW entries:

<nwcomments nwentryurl="http: ....">
   <comment>
        <id>c123</id>
        <previd>c122</previd>
        <author>
                <name>Steph Troeth</name>
                <email>steph@unadorned.org</email>
                <url>http://unadorned.org/</url>
        </author>
        <content>
        I was on the bridge and I took this photograph of the river ....
        </content>
   </comment>
   <comment>
        <id>c124</id>
        <previd>c123</previd>
        <author>
                <name>Daisy Cow</name>
                <email>cow@unadorned.org</email>
                <url>http://unadorned.org/cow/</url>
        </author>
        <content>
         This area has very nice grass to eat. Right near a train line where I can watch the trains go too. Nice place to visit.
        </content>
   </comment>
</nwcomments>

The previd element is optional. This is used for threading purposes (based on issue raised by Delphine). If the previd doesn't exist, we assume we are looking at a new thread.

Posted by steph at 06:18 PM
février 12, 2003
Parsing NW XML Files

Hi everyone,

I have been experimenting with parsing a typical index.xml file to transform it into a form where some elements are editable. I'm doing this with an event based parser (where you define how you handle the start, end elements, and characters in between, the same as in previous examples), and also with class_path_parser.

The class_path_parser method is more concise and perhaps better suited for our purposes:

<? print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Parsing NW XML files</title>
</head>
<body>
<?
include_once("../phplib/class_path_parser/class_path_parser.php");

// a map so that xml elements are transformed into human readable names
// on print
$map_array = array(
    "nwregion" => "Région",
    "nwdpt"  => "Département",
    "nwcanton"  => "Canton",
    "nwtitle" => "Titre",
    "nwlogo" => "Logo",
    "nwmetadesc" => "Metadesc",
    "nwkeywords" => "Keywords",
    "nwurl" => "URL",
    "nwcreadate" => "Creation date",
    "nwmoddate" => "Last modified date",
    "nwauteur" => "Auteur",
    "nwemail" => "Email"
);

// handle none editable elements
function nonEditHandler($name,$attribs,$content) {
  global $map_array;
  $pattern = "/<$name>(.*)<\/$name>/";
  $replacement = "$1";
  $content = preg_replace($pattern,$replacement,$content);

  print "<strong>$map_array[$name]:</strong> $content<br>\n";
}

// handle elements within nwtexte
function nwtexteHandler ($name,$attribs,$content) {
  global $map_array;
  $pattern = "/<$name>(.*)<\/$name>/";
  $replacement = "$1";
  $content = preg_replace($pattern,$replacement,$content);

  print "<strong>$map_array[$name]:</strong> <input size=\"70\" type=\"text\" value=\"$content\"><br />\n";
}

// handle elements where we want the information raw as it is
function rawHandler ($name,$attribs,$content)
{

  $pattern = "/<$name>/";
  $replacement = "";
  $content = preg_replace($pattern,$replacement,$content);
  $pattern = "/<\/$name>/";
  $replacement = "";
  $content = preg_replace($pattern,$replacement,$content);

  print "<textarea cols=\"70\" rows=\"10\">$content</textarea>\n";
}

$parser = new Path_parser();

//handle all our elements
$parser->set_handler("/nwcity/nwheader/nwregion","nonEditHandler");
$parser->set_handler("/nwcity/nwheader/nwdpt","nonEditHandler");
$parser->set_handler("/nwcity/nwheader/nwcanton","nonEditHandler");
$parser->set_handler("/nwcity/nwtexte/nwtitle","nwtexteHandler");
$parser->set_handler("/nwcity/nwtexte/nwlogo","nwtexteHandler");
$parser->set_handler("/nwcity/nwtexte/nwmetadesc","nwtexteHandler");
$parser->set_handler("/nwcity/nwtexte/nwkeywords","nwtexteHandler");
$parser->set_handler("/nwcity/nwtexte/nwurl","nonEditHandler");
$parser->set_handler("/nwcity/nwtexte/nwcreadate","nonEditHandler");
$parser->set_handler("/nwcity/nwtexte/nwmoddate","nonEditHandler");
$parser->set_handler("/nwcity/nwtexte/nwauteur","nwtexteHandler");
$parser->set_handler("/nwcity/nwtexte/nwemail","nwtexteHandler");
$parser->set_handler("/nwcity/nwtexte/texte","rawHandler");

print "<form method=\"post\" action=\"submitXML.php\">\n";

// currently we pass this a static file name, later on we'll grab it from
// a url
if(!$parser->parse_file("index.xml")) {
  print("Error:".$parser->get_error()."<br/>\n");
}

print "<p><input type=\"submit\" name=\"submitXML\" value=\"Submit changes\"></p>\n";

print "</form>\n";

?>
</body>
</html>

This is not currently on the live server yet. The obvious problem is we have to know the structure of the XML file. If the XML file format changes, this program will have to be changed too.

I'm working on using Sabletron XSL/PHP for templating, in order to save modified files back into the XML format .

Posted by steph at 01:09 AM