PHP Doku:: XSL - book.xsl.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:

Ein Service von Reinhard Neidl - Webprogrammierung.






23 BenutzerBeiträge:
- Beiträge aktualisieren...
sudhir dot vis at gmail dot com
25.04.2009 20:40
Here is a simplest way to transform the xml from php

= "<xml>";
$sXml .= "<sudhir>hello sudhir</sudhir>";
$sXml .= "</xml>";

$XML = new DOMDocument();
$XML->loadXML( $sXml );

$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load( 'xsl/index.xsl', LIBXML_NOCDATA);
$xslt->importStylesheet( $XSL );
print $xslt->transformToXML( $XML );
allens at nospam dot com
20.04.2009 5:45
Getting Netbeans error message:
Fatal error: Class 'xsltprocessor' not found

place copy of php.ini in the c:\windows directory!

by default the php.ini file has xsl for the line


to enable XSL....remove ; so that the extension gets included

Then if Netbeans gives you the following message:
PHP Startup: Unable to load dynamic library './php_xsl.dll'

then you need to run the php-5.2.9-1-win32-installer.msi again
this time selecting the XSL extension!
visual77 at gmail dot com
21.09.2008 1:35
The XSLTProcessor is kind of a pain to use to generate an entire website, mostly due to the tedious process of building an XML tree through DOMDocument's createElement and appendChild functions.

To get an entire website out of nothing but XML and XSL (which really is a superior system to just echoing out the HTML), you could use an object that does all the PHP data structure to XML data tree conversion, and combines with the XSLTProcessor for rendering. A good object like this is DOMi, which you can find at
emilise dot victor at gmail dot com
12.03.2008 17:10
Here is a way to get an output without using <xml:message> : use the var_dump function of php :
- First bind the php functions with
<? $xslt->registerPHPFunctions() ?>
and xmlns:php="" in the stylesheet
- Then you can call the var_dump function like this : <xsl:variable name="a" select="php:function('var_dump', $return)" />

I couldn't find a way to call the function and have it really print the result without creating a variable, but well, it's just for debugging...
shangxiao at php dot net
11.01.2008 2:38
When using the DOM api to add xsl commands to your stylesheet dynamically, be sure to use createElementNS() rather than createElement() otherwise the XSLTProcessor will not recognise these DOM nodes.

$xsl->createElementNS('', 'xsl:element');
pb at online-magazin dot at
12.10.2007 0:13
Calling the Saxon XSLT Processor is a very esay task to do!

You just need to do some simple task

1. Install the JavaBridge for PHP

2. Download the freeware (B) Saxon distribution from

3. Put the jar files in a directory whre you have access

4. Use this sample of code

// Directory where the jar files are located
define("SAXON_DIR", $_SERVER['DOCUMENT_ROOT']."/saxonb8.9.0/");

// include the jars
$sXslFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xsl"; // The xsl file
$sXmlFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xml"; // The xml file

  $oXslSource = new java("", "file://".$sXslFile);
  $oXmlSource = new java("", "file://".$sXmlFile);

  $oFeatureKeys = new JavaClass("net.sf.saxon.FeatureKeys");

  // Create the Factory
  $oTransformerFactory = new java("net.sf.saxon.TransformerFactoryImpl");

  //Disable source document validation
  $oTransformerFactory->setAttribute($oFeatureKeys->SCHEMA_VALIDATION, 4);

  // Create a new Transformer
  $oTransFormer = $oTransformerFactory->newTransformer($oXslSource);
  // Create a StreamResult to store the output
  $oResultStringWriter = new java("");
  $oResultStream = new java("", $oResultStringWriter);
  // Transform
  $oTransFormer->transform($oXmlSource, $oResultStream);
  // Echo the output from the transformation
  echo java_cast($oResultStringWriter->toString(), "string");
