Forum Moderators: open

Message Too Old, No Replies

Do I need XSLT processor on my webserver?

         

Stu_Rogers

3:29 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



I know this is a real beginner question but I can't find the answer.

I would like to be able to use XML on my website (hosted on a remote shared server). I understand that I need to use XSLT to convert the data to HTML.

My first attempts are getting nowhere and I have read that I require an XSLT processor to achieve this?

If so, is it likely that my webhost will already have it installed?

Can anyone supply me with, or point me to, a really simple XML and XSLT file which I could upload to test whether or not my webserver is capable of this?!

Stu_Rogers

3:47 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



I hate to answer my own post, especially before anybody else gets a fair chance to answer me!

I went to this page...

[w3schools.com...]

saved the files, uploaded them to my webserver, then viewed the xml through a web-browser and it worked!

XML files which I downloaded from a book's website did not work (I won't name the book).

I know this is basic stuff but I feel much further on now. Still curious to know what an XSLT processor is? Is this something which operates server-side or client-side (built into browser)?

asquithea

4:52 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



The XSLT engine (or processor) is just a program that takes uses an XSL stylesheet to transform some XML data. It can be built into the user's browser (client side), or run on the web server (server-side).

Client-side transformations are obviously subject to limitations of the user's implementation. Only users with the most modern or capable browsers will be able to do a transformation on the client side, so unless you're targeting a well-defined user-base I don't recommend it.

The advantage of client-side transformations is that there is a potential reduction in the amount of data flowing around the network. Combined with some Javascript, you could (for example) write a very clever page or extension for Firefox that can change the DOM on the fly, or even download new data in the background.

Server side transformations are more limited; you're essentially outputting HTML as you would from an ordinary CGI script, but you have tighter control over your XSLT engine, so you've got much less testing to do.

The advantage of using server-side XSLT over ordinary CGI, is that it can drastically simplify your scripts. Instead of patching a page togther "tag-by-tag", you can just get your data as XML (from a file or database) and use your XSLT engine with a predefined transform to create your output -- all in a few lines of code!

Stu_Rogers

8:31 am on Aug 3, 2005 (gmt 0)

10+ Year Member



The advantage of using server-side XSLT over ordinary CGI, is that it can drastically simplify your scripts. Instead of patching a page togther "tag-by-tag", you can just get your data as XML (from a file or database) and use your XSLT engine with a predefined transform to create your output -- all in a few lines of code!

Yes, this is exactly how I hope to utilise XML.

The test scripts from W3C Schools are working in Windows Firefox1, Netscape8, and IE6 - does this mean my webhost has an XSLT processor running? Or is it more likely that the transformation is happening client-side? Is there any way to tell?

Server side transformations are more limited; you're essentially outputting HTML as you would from an ordinary CGI script, but you have tighter control over your XSLT engine

