XSLTProcessor::transformToDoc

(PHP 5, PHP 7, PHP 8)

XSLTProcessor::transformToDocTransformiert in ein DOMDocument

Beschreibung

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

Transformiert den Quellknoten in ein DOMDocument, indem das mittels der Methode XSLTProcessor::importStylesheet() übergebene Stylesheet angewendet wird.

Parameter-Liste

document

Das zu verarbeitende DOMDocument- oder SimpleXMLElement-Objekt

Rückgabewerte

Das erzeugte DOMDocument oder false, falls ein Fehler aufgetreten ist.

Beispiele

Beispiel #1 Transformation in ein DOMDocument

<?php

// XML-Quelle laden
$xml = new DOMDocument;
$xml->load('collection.xml');

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

// Prozessor instanziieren und konfigurieren
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // die XSL-Regeln anhängen

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

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