catch(JavaException $e){
   echo java_cast($e->getCause()->toString(), "string");

5. Enjoy

This is working quite well.
Vasil Rangelov
21.08.2007 16:31
@basslines at gmail dot com
As far as I can tell, the most recent stable versions of LibXML/LibXSLT/LibEXSLT do NOT support xPath 2.0 / XSLT 2.0 transformations. The only support for XSLT 2.0 that I've found is in Java's SAXON processor (

Using SAXON or AltovaXML by using their JAVA bindings could prove to be a difficult task to do, and .NET seems to be buggy (at least for me). However, I've made a class that uses SAXON or AltovaXML from the command line. AltovaXML could also be used with COM, provided it's registered as a COM component.

You can download my wrapper from:

I still need to implement a more graceful error handling (the way the XSL extension uses the Libxml functions), and once the bug
is fixed, I'll implement the .NET interfaces too.

I'll appreciate any feedback.
geoffreyj dot lee at gmail dot com
2.07.2007 8:02
Here are a couple useful tips for those who care about cross-browser compatibility.


<script type="text/javascript" src="test.js"></script>

XSLT will automatically condense it to:

<script type="text/javascript" src="test.js"/>

While this is good, Internet Explorer breaks on empty script tags. So, the simple solution is to add something to prevent an empty tag. For example:

<script type="text/javascript" src="test.js"><xsl:comment/></script>


<script type="text/javascript" src="test.js">



Also, here is a way to use IE's conditional comments:

<xsl:comment>[if gte IE 5]&#62;
&#60;link rel="stylesheet" type="text/css" href="ie.css" /&#62;


<!--[if gte IE 5]>
<link rel="stylesheet" type="text/css" href="ie.css" />

This will let you isolate IE-related CSS without needing to use ugly CSS hacks.
gabriel dot birke at web dot de
10.05.2007 12:19
Each <xsl:message> tag will generate a PHP error (level Warning). If you want to collect and pretty-print them, you could use a custom error handling function that collects the warnings in a static variable.
mnot at mnot dot net
9.05.2007 5:59
You can set an HTTP proxy for the XSLT document() function to use (as well as DTD external references) by setting http_proxy in the environment.

E.g., in Apache configuration;

SetEnv http_proxy
Ryan D. Hatch
2.05.2007 10:14
Looking for php_xsl.dll?

If you installed on Windows using the MSI Installer - you may not have it.

1.) Add/Remove Programs and Change PHP Installation.  Select XSL Extension.


2.) Download complete .zip file of PHP and you can copy php_xsl.dll into your PHP/etc directory.

Just a note.

Ryan D. Hatch
adrian at foeder dot de
14.09.2006 11:45
If you want to use the document()-function inside your XSL stylesheet, please note that you have to use an absolute path there.
Important for windows users: the absolute path *has* to be with forward slashes, so subsitute windows-path-backslashes to forward slashes before you transform the document.

This will NOT work:
<xsl:copy-of select="document('test.xml')" />

This will also NOT work:
<xsl:copy-of select="document('c:\temp\test.xml')" />

But this WILL work:
<xsl:copy-of select="document('c:/temp/test.xml')" />

28.04.2006 3:26
Enable libxslt library, PHP 5, under windows:

To Enable:
In your php.ini
1. Uncomment ;extension=php_xsl.dll
2. Change extension_dir to
     extension_dir = "./ext"

To Confirm:
1. In a test.php page:
<?php phpinfo() ?>
2. Run test.php
3. Search for "libxslt Version". It should return a version number in a XSL headed table.

Further info Google
"Configuring and Testing PHP Servers for XSL Support"
Fabrice Bonny
30.03.2006 11:34
In response to how to use entities from DTD (internal or external) in XSLT. It works if you do this way:

$xsl = new DOMDocument;
$xsl->resolveExternals = TRUE;
$xsl->substituteEntities = TRUE;

