XSLTProcessor::transformToDoc

(PHP 5, PHP 7, PHP 8)

XSLTProcessor::transformToDocTransformierung in ein neues DOMDocument

Beschreibung

public XSLTProcessor::transformToDoc ( object $document , string|null $returnClass = null ) : DOMDocument|false

Transformiert die Quellnode unter Verwendung des mittels XSLTProcessor::importStylesheet() importierten Stylesheets in ein neues DOMDocument.

Parameter-Liste

document

Die zu verarbeitende Node.

Rückgabewerte

Das erzeugte DOMDocument oder false im Falle eines Fehlers bei der Verarbeitung.

Beispiele

Beispiel #1 Transformierung in ein DOMDocument

<?php

// Laden der XML/XSL-Quelldokomente
$xml = new DOMDocument;
$xml->load('collection.xml');

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

// Prozessor instanziieren und konfigurieren
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // XSL Document importieren

echo trim($proc->transformToDoc($xml)->firstChild->wholeText);

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!

Siehe auch

add a note add a note

User Contributed Notes 1 note

up
1
franp at free dot fr
17 years ago
In most cases if you expect XML (or XHTML) as output you better use transformToXML() directly. You gain better control over xsl:output attributes, notably omit-xml-declaration.

Instead of :
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$dom = $proc->transformToDoc($xml);
echo $dom->saveXML();

do use :
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$newXml = $proc->transformToXML($xml);
echo $newXml;

In the first case, <?xml version="1.0" encoding="utf-8"?> is added whatever you set the omit-xml-declaration while transformToXML() take the attribute into account.
To Top