Migrating to XForms
by Paul Sobocinski
|
Pages: 1, 2, 3
Phase 3: Finalizing the Output
As you've hopefully noticed, we haven't actually called the function that runs the parser. This is what we do next:
/*A*/
if (!xml_parse($parser, $input["body"], true))
{
$error = xml_error_string(xml_get_error_code($parser));
$line = xml_get_current_line_number($parser);
die("HTML error: " . $error . " , line " . $line);
}
xml_parser_free($parser);
/*B*/
$outhead = "<$f"."model>".$outhead."</$f"."model>";
$finaloutput = $input["top"].$input["head"].$outhead.$input["middle"].$outbody.$input["bottom"];
if (NSPACES_ON)
$finaloutput = str_replace("<html","<html xmlns:f='http://www.w3.org/2002/xforms'",$finaloutput);
/*C*/
$outfile = "output.html";
$fh = fopen($outfile, "w");
if (!fwrite($fh, $finaloutput))
die("Failed to write to file.");
A: xml_parse() is what puts the gears in motion -- we pass the variable representing the parser and the input that we wish to parse as the first two parameters, respectively. The third parameter is set to false if we want to pass the input in smaller chunks (this is done when the input is very large and a lot of processing is required). In our case, we will be parsing the input in one go, so we set the third parameter to true. If xml_parse() returns false, it encounters an error and is unable to finish parsing the input. When this happens, we use the xml_get_error_code() function to find out what happened, and xml_get_current_line_number() to find out where it happened. The final parser-related function, xml_parser_free(), removes the parser resource from memory. This is only done once we're finished with the parser entirely.
B: As previously mentioned, $outhead contains all the XForms elements that need to be added to the <head> tag of the output XHTML. However, before we do this, we encase all of this in a <model> tag to indicate that they are XForms tags. Now, we stick the file segments back together (as shown in Figure 1) and store the end result in $finaloutput. Before storing our result in a file, we add the namespace declaration to the <html> tag, using str_replace(). This function was explained when we used it in Phase 1.
C: Now that we have our translated form, we need to put it into a file. fopen() defines a file handler, which tells PHP that we will be doing something with a file. In this case, we will be writing to it, so we pass a parameter of w (the first parameter is the name of the output file). The function that does the actual file writing is fwrite() -- we pass the file handler we declared earlier, along with the data we wish to write. We produce an error message if the write fails.
At this point, we have consolidated the translated XHTML and written it to an output file. This marks the end of Phase 3, and the completion of the parser.
Scaling the Parser
What has been provided here are the rudimentary building blocks for a complete HTML to XForms translator. As explained earlier, the parser can be easily scaled to handle all possible HTML form elements and translate them into XForms. In addition to the introductory XForms article mentioned earlier, you may find this link useful: XForms for HTML Authors. It explains in detail how to use XForms to provide all features available with HTML forms.
All the files that were discussed (including the main translator) are available below:
- From XHTML to XForms
2006-11-20 06:14:24 Alain_COUTHURES - XHTML
2006-11-02 00:35:39 iconara - XHTML
2006-11-02 06:10:58 sobes