![]() |
Generating XML/XSL from a Mysql DB with PHP's DOM functions (part Two) XSLT
1 Attachment(s)
Hello again,
This is the second part to my PHP XML/XSLT tutorial. In this tutorial i will be going through a very simple XSL using the XML data produced from part one. I just going to briefly go over the point to all this, because, many people (including myself) learn things a lot quicker if the technology has a point, or to put it in better words the technology isn't described as a long list of mumbo jumbo and there is a clear cut point to answer the question 'why would i need that?'. Ok, we already know that XML is a standard for creating markup languages which describe the structure of data, and it comes in very handy, for example Ajax uses xml to transfer data between server and client, through xmlhttprequest. Most of us already know this, but what a few may not know (including myself until a few months ago) is that you can apply a style sheet to XML and produce HTML from it, the style sheets are called XSL or eXtensible Stylesheet Language. There are 3 main ways of doing this: the first way is to add a XSL style sheet to an XML file then let the browser do the 'transformation' in other words render the xml data according to the XSL style, much like css. Although this method works, it wont however work on non XSLT aware browsers. The second way of doing it is by using javascript to do the transformation, but, once again, browser incompatibilities limit the success of this method. So then how do we do it?, well the third way is to use PHP(or any other server side script like php or asp will do the trick) to do the transformation, then generate the HTML so that any browser can see it properly, this is the method that i will be showing you in this tutorial. Ok, first off we need an XSL file, i've got one pre made, ill paste it in then go over it. xml Code:
Because XSL is still XML we must start it as we would an XML file: xml Code:
xml Code:
xml Code:
xml Code:
Next we define a parameter, this will contain our final output title: xml Code:
Next we start our template: xml Code:
Again if that still makes no sense, it will become clear eventually :) Ok now i can start some html to start buiding up our template: xml Code:
xml Code:
Parameters allow us to have values that we can access from anywhere within the XSL file, the way we access them is by using the value-of method, normally this is used to get values of XML element by using an xPath statement like 'car/name' but because we prefixed the name with '$' it knows that it is a parameter and not an XML element we want. So basically that produces the HTML code: html Code:
Here we start a 'style' tag and give it some CSS: xml Code:
There are other characters that will be wrongly interpreted, they are: < less than > greater than & ampersand ' apostrophe " quotation mark In the case of javascript and indeed CSS we cannot replace the characters for there entity references because it will break the script, so we must wrap the code in the CDATA. Phew that was a long winded explanation of something relatively simple . Anyway onwards we must plod, we have much to cover. I'm going to skip most of the HTML because it should be self explanatory. The next xsl tag we come across is: xml Code:
xml Code:
Then we end the 'for-each', then the template (match="/") and then end the stylesheet. Ok, so thats our XSL roughly described, ON WITH THE PHP!!! The first thing we do is to create a new XsltProcessor object, this will allow us to process the xsl and perform our 'transformation': PHP Code:
PHP Code:
Next we need to load our XML file: PHP Code:
PHP Code:
Hopefully that has shown the usefulness and power of using these technology's. The main point of this script is to separate presentation layer from the logic layer so that any one of these layers can be modified without affecting the other, and I personally prefer this method from the templating systems out there for php, with this you have more flexibility with the XSL file, I haven't even gone over half of the capabilities of XML You could also save the HTML file instead of echoing it out like so: . PHP Code:
PHP Code:
Like for example we want alternating row background colors, the way that i can think of, is to have 2 classes 'r0' and 'r1', and then call this in the xsl: xml Code:
There is an example of this in the attachment. Anyway i hope i have explained everything and got my facts right, and i hope you guys find it useful, XML/XSLT and xPath are all powerful tools and i think we will see more and more of this kind of stuff in the future. |
I just wanna add another little xsl tag that is useful, <xsl:sort />
xml Code:
xml Code:
|
Wow, I remember touching on this stuff about a year or so ago, but then I heard it couldn't be used with IE and just forgot all about it. I'm going to have to do some learning because that stuff looks amazing. Why aren't more people using this technique?
|
Quote:
Transforming documents like this does result in a fair hit on the server (at least, it did a while back when I was experimenting). Couple that with yet another layer of abstraction and it's not ideal of every situation. But it's up to you to decide when it would be good to use these techniques and when it wouldn't. :-) |
Quote:
Quote:
|
hey guys,
sorry i havnt posted in a while, work is taking up all my free time atm :( Yea i thought the same Karl, when i first looked at it i was using the browser to do the transformation, it was a cool idea but I too just brushed it aside as being IE unfriendly, but then i found the PHP XSLT functions, i am certainly gonna use it more in the future, i think its real use is probably gonna be more like a page cache or something because (i havnt tested speeds) but im guessing it takes up more server power than normal php/html to create 'on the fly' so to speak. I do kinda agree with Salathe about the extra layer of abstraction, but that said, it does make it very easy to maintain in my opinion, you have another layer of functionality and lets be honest it does separate php from html, something that some people consider to be the holy grail. Quote:
Anyway im glad you guys found it useful |
Found this to be quite informative...
One question though what if I, oh lets say, have lots and lots of tags to be delt with… Like Code:
<RetRequest>But on the order of 50 or so parent tags 100's of child tags, pulling information form 20 tables in 2 db's... (Its the current problem that I am having...getting all the stored variables, and generating the xml string. Can do it just fine storing in session but can not get it to work out of the bd...) |
Quote:
Almost every modern browsers support native xml+xsl transformation. I've wrote, use, and released a set of 2 classes, that allow an easy output of xml, to select what I call a "rendering set" (imagine a version for ipod/pda and another "rich media") and to determine if the client should receive a server-transformed html file, or if xml+xsl can be sent directly. You can see it in action on Quixml::Quick Xml and Xsl for PHP. Try to look at the pages source from firefox or safari.... And an example is in the class documentation for the ClXml class => Docs For Class clXml (you loose the navigation by going directly to this page.) |
| All times are GMT. The time now is 01:17 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0