All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

Making Progress

20 October 2003

I spent the most of Sunday working on my project, learning oodles more about PHP and battling with various ways of treating XML. The solution I settled with was this.

One thing that PHP (like Perl before it) does really well is reading a writing files, whereas one thing it does fairly badly at the present time (future DOM functions excluded) is XML parsing. This much is known. If I was writing this in Perl I would probably use comma delimited value (CSV) files for storing my data and would have read a line at a time, splitting the string on the commas to form an array. The problem with CSV files is they are not of robust structure, and their really tricky to edit by hand. So why not use XML and treat it as a more structured text file? Why not indeed. So that’s what I’m doing. Screw the XML functions, I’ve decided to write my XML using basic string manipulation. (I like string manipulation almost as much as XML).

The second part of my strategy is to use lots of small XML files rather than getting fancy and building big ones. This makes them easy to write, and if I need to serialize the file into an array, it prevents the array getting to complex, thus being easy on the brain and easy on the eye.

The third part of my plan (and this is why it makes the most sense to stick with XML as a file format) is to transform my data to XHTML using XSLT. PHP’s basic XSLT functionality seems pretty sound, although I did have a hard time getting it working. I ended up having to do a dl('xslt.so'); to get the module to load. Any suggestions why, or how I can load it automatically?
I’m paring my small XML files with small XSLT files, which will hopefully keep the processing overhead down. The really neat aspect of the PHP xslt_process() function is that you can simply pass it the addresses of your XML and XSL files and it performs the transformation without you having to specifically create file pointers for either, which is tidy. I guess it’s also more performant – it certainly seems that way.

In a couple more days I might be ready to post some screenshots so you can see what I’m up to. Thanks for all the advice – it’s been extremely useful so far!

On a completely different note – Google Definitions. Define arse, web log, life, and one for Clinton, define sex.

- Drew McLellan

Comments

  1. § zlog: PHP’s XSLT functions *are not* compiled in by default. You must either recombile with ”—enable-xslt—with-xslt-sablot” or see this page is you are running on Windows.

    That’s the only thing I can think of.
  2. § Drew: phpinfo() reported that PHP was compiled with XSLT (using the switches you listed). However, a call to any of the XSLT functions returned an unknown function error.

    Apart from the compilation switches, there’s no further mention of XSLT in my phpinfo() output.
    This is a straight up Debian Woody install.
  3. § Kris: Try to let Google define ’web standards’... :)
  4. § Dysfunksional Monkey: You have to make sure that the XSLT libraries are in the right place on your box, and that you have your ini set up correctly.

    When working on windows (as I tend to do) its easiest to remove PHP and start from scratch than trying to ”add in” libraries. Dunno why, but it seems to break any other way. A clean, manual install including everything you need seems to work fine though.

    What *exactly* are you doing with XML? What I mean is: are you sure the best approach is to store your data in an XML format, or would you be better generating the XML output dynamically?

    I remember this tip I learned a long time ago; ”Create your application around your content. Don’t ever, ever, ever try to fit your content around your application. You’ll double/treble your project time”, and believe me, I’ve prooved this.

    PHP/MySQL is perfect for shopping carts, timesheet systems, etc. Its data-centric.

    PHP/XML is best suited for articles, documents, memo’s, notes, news... that sort of thing. Its document-centric.
  5. § Drew: I’m pretty sure XML is a good approach here. My application is document-centric. With the exception of the settings file which will change on occation, the data files will be written once and never changed. I’d go as far as to say they are unchangable - according to the business logic.

    So yeah, I think file-based XML is the way to go.

    (Also it offers really easy data backup and document portability)
  6. § Dysfunksional Monkey: Glad to see you’ve looked at it before building it. Not many ”programmers” do.

    You wouldn’t believe how many people code in ”PHP/MySQL” or ”ASP/SQL Server” or ”JSP/PostgreSQL” or [insert chosen dicipline here] and thats it - no matter what the data is. They’ll generate a SQL database and a three-tier app for a company who just wants a 10 page website built from thier company brochure.

    What about the config file? If its nice and simple, you can’t get better than an ini file. PHP’s parse_ini_file() function is faster than any file read/parse function i’ve seen yet, and it pops it into an array for you. Perfect when you just want to have one file for config, which is human readable and easy to alter. XML has its place, but for simple ”a = 1”, it just doesn’t this the mark.
  7. § vlad: just uncomment this line in your php.ini file:

    ;extension=php_xslt.dll

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.