PHP - Fonction XSLTProcessor :: transformToDoc ()

Définition et utilisation

XML est un langage de balisage permettant de partager les données sur le Web, XML est à la fois lisible par l'homme et par machine. L'extension XSL est une implémentation du standard XSL pour effectuer une transformation XSTL à l'aide de la bibliothèque libxslt.

le XSLTProcessor::transformToDoc() function accepte un objet de la classe DOMNode comme paramètre et le transforme en DOMDocument en lui appliquant une feuille de style.

Syntaxe

XSLTProcessor::transformToDoc($doc);

Paramètres

Sr. Non Paramètre et description
1

doc(Mandatory)

Il s'agit d'un objet de la classe DOMNode représentant le document à transformer.

Valeurs de retour

Cette fonction renvoie un objet de la classe DOMDocument en cas de succès et une valeur booléenne qui est FALSE en cas d'échec.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

Voici un exemple de cette fonction -

sample.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<Tutorial>
   <Title>JavaFX</Title>
   <Authors>
      <Author>Krishna</Author>
      <Author>Rajeev</Author>
   </Authors>
   <Body>Sample text</Body>
</Tutorial>

sample.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>

   <xsl:template match="/">
      Title - <xsl:value-of select="/Tutorial/Title"/>
      Authors: <xsl:apply-templates select="/Tutorial/Authors/Author"/>
   </xsl:template>

   <xsl:template match="Author">
      - <xsl:value-of select="." />
   </xsl:template>
</xsl:stylesheet>

sample.php:

<?php
   //Loading an XSL document
   $xsl = new DOMDocument();
   $xsl->load("sample.xsl");

   //Loading an XML document
   $xml = new DOMDocument();
   $xml->load("sample.xml");

   //Creating an XSLTProcessor
   $proc = new XSLTProcessor();

   //Importing the XSL document
   $proc->importStyleSheet($xsl);

   //Transforming the style to XML
   $res = $proc->transformToDoc($xml);
   print_r($res);
?>

Cela produira le résultat suivant -

DOMDocument Object
(
   [doctype] =>
   [implementation] => (object value omitted)
   [documentElement] =>
   [actualEncoding] =>
   [encoding] =>
   [xmlEncoding] =>
   [standalone] => 1
   [xmlStandalone] => 1
   [version] => 1.0
   [xmlVersion] => 1.0
   [strictErrorChecking] => 1
   [documentURI] =>
   [config] =>
   [formatOutput] =>
   [validateOnParse] =>
   [resolveExternals] =>
   [preserveWhiteSpace] => 1
   [recover] =>
   [substituteEntities] =>
   [nodeName] => #document
   [nodeValue] =>
   [nodeType] => 9
   [parentNode] =>
   [childNodes] => (object value omitted)
   [firstChild] => (object value omitted)
   [lastChild] => (object value omitted)
   [previousSibling] =>
   [nextSibling] =>
   [attributes] =>
   [ownerDocument] =>
   [namespaceURI] =>
   [prefix] =>
   [localName] =>
   [baseURI] =>
   [textContent] =>
   Title - JavaFX
   Authors:
   - Krishna
   - Rajeev
)