XSLTProcessor::setParameter

(PHP 5, PHP 7, PHP 8)

XSLTProcessor::setParameterSet value for a parameter

Description

public XSLTProcessor::setParameter(string $namespace, string $name, string $value): bool
public XSLTProcessor::setParameter(string $namespace, array $options): bool

Sets the value of one or more parameters to be used in subsequent transformations with XSLTProcessor. If the parameter doesn't exist in the stylesheet it will be ignored.

Parameters

namespace

The namespace URI of the XSLT parameter.

name

The local name of the XSLT parameter.

value

The new value of the XSLT parameter.

options

An array of name => value pairs.

Return Values

Returns true on success or false on failure.

Errors/Exceptions

Throws a ValueError if any of the arguments contain null bytes.

Changelog

Version Description
8.4.0 Now throws a ValueError if any of the arguments contain null bytes instead of silently truncating.
8.4.0 It is now possible to set a parameter value containing both single and double quotes. Prior to PHP 8.4.0, this resulted in a warning.

Examples

Example #1 Changing the owner before the transformation

<?php

$collections
= array(
'Marc Rutkowski' => 'marc',
'Olivier Parmentier' => 'olivier'
);

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

/ 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 . '.html');
}

?>

See Also

add a note

User Contributed Notes 6 notes

up
1
Lennaert van der Linden
17 years ago
[EDIT by nielsdos: This is no longer true as of PHP 8.4.0]

The parameter will not be set if the value contains both single and double quotes. Instead a warning will be shown when transforming the document:

PHP Warning: XSLTProcessor::transformToXml(): Cannot create XPath expression (string contains both quote and double-quotes)
up
2
richard at aggmedia dot net
16 years ago
Note that there is no way to remove a parameter from an XSLTProcessor unless you know its name, and there is no way (that I can find) to get a list of the current parameters.

This means that you cannot reuse an XSLTProcessor with different parameters unless you call XSLTProcessor->removeParameter() on every parameter, and to do that you need to know the names of all the currently set parameters.

I bumped into this because we were caching XSLTProcessors for reuse, and they were spitting out content based on phantom parameters (they were still there from previous uses).
up
1
brettz9
18 years ago
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):

Stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://bugs.php.net/bug.php?id=30622
up
0
OrionI
18 years ago
After looking at this a little further (see http://bugs.php.net/bug.php?id=41248), it appears that it's a shortcoming of libxslt, not PHP, that prevents passing in DOMDocuments or DOMNodes as parameters.
up
0
Orion I
18 years ago
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 http://xmlsoft.org/XSLT/html/libxslt-variables.html
#xsltParseGlobalParam)

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()
up
-1
heinemann dot juergen at hjcms dot de
19 years ago
Example for how it works.

<?xml version = '1.0' encoding = 'utf-8' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.php.net</block>
</docs>'
);

$out = $xsl->transformToXML( $dom );

var_dump( '<pre>',
htmlentities( $out, ENT_QUOTES, 'utf-8' ),
$xsl->getParameter( 'block', 'xmlns' ),
$xsl->getParameter( 'docs', 'xmlns' ),
'</pre>' );

?>
To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant