Saturday, November 8, 2008 at 02:25
XSLT == bollocks
The other day I was doing some work on a WordPress site that involved importing some data from an XML file and displaying it on a page (in a sidebar). Naturally, there were two ways to go about doing this.
Since this was already a PHP site, we could have whipped up some sort of PHP XML parser and then used a PHP include on sidebar.php. The other option was using a minimal amount of PHP and relying more on XSLT so that we could just have the XML file and a stylesheet. Since we were trying to adhere to the ‘the best code is the kind you don’t have to write’ strategy, we decided to try XSLT.
The cool thing about XSLT is that if you know what you’re doing you can easily change XML into HTML since they’re so similar anyway. Unfortunately, it’s almost impossible to have the same kind of control you would have with PHP. Specifically, I needed to take a value from the XML file (specifically an Amazon ASIN) and then append it to a link’s href.
So why does XML suck at this? Because you can’t have something like:
<a href="http://amazon.com/<xsl:value-of select="asin"/>...
…Because obviously this produces a parsing error. So the solution would appear to be making the entire anchor tag into CDATA so that the xsl could be stuck in the middle. But the problem with this is that when the xml output method is set to html, the < and > characters automatically get turned into HTML entities such as < which then cause the tag to show up on the page, not interpreted as markup.

So of course there is the option of using the plain text output method, but this–surprise!–turns <br /> into carriage returns which, again, are not interpreted by the browser.
The solution I ended up with? Wrap every HTML tag as CDATA. Yes, that is what it came to.
XML does not solve all the world’s problems, folks.