Hi there
I am skilled at XSLT (1.0 mostly), XML and PHP, but my current project requires that I implement an XML to HTML transformation. This is new to me and I am currently checking out my options.
One of them being that I use DOMDocument and XSLT stylesheets to transform the XML file to HTML output.
I have the following code that works fine
Code:
<?php
$stylesheet = '<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><head></head><body>
<xsl:for-each select="//nodes/*">
<xsl:value-of select="name()"/><br/>
</xsl:for-each>
</body></html>
</xsl:template>
</xsl:stylesheet>';
$xslDoc = new DOMDocument();
$xslDoc->loadXML( $stylesheet );
$xmlDoc = new DOMDocument();
$xmlDoc->load("test.xml");
$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);
Because I have a modular approach in mind, i.e. the users should be able to select which parts of the XML file they want to be transformed and displayed, and the function 'XSLTProcessor::importStylesheet' does not allow to import more than one stylesheet file, I decided to create a stylesheet string instead. The stylesheet string will be created according to the user selection (not yet in the code).
The code loads then the final stylesheet string with the 'DOMDocument::loadXML' function.
My questions:
1. If you load an XML string, you have to wrap it in an XML node. Otherwise, it does not seem to be a real XML document. Do I have to do this with the XSLT string too?
2. Does anyone has experience with loading XSLT strings? Do I have to worry that some tags/characters will be filtered, if I use a string?
3. I would also appreciate comments on whether this is a good approach or not. Constructive comments and suggestions are most welcome.
My system:
libxslt Version 1.1.28, libxslt compiled against libxml Version 2.8.0, EXSLT enabled, libexslt Version 1.1.28, libxml2 Version 2.8.0
PHP 5.4.10 on my localhost webserver MAMP
regards goedda