xml and xsl

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Post by sike »

what's the main reason for you to choose xslt over a pure php solution?
i don't really get the point i guess.
markup order: it's always been a pain to implement certain layouts (like 3 column + footer) using CSS when the markup is out of order. XSLT could just transform the order of the markup to something more usable.
care to explain how this would work?


cheers
Chris
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

It's all about separation - so in MVC you don't need access to the model or the controller to implement different views.

Think of it like a (brick & mortar) library - you go in and ask for books about lizards at the desk. The person at the desk is the controller. They decide how and where to look for lizard books, then order them from the shelves (data models) which return the books (xml). Now you have a bunch of books (xml) that you want to write a report about.

Would it have been more efficient for the librarian to write your report? Yes. But she's a librarian, and it's your damn report.

XSLT is the power to turn lizard books into lists of lizard species, descriptions of lizards, etc... does that make any sense?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

does that make any sense?
:/ not really
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

XSLT IS LIZARD BOOKS DAMNIT!

ok - bad analogy. At least it's not the worst analogy I've ever heard!

What I mean is this: you could do everything in one script, but it's considered better practice to separate your web app into an MVC structure. XSLT falls under the "view" area.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Kieran Huggins wrote:At least it's not the worst analogy I've ever heard!
Ha ha! I lol'd and then watched a load of his videos.
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Post by sike »

i was not talking about a procedural one file php monster.
i am more interested in why you like xslt for your view (given you have a neatly layered application which adheres to the beloved oop principles).

chris
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

XML data feeds (RSS or other) in raw format (i.e. <element attrib="value">Content</element>) are un-readable (to a 'user')

XSLT transforms XML data and applies markup. It can do this client-side, so PHP or other scripting language on the server is not needed. It's just the same as having static HTML markup in a static file somewhere.

We have XML feeds that are dumped from the mainframe onto a shared drive, we also keep XSLT files on those shared drives.

No apache/ISS needed, no PHP installation needed. It's a hell of a lot "cheaper" than running a webserver.
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Post by sike »

jup jenk, but you need a decent xslt processor afaik.

i did try xslt for my view layer 2 years ago and got the impression that you produce a lot of code (and to be honest not really that readable) but get no real benefit back when compared to php templates.
that's why i was asking.

chris
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

php templating systems like smarty are other options. I prefer using xslt because: I believe the syntax was thought out better, it uses xpath, I can do it on the server or in the browser, it's a w3c standard (good cross platform support). Also, the separation means it's not tied to php, so it makes my layers more portable / flexible between systems.

Also, it's fun to call it "EX-SLUT" or "EXCESS LIT"... the possibilities are truly endless.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It is somewhat verbose, but no more verbose than you want to be: it all depends upon the amount of abstraction you put into the file. You could, theoretically speaking, using XSLT as a regular template machine, and manually use XPath to retrieve all the interesting parts of the XML document, but that's boring. XSLT is all about recursion, delegation, and reusability of parts.

Suppose, for example, I have an area on my web-page that represents a forum comment. In a normal templating system, you'd probably do a foreach loop inside a table and generate each table row as a comment. For XSLT, you'd encounter the XML node <posts>, which would create the table, but then you would defer to <xsl:apply-templates />, which would process all the nodes inside <posts>. You would then have another rule that caught <post> tags, and format them accordingly as table rows.

The next time, in another page, you need to show a bunch of posts, it's as simple as importing the appropriate XSL stylesheet and the using <xsl:apply-templates />, rather than having to muck around with the templating language's ability to "include" files with parameters (which, while functional, turn into horrible hacky messes after a while. Trust me: I've used Smarty before).
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

sike wrote:jup jenk, but you need a decent xslt processor afaik
Like Internet Explorer/FireFox/Opera, perhaps? :roll:
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I'm sure there are subtle incompatibilities that will cause you to tear your hair out, and that's why we're all so wary about using XSLT client-side. This might be a false worry, since XSLT was designed from the outset to be strict, something that cannot be said of HTML or CSS.

Hey Kieran...
Use DOM objects! (or at least extend them)
Could you elaborate? Right now, my code for making objects looks something like:

Code: Select all

$types_document = new DOMDocument('1.0', 'UTF-8');
$types_root = $types_document->createElement('types');
$types_document->appendChild($types_root);
$types_document->formatOutput = true;
foreach ($schema->types as $name => $expanded_name) {
    $types_type = $types_document->createElement('type', $expanded_name);
    $types_type->setAttribute('id', $name);
    $types_root->appendChild($types_type);
}
$types_document->save('types.xml');
If it's short, it's reasonable, but it gets ugly fast.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

i think for applications that have complex layout like in CMS that has many boxes of different pieces of information @ different places in a page, you have to use a templating solution no matter you use xsl or not. we know that xsl file is associated with one xml file. we cannot put all different data elements/modules of an entire page into a xml page and associate xsl file with it because that is messy and makes things complex.

if you are developing a simple application like a guest book that displays the guest entries, then it is easy to use xsl for formatting in view of MVC. i also strongly suggest that xsl formatting should be done server side to avoid any major surprises with browsers.

even if you have an application like blog, you can have one single piece of module in one page, like displaying all users registered with the website in one page, displaying blogs made on january, displaying posts of a blog and we also develop separate xsl sheet for each of these pages. but if you plan to have more than one module in a page, then from maintanence-aspect, we need a templating solution.

EDIT: I do not really know whether XSL-FO can act as templating solution. even if does that, we have to examine how maintaneable and how easy to code it is?

thanks for all the replies so far guys.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<xsl:include href="http://yoursite.com/page_to_be_included.xml" />
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

@ambush: you're doing essentially hat I had in mind. It's verbose, to be sure.

@Jenk: EXACTLY!
Post Reply