XSLTProcessor::importStylesheet
(PHP 5)
XSLTProcessor::importStylesheet — Import stylesheet
Description
void XSLTProcessor::importStylesheet
( object
$stylesheet
)This method imports the stylesheet into the XSLTProcessor for transformations.
Return Values
No value is returned.
Changelog
Version | Description |
---|---|
5.2.8 | Accepts SimpleXMLElement again which was broken since PHP 5.2.6. |
- Функция XSLTProcessor::__construct() - Создаёт новый экземпляр класса XSLTProcessor
- Функция XSLTProcessor::getParameter() - Возвращает значение параметра
- Функция XsltProcessor::getSecurityPrefs() - Get security preferences
- Функция XSLTProcessor::hasExsltSupport() - Определяет имеет ли PHP поддержку EXSLT
- Функция XSLTProcessor::importStylesheet() - Импортирует таблицу стилей
- Функция XSLTProcessor::registerPHPFunctions() - Включает возможность использовать PHP функции в качестве функций XSLT
- Функция XSLTProcessor::removeParameter() - Удаляет параметр
- Функция XSLTProcessor::setParameter() - Устанавливает значение параметра
- Функция XSLTProcessor::setProfiling() - Устанавливает файл для профилирования
- Функция XsltProcessor::setSecurityPrefs() - Set security preferences
- Функция XSLTProcessor::transformToDoc() - Преобразовует в DOMDocument
- Функция XSLTProcessor::transformToUri() - Преобразует в URI
- Функция XSLTProcessor::transformToXML() - Преобразует в XML
Коментарии
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!
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!
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.
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.
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.