Introduction to Server-Side XML

It sets the scene for server-side XML, and shows what you can do with it, by way of a parallel example done in ASP, PHP, and JSP (we have only included the first of the example sections here). The three chapters that follow this one in the book are case studies, which go into using XML with the three server-side languages mentioned above in much more detail.

This sample is taken from Chapter 8 "Introduction to Server-Side XML" of the glasshaus title "Practical XML for the Web".

ASP: Transforming the XML

The following page (cd_list.asp) does the trick:

<%@ Language=VBScript %>

<%

  Dim oXML, oXSL

  Set oXML = CreateObject("MSXML2.DOMDocument")

  oXML.load(Server.MapPath("cd_list.xml"))

  Set oXSL = CreateObject("MSXML2.DOMDocument")

  oXSL.load(Server.MapPath("cd_list.xsl"))

  Response.write oXML.transformNode(oXSL)

%>

First we create an XML document object:

Set oXML = CreateObject("MSXML2.DOMDocument")

Then we load the cd_list.xml into the object:

oXML.load(Server.MapPath("cd_list.xml"))

We do the same for the cd_list.xsl document, and finally we perform the transformation and write out the result.

PHP: Transforming the XML

Performing an XSL transformation in PHP is relatively straightforward. Once you have the XSLT extension installed the following script is all it takes (cd_list.php):

<?php

  $xsltHnd = xslt_create();

  xslt_set_base($xsltHnd,'file://c:/xml/chapter10/');

  $html =  xslt_process($xsltHnd, 'cd_list.xml', 'cd_list.xsl');

  echo $html;

?>

The first line:

$xsltHnd = xslt_create();

creates a new XSLT processor. The function returns the XSLT processor resource, which we will use for all our other XSLT functions.

If you are working on a Windows machine and intend to pass external files to the XSLT functions, you will need to set the base URI so that XPath can resolve the file names. In the setup I used, the URI is as follows:


xslt_set_base($xsltHnd,'file://c:/xml/chapter10/');

With all the XML and XSL documents residing in C:\xml\chapter10. xslt_set_base takes 2 arguments  the XSLT processor resource and the URI. Once we have set that, we can then use the xslt_process function to perform the transformation.

$html =  xslt_process($xsltHnd, 'cd_list.xml', 'cd_list.xsl');

In our example we have passed the function the three arguments  the XSLT processor resource, the name of the XML file, and the name of the XSL stylesheet. The result of the transformation is returned and stored in the variable $html.

The xslt_process function has an additional 3 optional arguments, and it is through these that we have great versatility in the manner in which we can use the function. Let's take a look at some of the other ways in which we could have written this script:

<?php

  $xsltHnd = xslt_create();

  xslt_set_base($xsltHnd,'file://c:/xml/chapter10/');

  xslt_process($xsltHnd, 'cd_list.xml', 'cd_list.xsl','cd_list.html');

?>

In this example we are not returning the result of the transformation into a variable; instead we are specifying a file name as the fourth argument  the results will be saved in cd_list.html. Another way we can use the function is by having our XML and XSL stored in variables:

<?php

  $xsltHnd = xslt_create();

  $xml = join('',file('cd_list.xml'));

  $xsl = join('',file('cd_list.xsl'));

  $arguments = array(

     '/_xml' => $xml,

     '/_xsl' => $xsl

  );

  $html = xslt_process($xsltHnd, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);

  echo $html;

?>

In this example, the first thing you'll notice is that we have left out the line that sets the base URI. Since our data is going to be stored in variables we will not need to set this. The two lines:

$xml = join('',file('cd_list.xml'));

$xsl = join('',file('cd_list.xsl'));

are simply setting the variables $xml and $xsl to the contents of the XML and XSL files. The file() function reads a file off disk into an array, one line per element. The join() function joins array elements with a specified string.

We then create an associative array, called $arguments, containing $xml and $xsl. Since we don't want to output to a file, we make the fourth argument NULL. While we are talking about the arguments to the xslt_process function, it is worth noting that we can pass it an optional fifth argument, which is an array of parameters. These parameters can then be accessed in your XSL document with <xsl:param name="parameter_name">.


George Petrov

George PetrovGeorge Petrov is a renowned software writer and developer whose extensive skills brought numerous extensions, articles and knowledge to the DMXzone- the online community for professional Adobe Dreamweaver users. The most popular for its over high-quality Dreamweaver extensions and templates.

George is also the founder of Wappler.io - the most Advanced Web & App Builder

See All Postings From George Petrov >>

Comments

Be the first to write a comment

You must me logged in to write a comment.