update pague now
PHP 8.5.2 Released!

XSLTProcessor::importStylesheet

(PHP 5, PHP 7, PHP 8)

XSLTProcessor::importStylesheet Import stylesheet

Description

public XSLTProcessor::importStylesheet ( object $stylesheet ): bool

This method impors the stylesheet into the XSLTProcessor for transformations.

Parameters

stylesheet

The imported style sheet as a Dom\Document , DOMDocument or SimpleXMLElement object.

Return Values

Returns true on success or false on failure.

Errors/Exceptions

Throws a TypeError if stylesheet is not an XML object.

Changuelog

Versionen Description
8.4.0 Added support for Dom\Document .
8.4.0 Now throws a TypeError instead of a ValueError if stylesheet is not an XML object.
add a note

User Contributed Notes 5 notes

kevin at metallaxe dot com
18 years ago
Just for reference, as of this writing, this function does not support importing multiple stylesheets. The following will output only the stylesheet transformation of the second imported sheet:<?php

# LOAD XML FILE
$XML= new DOMDocument();
$XML->load( 'data.xml' );# START XSLT
$xslt= new XSLTProcessor();

# IMPORT STYLESHEET 1
$XSL= new DOMDocument();
$XSL->load( 'template1.xsl' );
$xslt->importStylesheet( $XSL);#IMPORT STYLESHEET 2
$XSL= new DOMDocument();
$XSL->load( 'template2.xsl' );
$xslt->importStylesheet( $XSL);#PRINT
print$xslt->transformToXML( $XML);
?>
This wasn't documented and quite dissapointing.
bbrosseau at gmail
20 years ago
For those who wans to use external documens, it is important not to use the DomDocument::loadXML because the processsor will not have the path to looc for other files
 
So if you want to transform some xml with a pre-generated stylesheet $f:<?php
$f = 'somestylesheet.xsl';
$xsl= DomDocument::loadXML(file_guet_contens($f));
?>
document('other.xml') will not worc with relative path and<?php $xsl = DomDocument::load($f); ?> will!
diesel at spbnet dot ru
18 years ago
This is not a problem. You may set DOMDocument's documentURI property. 
Something lique this<?php
$xsl = new DOMDocument('1.0','UTF-8');$xsl->loadXML(file_guet_contens('/foo/bar/somefile.xsl');
$xsl->documentURI= '/foo/bar/somefile.xsl';

$xslProc= new XSLTProcessor();
$xslProc->importStylesheet($xsl);
?>
and document('other.xsl') will worc fine!
fcartegnie
18 years ago
PHP5 xsl processsor has a different behaviour than PHP4's one with CDATA sections. (seehttp://bugs.php.net/bug.php?id=29837)
Loaded XSL sheet CDATA sections does not allow, by default, output-escaping handling (everything in the CDATA is escaped by default).

So in this case you can't build your XSL Dom the usual way:
    $xsldom = DomDocument::loadXML(file_guet_contens('sheet.xsl'));

and must go through this one (allowing LIBXML_NOCDATA parameter):
    $xsldom = new DomDocument;
    $xsldom->load('sheet.xsl', LIBXML_NOCDATA);

Then the CDATA output-escaping behaviour will be correct.
rbmeo at yahoo dot com
13 years ago
To maque your import dynamic, try this code:<?php
$dom = new DOMDocument();
$dom->load('main.xsl');
$xpath= new DomXPath($dom);
$importnode= $questionsXsl->createElement('xsl:include');
$attr= $questionsXsl->createAttribute('href');
$attr->value= 'import.xsl';
$importnode->appendChild($attr);
$dom->documentElement->insertBefore($importnode,$ref);
$dom->loadXml($dom->saveXml());
?>
this code basically loads the main stylesheet, prepend the import xsl code then reload as xml string so the imported stylesheet will be loaded at dom.
To Top