Again, apologies for the basic questions, but assuming my webhost is running an XSTL engine, how do I have control over it? (I'm on a shared server).

Thanks for your comprehensive answer, asquithea. I'm sure you have helped many people getting the basics of XML.

Stu_Rogers

10:41 am on Aug 10, 2005 (gmt 0)

10+ Year Member



I've taken a week off and when I come back to this the answer was obvious - view the files locally!

Sure enough the browser is handling the transformation. This is contrary to my XML book which states "To transform XML with XSLT you'll need an XSTL processor." Is this statement now only applicable to people with old browsers? Or am I misunderstanding it?

My next puzzle.

The xsl document features html (eg. table tags) which I thought would be output to the browser to create the page. The browser is displaying the page formatted in a table but when I view the source of the page it only shows me the xml. Is that right?

As a webmaster, making the step from HTML/CSS to XML/XSL is turning into a giant leap. It just hasn't clicked yet for me! Any pointers appreciated.

danseagrave

2:35 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Hi,
(my first post here - woo!)

Sure enough the browser is handling the transformation. This is contrary to my XML book which states "To transform XML with XSLT you'll need an XSTL processor." Is this statement now only applicable to people with old browsers? Or am I misunderstanding it?

Technically, your book isn't wrong. Your browser/OS has probably got its own XLST processor.

For Example: in windows with IE, if you go to an *.xml page that links to an xslt stlyesheet IE will download the xslt (just like it does with css files) and then use the MSXML parser/processor/API to do the translation.

If you are hosting on IIS and using classic asp then it is likely that MSXML is availiable to you on the server-side. likewise, asp.net/dotnet framework provide interfaces to the MS xml parser. Other processors are availiable on the server-side for php (bulit-in to recent versions)/apache (Xalan)/python (4xslt)/etc..

The xsl document features html (eg. table tags) which I thought would be output to the browser to create the page. The browser is displaying the page formatted in a table but when I view the source of the page it only shows me the xml. Is that right?

This is right. It is a side effect of navigating directly to the xml file using IE (which I think you are?). IE is showing you the source of the page, the "html source" that you can't see is an intermediary result of the rendering of the page.

Is that any help?

Dan

athinktank

4:30 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Welcome to WebmasterWorld danseagrave!

Stu_Rogers

9:26 am on Aug 19, 2005 (gmt 0)

10+ Year Member



It is a side effect of navigating directly to the xml file using IE (which I think you are?)

Yes I am. Is there another way? Can I use my xml file to dynamically create a page with an xhtml extension?

danseagrave

3:05 pm on Aug 19, 2005 (gmt 0)

10+ Year Member



not with an xml file, but with server-side scripting language, yes

EG, in classic asp:


'set the source location
sXMLFile="myxml.xml"
'set the XSL location
sXSLFile="mytemplate.xslt"


'Load the XML
Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument.3.0")
oXMLDom.async = false
sXMLFile = Server.MapPath(sXMLFile)
'error check the xml
If Not oXMLDom.Load(sXMLFile) Then
Set oErr = oXMLDom.parseError
sErrMsg = "XML Parsing Error. File: " & oErr.url & " Reason : " & oErr.reason & " Line: " & oErr.line & ", Character: " & oErr.linepos & ", Text: " & oErr.srcText
Err.Raise 9999, sErrMsg
End If


'Load the XSL
Set oXSLDom = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
oXSLDom.async = false
sXSLFile = Server.MapPath(sXSLFile)
'error check it
If Not oXSLDom.Load(sXSLFile) Then
Set oErr = oXSLDom.parseError
sErrMsg = "XSL Parsing Error. File: " & oErr.url & " Reason : " & oErr.reason & " Line: " & oErr.line & ", Character: " & oErr.linepos & ", Text: " & oErr.srcText
Err.Raise 9999, sErrMsg
End If


'create & setup the XSLT processor
set oXSLTmp = Server.CreateObject("MSXML2.XSLTemplate.3.0")
Set oXSLTmp.stylesheet = oXSLDom
Set oProcessor = oXSLTmp.createProcessor
oProcessor.input = oXMLDom


'transform the document
oProcessor.Transform


'send the result of processing to the client
Response.write(oProcessor.output)

Stu_Rogers

12:33 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



For the benefit of anyone using PHP, the neccessary code is:

//set filenames
$xml_file = "xml_file_name.xml";
$xslt_file = "xsl_file_name.xsl";

//create processor
$xp = xslt_create() or die("Could not create processor");

//process and echo output
if ($result = xslt_process($xp, $xml_file, $xslt_file)) {
echo $result;
} else {
echo"An error occurred: " . xslt_error($xp) . "(error code " . xslt_errno($xp) . ")";
}

//free the resources used by the handler
xslt_free($xp);

This enables you to drop chunks of formatted XML data into the middle of a PHP page. I'm finally getting somewhere now!

rainmaker32

12:15 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



For the benefit of those using Linux/OpenBSD use xsltproc is an engine to perform translations. On Fedora/Suse/Knoppix/? it installed as part of the base. In Openbsd it comes with the kde desktop.

usage:
xsltproc <xsltfile> <xmlfile>

example:
xsltproc mywebtrans.xsl mywebdata.xml
**result goes to your screen i.e. stdout

xsltproc mywebtrans.xsl mywebdata.xml > mywebpage.html
**result goes to the mywebpage.html file

Therefore an long as you had xsltproc installed on the webserver you could set up a simple cron job to perform translations anytime you get new data.