Cette page fournit des exemples d'utilisation de document XHTML avec différents langages et techniques.
La page XHTML d'origine est tout d'abord traitée par l'intermédiaire des fonctions DOM XML du langage PHP afin d'afficher une page au format HTML présentée à l'aide de règles de style CSS (Cascading Style Sheets).
Ensuite, deux autres exemples montrent que des documents XHTML peuvent être transformés en d'autres documents XHTML par l'intermédiaire des feuilles de style de transformation (XSLT).
Exemple [voir]<!-- Fichier d'origine : texte.xhtml --> <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="FR"> <head> <title>Auteur : Titre</title> </head> <body> <h1 id="titre"> ... </h1> <h2 id="soustitre1"> ... </h2> <p id="para1"> ... </p> <h2 id="soustitre2"> ... </h2> <p id="para2"> ... </p> <h2 id="soustitre3"> ... </h2> <p id="para3"> ... </p> <h2 id="soustitre4"> ... </h2> <p id="para4"> ... </p> <h2 id="soustitre5"> ... </h2> <p id="para5"> ... </p> <h3 id="auteur"> ... </h3> </body> </html> <!-- Traitement PHP : traitement.php --> <html> <head> <style type="text/css"> p {font-size: 11pt; font-family: Verdana; text-align: justify;} #titre, #soustitre1, #soustitre2, #soustitre3, #soustitre4, #soustitre5 { font-size:14pt; font-weight: bold; font-family: Arial Black; text-align: left; } #titre { font-size:24pt; text-align:center; border: 6px double #000000; margin-bottom: 24px; } #para1:first-letter, #para2:first-letter, #para3:first-letter, #para4:first-letter, #para5:first-letter { font-size: 2.2em; font-weight: bold; float: left; margin-right: 2px; } #soustitre1 {color: #0000FF;} #soustitre2 {color: #660000;} #soustitre3 {color: #FF6600;} #soustitre4 {color: #663300;} #soustitre5 {color: #336600;} #soustitre1 + div {color: #0000FF;} #soustitre2 + div {color: #660000;} #soustitre3 + div {color: #FF6600;} #soustitre4 + div {color: #663300;} #soustitre5 + div {color: #336600;} </style> </head> </body> <?php // Fichier : traitement.php $tab_elements = array("h1","h2","h3","p"); $xml_doc = domxml_open_file("texte.xhtml") or die("Impossible d'ouvrir le fichier XML !"); $element_racine = $xml_doc->document_element(); $noeuds_enfants = $element_racine->child_nodes(); $corps = $element_racine->get_elements_by_tagname("body"); $entete = $element_racine->get_elements_by_tagname("head"); foreach($tab_elements as $element){ $$element = $corps[0]->get_elements_by_tagname($element); } $titre = $h1[0]->get_content(); $i = 0; foreach($h2 as $soustitre){ $titres[$i][0] = $soustitre->get_content(); $titres[$i++][1] = $soustitre->get_attribute("id"); } $i = 0; foreach($p as $paragraphe){ $paragraphes[$i][0] = $paragraphe->get_content(); $paragraphes[$i++][1] = $paragraphe->get_attribute("id"); } $auteur = $h3[0]->get_content(); echo '<div id="titre">' . $titre . '</div>'; for($i = 0; $i < sizeof($titres); $i++){ echo '<div id="' . $titres[$i][1] . '">' . $titres[$i][0] . '</div>'; echo '<div id="' . $paragraphes[$i][1] . '">' . $paragraphes[$i][0] . '</div>'; } echo '<div id="auteur">' . $auteur . '</div>'; ?> </body> </html> |
<!-- Fichier de transformation ASP : traitement.asp --> <% Dim Objet_Document Dim Objet_Style Dim Objet_Template Dim Objet_Processor Set Objet_Document = Server.CreateObject("MSXML2.DOMDocument") Set Objet_Style = Server.CreateObject("MSXML2.FreeThreadedDOMDocument") Set Objet_Template = Server.CreateObject("MSXML2.XSLTemplate") Objet_Document.async = False Objet_Document.Load Server.mapPath("texte.xhtml") Objet_Style.async = False Objet_Style.Load Server.mapPath("transformation.xsl") Set Objet_Template.stylesheet = Objet_Style Set Objet_Processor = Objet_Template.createProcessor() Objet_Processor.input = Objet_Document Objet_Processor.Transform Response.Write Objet_Processor.output Set Objet_Document = Nothing Set Objet_Style = Nothing Set Objet_Template = Nothing Set Objet_Processor = Nothing %> <!-- Feuille de style : transformation.xsl --> <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" encoding="ISO-8859-1" doctype-public= "-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/dtD/xhtml1-strict.dtd"/> <xsl:template match="xhtml:html"> <html> <xsl:apply-templates select="xhtml:body"/> </html> </xsl:template> <xsl:template match="xhtml:body"> <head> <title> <xsl:value-of select="xhtml:h3"/> - <xsl:value-of select="xhtml:h1"/> </title> </head> <body> <table> <caption> <xsl:value-of select="xhtml:h1"/> </caption> <xsl:apply-templates select="xhtml:h2"/> <tr> <th> <xsl:value-of select="xhtml:h3"/> </th> </tr> </table> </body> </xsl:template> <xsl:template match="xhtml:h2"> <tr> <th> <xsl:value-of select="."/> </th> </tr> <tr> <td> <xsl:value-of select="following-sibling::xhtml:p"/> </td> </tr> </xsl:template> </xsl:stylesheet> <!-- Résultat produit --> <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/dtD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="FR" lang="French [fr]"> <head> <title>Livre de Auteur</title> </head> <body> <table border="0" summary="Livre"> <tr> <th colspan="2">Titre du livre</th> </tr> <tr> <td>Sous Titre 1</td> <td>Le texte du chapitre 1</td> </tr> <tr> <td>Sous Titre 2</td> <td>Le texte du chapitre 2</td> </tr> <tr> <td>Sous Titre 3</td> <td>Le texte du chapitre 3</td> </tr> <tr> <td>Sous Titre 4</td> <td>Le texte du chapitre 4</td> </tr> <tr> <td>Sous Titre 5</td> <td>Le texte du chapitre 5</td> </tr> <tr> <td colspan="2">Auteur</td> </tr> </table> </body> </html> |
<!-- Fichier de transformation PHP : traitement.php --> <? $fxml = "texte.xhtml"; $fxsl = "fichierxslt.xsl"; $id_xml = fopen($fxml, "rb"); $id_xsl = fopen($fxsl, "rb"); $xml = fread($id_xml, filesize ($fxml)); $xsl = fread($id_xsl, filesize ($fxsl)); echo $xml; $arg = array('/_xml' => $xml, '/_xsl' => $xsl); $analyseur_xslt = xslt_create(); $resultat = xslt_process($analyseur_xslt, 'arg:/_xml', 'arg:/_xsl', NULL, $arg); if ($resultat != false){ print($resultat); } else{ echo "<h3><u>Une erreur est suvenue :</u></h3> " . "<h4>Code :</h4> " . xslt_errno($analyseur_xslt) . "<h4>Message :</h4> " . xslt_error($analyseur_xslt); } xslt_free($analyseur_xslt); ?> <!-- Feuille de style : fichierxslt.xsl --> <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/dtD/xhtml1-strict.dtd"/> <xsl:template match="xhtml:html"> <html> <xsl:apply-templates select="xhtml:body"/> </html> </xsl:template> <xsl:template match="xhtml:body"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="FR"> <head> <title> <xsl:value-of select="xhtml:h3"/> - <xsl:value-of select="xhtml:h1"/> </title> </head> <body> <h1> <xsl:value-of select="xhtml:h1"/> </h1> <dd> <xsl:for-each select="xhtml:h2"> <dl> <xsl:value-of select="."/> </dl> <dt> <xsl:value-of select="following-sibling::xhtml:p"/> </dt> </xsl:for-each> </dd> <h3> <xsl:value-of select="xhtml:h3"/> </h3> </body> </html> </xsl:template> </xsl:stylesheet> <!-- Résultat produit --> <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="FR"> <head> <title>Auteur : Titre du livre</title> </head> <body> <h1>Titre du livre</h1> <dd> <dl>Sous Titre 1</dl> <dt>Le texte du chapitre 1</dt> <dl>Sous Titre 2</dl> <dt>Le texte du chapitre 2</dt> <dl>Sous Titre 3</dl> <dt>Le texte du chapitre 3</dt> <dl>Sous Titre 4</dl> <dt>Le texte du chapitre 4</dt> <dl>Sous Titre 5</dl> <dt>Le texte du chapitre 5</dt> </dd> <h3>Auteur</h3> </body> </html> |