Barre Formation IN2P3
École franco-maghrébine 2003 WebServices 13-17 octobre 2003

Travaux pratiques

Précédent

tp2e

Suivant

Enoncé :

Réaliser un client PHP qui, à partir du document obtenu, présente le fichier XML dans un browser (utilisation de CSS ou XSLT).

Aide :

Correction :

Contenu du fichier tp2e/tp2e.php.

<?php
// Utilisation du client SOAP de PEAR
include("SOAP/Client.php");

$client = new SOAP_Client('http://localhost:8080/axis/services/InfoCours3');

$params = array('nomSalle'=>"Marguerite", 'numeroSemaine'=>23);
// ! on ne parse pas la sortie 
$options = array('use'=>'literal', 'style'=>'document', 'trace'=>1, 'result'=>'no_parse');

$ret = $client->call('infoCours', $params, $options);

if ( PEAR::isError($ret) ) {
    print("Erreur: " . $ret->getMessage() . "<br>\n");
} else {
    // $ret contient le XML de retour
	ereg("<soapenv:Body>(.*)</soapenv:Body>", $ret, $response);
	header("Content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	if ( isSet($_REQUEST['format']) && $_REQUEST['format'] == "xsl" ) {
	    echo "<?xml-stylesheet type=\"text/xsl\" href=\"tp2e.xsl\"?>\n";
	} else {
		echo "<?xml-stylesheet type=\"text/css\" href=\"tp2e.css\"?>\n";
	}
	echo $response[1];
}
?>

Contenu du fichier tp2e/tp2e.css.

body, html {
	background-color: #eaeff0;
}
semaine {
	background-color: #eaeff0;
	width: 90%;
}
salle {
	border: 2px solid #222266;
	font-family: Arial;
	font-size: 20pt;
	text-align: center;
	display: block;
}
nom {
	color: #333366;
	font-weight: bold;
}
numero {
	color: #222266;
}
batiment {
	font-family: Georgia;
	font-style: italic;
	color: #222266;
}
jour {
	font-family: Verdana;
	padding: 10pt;
	display: block;
}
am {
	display: block;
}
pm {
	display: block;
}
intitule {
	font-weight: bold;
}
responsable {
	font-style: italic;
}

Contenu du fichier tp2e/tp2e.xsl.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:planning="http://in2p3.fr/WS/tp1">
	<xsl:output method="html" encoding="utf-8"/>
	<xsl:template match="/">
		<html>
			<head>
				<title>WS - TP2E</title>
			</head>

			<body>
				<xsl:apply-templates select="/planning:semaine"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="planning:salle">
		<p>
            <xsl:value-of select="planning:nom"/><br/>
            <xsl:value-of select="planning:numero"/><br/>
            <xsl:value-of select="planning:batiment"/><br/>
		</p>
	</xsl:template>
	<xsl:template match="planning:jour">
		<b><xsl:value-of select="@planning:nomjour"/> : </b><br/><xsl:apply-templates select="planning:am |
planning:pm"/>
	</xsl:template>
	<xsl:template match="planning:am">
            am
            <xsl:apply-templates select="planning:cours"/>
	</xsl:template>
	<xsl:template match="planning:pm">
            pm
            <xsl:apply-templates select="planning:cours"/>
	</xsl:template>
	<xsl:template match="planning:cours">
            <xsl:value-of select="planning:intitule"/><br/>
            <xsl:value-of select="planning:responsable"/><br/>
	</xsl:template>
</xsl:stylesheet>

École franco-maghrébine 2003 WebServices 13-17 octobre 2003