Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


ASP: Transforming the XML - Page 22

November 18, 2002

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">.


Transforming the XML - Page 21
Practical XML for the Web
JSP: Transforming the XML - Page 23


Up to => Home / Authoring / Languages / XML / PracticalXML




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers