XSLTProcessor::importStylesheet

(PHP 5, PHP 7, PHP 8)

XSLTProcessor::importStylesheet β€” Π˜ΠΌΠΏΠΎΡ€Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ стилСй

ОписаниС

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

ΠœΠ΅Ρ‚ΠΎΠ΄ ΠΈΠΌΠΏΠΎΡ€Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ стилСй Π² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ XSLTProcessor для трансформации.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

stylesheet

Π˜ΠΌΠΏΠΎΡ€Ρ‚ΠΈΡ€ΡƒΠ΅ΠΌΠ°Ρ Ρ‚Π°Π±Π»ΠΈΡ†Π° стилСй Π² Π²ΠΈΠ΄Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° Dom\Document ΠΈΠ»ΠΈ DOMDocument.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Ошибки

ΠœΠ΅Ρ‚ΠΎΠ΄ выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ TypeError, Ссли Ρ‚Π°Π±Π»ΠΈΡ†Π° стилСй stylesheet Π½Π΅ XML-ΠΎΠ±ΡŠΠ΅ΠΊΡ‚.

Бписок измСнСний

ВСрсия ОписаниС
8.4.0 Π”ΠΎΠ±Π°Π²ΠΈΠ»ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΡƒ класса Dom\Document.
8.4.0 ΠœΠ΅Ρ‚ΠΎΠ΄ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ TypeError вмСсто ошибки ValueError, Ссли Ρ‚Π°Π±Π»ΠΈΡ†Π° стилСй stylesheet Π½Π΅ XML-ΠΎΠ±ΡŠΠ΅ΠΊΡ‚.
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
5
kevin at metalaxe 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.
up
3
bbrosseau at gmail ΒΆ
21 years ago
For those who wants to use external documents, it is important not to use the DomDocument::loadXML because the processor will not have the path to look 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_get_contents($f));
?>

document('other.xml') will not work with relative path and <?php $xsl = DomDocument::load($f); ?> will!
up
1
diesel at spbnet dot ru ΒΆ
19 years ago
This is not a problem. You may set DOMDocument's documentURI property. 
Something like this 

<?php
$xsl = new DOMDocument('1.0','UTF-8');
     
$xsl->loadXML(file_get_contents('/foo/bar/somefile.xsl');
$xsl->documentURI = '/foo/bar/somefile.xsl';

$xslProc = new XSLTProcessor();
$xslProc->importStylesheet($xsl);
?>

and document('other.xsl') will work fine!
up
0
fcartegnie ΒΆ
18 years ago
PHP5 xsl processor has a different behaviour than PHP4's one with CDATA sections. (see http://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_get_contents('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.
up
-3
rbmeo at yahoo dot com ΒΆ
13 years ago
To make 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.