Formatting XML with PHP and XSL

I l @ ve RuBoard

We looked at how to format XML using XSL earlier in this chapter. In that case, you embedded the XSL document directly into the XML document. Sometimes, however, that approach might not be best. Imagine if you needed to format the XSL document as either HTML or WAP, depending on the browser being used. You could create two versions of the same XML file and embed them. However, this approach is not ideal. Making changes to the documents or keeping them current can present a lot of problems. So how do you use XML and two different XSL documents? Thankfully, both Expat and MSXML let you embed an XSL document into an XML document. Here you can use PHP to detect the browser type, select the matching XSL document, and use either Expat or MSXML to embed the XSL document.

Using the PHP XSL Extension for XSL Formatting

PHP uses Expat and another open -source library called Sablotron. Sablotron is an XSL parser library and is used in combination with Expat to make up PHP's XSL extension. Support is planned for other libraries, such as the Xalan and libxslt libraries.

To enable XSLT support in PHP, you must uncomment the following line in your PHP.ini file:

 extension=php_xslt.dll 

You can use the PHP XSLT extension in two ways. You can load the XML and XSL documents directly into the extension, or you can load the documents into memory first. You can use the second method if the XML and XSL documents come from a non- file-based source, such as a database.

The Direct Method

The following code loads the XML and XSL files directly into the XSLT extension:

 <?php  //path  $file_path = "C:\Book\Code\Chapter8_XML\XML\";  //XML file  $xml_file = "file://" . $file_path . "people.xml";  //XSL file  $xsl_file = "file://" . $file_path . "people.xslt";  //allocate a new XSLT instance  $xh = xslt_create();  //process the document  $result = xslt_process($xh, $xml_file, $xsl_file);  //display the document  print $result;  //unload XSLT  xslt_free($xh);  ?> 

First you set up the paths to the XML and XSL files:

 $file_path = "C:\Book\Code\Chapter8_XML\XML\";  $xml_file = "file://" . $file_path . "people.xml";  $xsl_file = "file://" . $file_path . "people.xslt"; 

Note that I have used a local file path; Sablotron has no support for loading files over HTTP. Also note that each file begins with "file://" . This is required so that Sablotron knows that you are referencing a file. You will receive an error if you omit it.

The next stage is to create an instance of the XSLT extension:

 $xh = xslt_create(); 

Next you call the xslt_process function of the XSLT extension, passing the instance of the XSLT extension as well as the locations of your XML and XSL files:

 $result = xslt_process($xh, $xml_file, $xsl_file); 

You store the result of the xslt_process function in a variable ( $result ), which you can now display:

 print $result; 

To complete your code, you erase your instance of the XSLT extension from memory:

 xslt_free($xh); 
The Load into Memory Method

The second method you can use is to load the XML and XSL files into memory and then style the XML file with the XSL file. Using PHP, you do this as follows :

 <?php  //path  $file_path = "http://localhost/phpbook/Chapter8_XML/XML/";  //XML file  $xml_file = $file_path . "people.xml";  //XSL file  $xsl_file = $file_path . "people.xslt";  //open XML file  $xmlfile = fopen ($xml_file, "r");  while (!feof ($xmlfile)) {     $xml_file_contents .= fgets($xmlfile, 4096);  }  fclose ($xmlfile);  //open XSLT contents  $xslfile = fopen ($xsl_file, "r");  while (!feof ($xslfile)) {     $xsl_file_contents .= fgets($xslfile, 4096);  }  fclose ($xslfile);  $arguments = array(     '/_xml' => $xml_file_contents,      '/_xsl' => $xsl_file_contents  );  //allocate a new XSLT processor  $xh = xslt_create();  //process the document  $result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);  print $result;  xslt_free($xh);  ?> 

First, you set up the paths to the XML and XSL files:

 $file_path = "http://localhost/phpbook/Chapter8_XML/XML/";  $xml_file = $file_path . "people.xml";  $xsl_file = $file_path . "people.xslt"; 

The next stage is to read both the XML and XSL files into memory using PHP's file functions. Here you use the fopen function to load the XML and XSL files and loop through each file, reading it into a variable using the fgets function. As mentioned earlier, Sablotron does not support reading files over HTTP. However, when you use file functions, this has no bearing if the file is coming from a local file system or via HTTP.

 $xmlfile = fopen ($xml_file, "r");  while (!feof ($xmlfile)) {     $xml_file_contents .= fgets($xmlfile, 4096);  }  fclose ($xmlfile);  $xslfile = fopen ($xsl_file, "r");  while (!feof ($xslfile)) {     $xsl_file_contents .= fgets($xslfile, 4096);  }  fclose ($xslfile); 

Next you set up a parameters array. This links both the XML and XSL files to a set of parameters that you will reference in Sablotron:

 $arguments = array(     '/_xml' => $xml_file_contents,      '/_xsl' => $xsl_file_contents  ); 

Here you link '/_xml' to the XML file and '/_xsl' to the XSL file. Now that you have everything ready, you can load the Sablotron functions. First, you create a new instance of Sablotron:

 $xh = xslt_create(); 

Next you call the xslt_process function. Here you pass a handle to the Sablotron parser, as well as the references to the parameters array:

 $result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments); 

Here, the xslt_process function matches 'arg:/_xml' and 'arg:/_xsl' against values in the parameters array ( $arguments ). If it finds a match, it replaces each with the value from the array.

You store the result from the xslt_process function to a variable ( $result ). To complete the example, you display that result and then unload Sablotron from memory:

 print $result;  xslt_free($xh); 

Using PHP and MSXML for XSL Formatting

The MSXML parser includes XSL formatting functions. You don't need to call on any other parsers for XSL formatting. Using the XSL functions is very simple:

 <?php   //Load the XML   $xml = new COM("Microsoft.XMLDOM");   $xml->async = false;   $xml->load("http://localhost/phpbook/Chapter8_XML/XML/people.xml");   //Load the XSL   $xsl = new COM("Microsoft.XMLDOM");   $xsl->async = false;   $xsl->load("http://localhost/phpbook/Chapter8_XML/XML/people.xslt");   $output = $xml->transformNode($xsl);   print $output;  ?> 

You first create a new instance of the MSXML COM object and load the XML file using that object:

 $xml = new COM("Microsoft.XMLDOM");   $xml->async = false;   $xml->load("http://localhost/phpbook/Chapter8_XML/XML/people.xml"); 

You then create another instance of the MSXML COM object and load the XSL file using that object:

 $xsl = new COM("Microsoft.XMLDOM");  $xsl->async = false;  $xsl->load("http://localhost/phpbook/Chapter8_XML/XML/people.xslt"); 

Next you call the transformNode method of the XML file object. This method expects a COM instance of the XSL file, so you pass the second MSXML COM object you created:

 $output = $xml->transformNode($xsl); 

Finally, you display the output of the transformNode object:

 print $output; 
I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net