Skip to content
 

Dynamically Assigning XSLT And XML Files Via PHP And Apache mod_rewrite

Recently asked on Yahoo! Answers:

How to work with XML files in PHP?
I’ve a couple directories filled with thousands of files.

Also all these files should be reachable with their filename (but have . at the end instead of .) in the url.

For example in the article directory the file named “news.”should be reachable like this:
http://www.website.com/article/news.php
2nd example in the articles directory the file named “helpwithjeans.” should be reachable like this:
http://www.website.com/article/helpwithj
3rd example in the products directory the file named “bluray.” should be reachable like this:
http://www.website.com/product/bluray.ph

How to read what url is typed and process it by going to the right file? After that the still needs to be converted to one of several specific html formats.

Something like using the parameter “l” see in above examples (2nd and 3rd) to know which layout is desired and which elements to grab.

How can this be handled? And if there is multiple options, what do you suggest is a good way to handle this.

Thanks for pointing me in the right direction!

I was thinking might be the way to convert the to html but I don’t know how to use the “l” parameter from the url (if I’m heading int hat direction) in the and that would really be needed.

I’m really open to any input. Everything may help. Thanks a lot.

The most efficient way to accomplish this task is via a combination of Apache’s mod_rewrite module and ’s XSLTProcessor class.

By adding the two together, we can reduce the number of files needed to convert any number of these files to one file.

(A quick aside: A previous answer to the same question provides a similar methodology to what I describe here. The work here is all original, including my conclusion of the best way to proceed. It just happens that the previous answer jives with this answer.

I had been asked to answer this by the questioner to answer this question, but my work load caused a delay in responding. I did not read the previous answer until after this blog entry was written.)

The Template Switcher

The mechanics are well established: We’re going to have lots of uniquely named files, but they’re all going to conform in terms of schema.

In other words, the data in each file will be different, but the node hierarchy and syntax will be the same for all files. To ensure we can use the same files to transfer multiple files, the structure of the files must be consistent.

We’ll use to switch templates based on the URL parameters noted in the question. (We’ll tweak the URL a bit to be search-engine friendly; more on that in a bit.)

We can do this quite easily in 5 thanks to the XSLTProcessor class, which is installed by default. All we need is for our page to receive from a querystring variable the name of the file to load and the file to apply. The class does the rest.

define('DIR_ROOT', dirname(__FILE__));
 
//check inputs
if($_GET['xml'] == "") {
	die('XML file input not specified');
}
elseif($_GET['xsl'] == "") {
	die('XSL file input not specified');
}
 
$xmlfile = DIR_ROOT . "/" . $_GET['xml'] . ".xml";
$xslfile = DIR_ROOT . "/" . $_GET['xsl'] . ".xsl";
 
if(!is_file($xmlfile)) {
	die('XML file does not exist');
}
elseif(!is_file($xslfile)) {
	die('XSL file does not exist');
}
 
// Load and apply the XML / XSLT source
$xml = new DOMDocument;
$xml->load($xmlfile);
 
$xsl = new DOMDocument;
$xsl->load($xslfile);
 
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
 
//output transformed XML
echo $proc->transformToXML($xml);

The mod_rewrite Rule

With our file saved as index. and stored in the same directory that holds all our and files, we can now set up mod_rewrite to create, from a static URL, the querystring variables we want to process.

The URL we will rewrite will conform to a simple syntax: article/{}/{}/. For example, if we have an file named “helpwithjeans.” and an file named “simple.xsl”, the URL might appear as http://www.website.com/article/simple/helpwithjeans/. If we have a stylesheet named “advanced.xsl” and want to apply that to the same article, our URL would be http://www.website.com/article/advanced/helpwithjeans/.

To create a rewrite rule, you first need to ensure you have installed the mod on your server (most Web hosting companies will have installed this module). Then, you add the appropriate lines to your .htaccess file, at the root directory of your Web site, to break the URL into querystring variables and pass it to index.:

RewriteEngine On
RewriteRule ^article/([^/]+)/([^/]+)/?$ article/index.php?xsl=$1&xml=$2 [L]

This rewrite rule assumes that the article subdirectory does, indeed, exist, and that within it are the page that does the template switching, all the stylesheets and all the documents. You can, of course, change paths as appropriate.

And believe it or not, that’s all there is to it.

I distribute all code under the GNU GPL version 3.

Comments (2)

  1. Eric Covener says:

    You mention Rewrite + htaccess, but your RewriteRule starts with “^/” — this is unmatchable in per-directory context. If the htaccess were in your docroot, ^article/… would be the “longest” matching regex.

  2. Thanks for the heads-up, Eric. I’ve changed the code to reflect the proper rewrite rule. (And next time, I’ll test code before I post it.)

Leave a Reply

Spam Protection by WP-SpamFree