Hope this helps! ;-)
basslines at gmail dot com
29.03.2006 20:07
As far as I can tell, the most recent stable versions of LibXML/LibXSLT/LibEXSLT do NOT support xPath 2.0 / XSLT 2.0 transformations. The only support for XSLT 2.0 that I've found is in Java's SAXON processor (

17.03.2006 18:14
In response to appletalk at gmail dot com
As many of you may have noticed, DOM parser gives errors if the '&nbsp;' entity is present. The E_WARN message looks like:
Warning: DOMDocument::load() [function.load]: Entity 'nbsp' not defined in ...
There're many ways to solve this:
b) Defining &nbsp;
At the top of the document, after the <?xml?> definition, add:
   <!DOCTYPE xsl:stylesheet [
   <!ENTITY nbsp "&#160;" >
Just wanted to let people know that option b does NOT work. I'm not sure why this isn't implemented correctly, but it isn't, so don't waste your time. It's unfortunate the DOMXSL transform is so much less capable than the old xslt function.
olivier dot sow at alias dot france
5.08.2005 15:27
if you have 'XML parser error 4: not well-formed (invalid token)' error without but your are sure to do no error at all, try this:
$Str = '';

$xp = xslt_create() or trigger_error('Could not create XSLT process.', E_USER_ERROR);

xslt_set_encoding($xp, 'ISO-8859-1');

// read the files into memory
$xsl_string = join('', file($XSLFile));
$xml_string = join('', file($XMLFile));

// set the argument buffer
$arg_buffer = array('/xml' => $xml_string, '/xsl' => $xsl_string);

// process the two files to get the desired output
if (($Str = xslt_process($xp, 'arg:/xml', 'arg:/xsl', NULL, $arg_buffer, $xsl_params))){

//Saves transformation result into '$ResultFile'
$fp = fopen($ResultFile,"w+");


found here:
venkatesh at lammersmedical dot com
1.03.2005 15:17
Here is function to read from the XSL sheet which is saved as a text file.

function ReadExcelSheet($filename){
             for ($i=0; $i<count($ar); $i++) {
                       if((substr($ar[$i],0,1)!= "\t")){
appletalk at gmail dot com
5.02.2005 7:32
As many of you may have noticed, DOM parser gives errors if the '&nbsp;' entity is present. The E_WARN message looks like:

Warning: DOMDocument::load() [function.load]: Entity 'nbsp' not defined in ...

There're many ways to solve this:
a) The hard way
<xsl:text disable-output-escaping="yes"> &amp;nbsp;</xsl:text>

b) Defining &nbsp;
At the top of the document, after the <?xml?> definition, add:
   <!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp "&#160;" >

c) External Doctype
Just in case you want need other HTML entities, you can call an external doctype with the proper definitions


Of course, you can download the file and place it in your server.
kekoajs at yahoo dot com
22.12.2004 23:07
Wow, I spent the better part of a day looking for how one could pass an entire test expression to an XSL stylesheet.  It seems that the XSLT 1.0 specification doesn't support it but PHP 5 (and maybe 4s) inclusion of  EXSLT allows one to do exactly that...

simply add these lines...


to the <xsl:stylesheet> element and when using an expression stored in a <xsl:param> element write

<xsl:if test="dyn:evaluate($param-name)">

and viola!  you can now use expressions generated externally in your stylesheet!

EXSLT adds many useful functions that can be integrated into your XSL in a similar fashion.  You can go to to learn more...
rd at jerntorget dot se
24.09.2004 12:58
If you get the following warning message:

xsltApplyOneTemplate: _if_ was not compiled in (if can for example be apply-template or some other method), i've found that the problem seems to be an &nbsp; directly before the <xsl:if> or what ever is causing the problem.

One way to get thru the problem is to use span tags around &nbsp;  like : <span>&nbsp;</span>
jw at jwscripts dot com
6.09.2004 11:23
The following code is a wrapper to support calls to some of the old xslt_* functions:


if (PHP_VERSION >= 5) {
// Emulate the old xslt library functions
function xslt_create() {
        return new

$xslcontainer = null,
$args = null,
$params = null) {
// Start with preparing the arguments
$xml_arg = str_replace('arg:', '', $xml_arg);
$xsl_arg = str_replace('arg:', '', $xsl_arg);

// Create instances of the DomDocument class
$xml = new DomDocument;
$xsl = new DomDocument;

// Load the xml document and the xsl template

// Load the xsl template

// Set parameters when defined
if ($params) {
            foreach (
$params as $param => $value) {
$xsltproc->setParameter("", $param, $value);

// Start the transformation
$processed = $xsltproc->transformToXML($xml);

// Put the result in a file when specified
if ($xslcontainer) {
            return @
file_put_contents($xslcontainer, $processed);
        } else {


xslt_free($xsltproc) {

$arguments = array(
'/_xml' => file_get_contents("newxslt.xml"),
'/_xsl' => file_get_contents("newxslt.xslt")

$xsltproc = xslt_create();
$html = xslt_process(


24.06.2004 4:43
If you're want to use XML from a variable e.g. $xmldata but the XSLT-StyleSheet from a file, you can do it also the following way:

= new xsltProcessor;

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)