Categories


Archives


Recent Posts


Categories


Dead Simple Command Line XSLT Processor with PHP 5

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Update: A few people have pointed out that OS X now has, by default, a command line XSLT processor. PHP is my hammer, the world is my nail.

Seven or so years ago, learning XSLT outside of an organization that used it regularly was a daunting proposition. Beyond the huge difference between “linear” programming and template based programming, the language itself was still shaking out. You also had to find a way to get an XSLT processor running on your machine, which at the time meant a bunch of different interdependent packages you’d have to compile by hand.

Template programming is still a big giant pain in the ass and I don’t know if XSLT ever shook itself out, but it’s now easy to get an XSLT processor up and running, thanks to PHP 5

One of the big changes in PHP 4 vs. PHP 5 was the inclusion, by default, of an XSLT processor, no external libraries needed. Since PHP can be run as both an Apache module and a stand along interpreter, this means you can easily whip up a script that creates a “command line” XSLT processor, perfect for running through all those examples and learning what the language can do.

The following instructions are OS X specific, but if you’re not using OS X you must enjoy tinkering with your computer, and can figure out what to modify.

Step 1

Get PHP 5.

Step 2

Download the script to your computer, saving it as a text file. I recommend creating a “bin” folder in your home directory. If you’ve installed PHP 5 to a different location you’ll need to change the first line of the file

#!/usr/local/php5/bin/php 

Step 3

Add the location of your newly downloaded script to your system PATH. Your system PATH is a list of directories that are searched whenever you type in a command. Assuming you followed my advice in Step 2 and are using OSX, add the following line to the ‘.bash_profile’ file in your home directory

export PATH=$PATH:~/bin

That’s it. Just type ‘xslt’ (or whatever you named the text file) at the command prompt for usage.

your-mac-g4:~ heyzeus$ xslt
ERROR: no input XML file specified.  Usage: 
xslt [XML-FILE] [XSLT-FILE]
Originally published April 23, 2006

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 23rd April 2006