PREVIOUS UP   Technologies Internet et Education, © TECFA
  6. XSLT avec DOM

6. XSLT avec DOM

6.1 Usage simple

Exemple 6-1: Lire un xml et un XSLT et renvoyer un document

$xml_file = 'programme.xml';
$xsl_file = 'programme.xsl';
 
// load the xml file (and test first if it exists)
$dom_object = new DomDocument();
if (!file_exists($xml_file)) exit('Failed to open $xml_file');
$dom_object->load($xml_file);
 
// create dom object for the XSL stylesheet and configure the transformer
$xsl_obj = new DomDocument();
if (!file_exists($xsl_file)) exit('Failed to open $xsl_file');
$xsl_obj->load($xsl_file);
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl_obj);
 // attach the xsl rules
 
$html_fragment = $proc->transformToXML
($dom_object);
print ($html_fragment);

6.2 Inclure des fonctions PHP dans XSLT

Pour que cela marche il faut enregister les fonctions php dans xslt:

$proc = new XSLTProcessor;
$proc->registerPHPFunctions();

Fonctions PHP dans XSLT:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:php="http://php.net/xsl"
 xmlns:xlink="http://www.w3.org/1999/xlink">

php:function('nom_de_la fonction', arg, arg, ...)

Extrait d'un fichier XSL

<xsl:variable name="pos_y" select="$ori_y + round(php:function('sin', $item_angle) * $radius)"/>T

PREVIOUS UP -- TIE