Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : XSL functions : XSLTProcessor::setParameter


Set value for a parameter ()

Example 2653. Changing the owner before the transformation


= array(
'Marc Rutkowski' => 'marc',
'Olivier Parmentier' => 'olivier'

$xsl = new DOMDocument;

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

foreach ($collections as $name => $file) {
// Load the XML source
$xml = new DOMDocument;
$xml->load('collection_' . $file . '.xml');

$proc->setParameter('', 'owner', $name);
$proc->transformToURI($xml, 'file:///tmp/' . $file . '.php');


Related Examples ( Source code ) » xsl xsltprocessor set parameter

Code Examples / Notes » xsl xsltprocessor set parameter

stoke a

XSLTProcessor::removeParameter() won't work on parameters set via XSLTProcessor::setParameter(string namespace, array options) in 5.1+ -- they need to be set and removed one by one.


It seems heinemann's usage is not correct and does not achieve the intended result.
This method's purpose is to change a global <xsl:param> value in the XSL stylesheet--not to change an attribute of any other element.  <xsl:param> basically lets you set up a stylesheet which can be customized (as from PHP) externally (without needing to tamper with the original XSL file).
Here's an example of usage (that will work):
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
<xsl:param name="print_something" select="defaultstring"/>
<xsl:template match="/mydoc">
<p style="color:red;">Printed parameter: <xsl:value-of select="$print_something"/>
$dom = new DOMDocument();
$xsl = new XSLTProcessor;
$xsl->setParameter( '', 'print_something', "Now I've overridden the default!");
$style = realpath( "./my_stylesheet.xslt" );
$out = $xsl->transformToXML( $dom );
var_dump( '<pre>',
   htmlentities( $out, ENT_QUOTES, 'utf-8' ),
   $xsl->getParameter('', 'print_something'),
'</pre>' );
string(5) "
string(143) "<?xml version="1.0"?>
<p style="color:red;">Printed parameter: Now I've overridden the default!
string(32) "Now I've overridden the default!"
string(6) "
Notice that at present adding a namespace will not work. The only option at present is to set the first parameter for namespace to an empty string (though you can add the prefix with colon to the second argument for name in order to set the parameter for a namespace-prefixed parameter name).

orion i

I've been trying to pass in a DOMDocument object as a parameter so I can stuff a bunch of data into XML nodes, but it appears that this function is not capable of it. I was hoping to get it to work like it does in the .NET 2.0 framework. (See
But after looking at the PHP 5.2.1 source code, /php-5.2.1/ext/xsl/xsltprocessor.c line 604-650, it appears that it's not possible to do so in PHP even though it appears that libxslt supports it (see
In fact, if the parameters aren't exactly what's expected, you'll always get a warning like this:
Wrong parameter count for XSLTProcessor::setParameter()

heinemann dot juergen

Example for how it works.
<?xml version = '1.0' encoding = 'utf-8' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
<xsl:output method="xml"
doctype-system = "-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-public = ""
<xsl:template match="docs">
<xsl:for-each select="block">
<xsl:value-of select="." />
$dom = new DomDocument( '1.0', 'utf-8' );
$xsl = new XSLTProcessor;
$xsl->setParameter( 'block', 'xmlns', '' );
$style = realpath( "./my_stylesheet.xslt" );
$dom->load( $style );
$xsl->importStyleSheet( $dom );
$dom->loadXML( '<docs>
<block>Howto set xhtml Transitional Namespaces width php</block>
</docs>' );
$out = $xsl->transformToXML( $dom );
var_dump( '<pre>',
htmlentities( $out, ENT_QUOTES, 'utf-8' ),
$xsl->getParameter( 'block', 'xmlns' ),
$xsl->getParameter( 'docs', 'xmlns' ),
'</pre>' );


After looking at this a little further (see, it appears that it's a shortcoming of libxslt, not PHP, that prevents passing in DOMDocuments or DOMNodes as parameters.

Change Language

Follow Navioo On Twitter
eXTReMe Tracker