Nancy-Université M1 Master Informatique 2006-2007 Techniques de réécriture et transformations Transformations XSLT 1 Liste de personnes Dans cet exercice, on vous demande de transformer en HTML le fichier XML suivant: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="ws1.xsl"?> <PERSONNES> <PERSONNE> <NOM>Mark Wilson</NOM> <ADRESSE>911 Somewhere Circle, Canberra, Australia</ADRESSE> <TEL>(++612) 12345</TEL> <FAX>(++612) 12345</FAX> <EMAIL>Mark.Wilson@somewhere.com</EMAIL> </PERSONNE> <PERSONNE> <NOM>Tracey Wilson</NOM> <ADRESSE>121 Zootle Road, Cape Town, South Africa</ADRESSE> <TEL>(++2721) 531 9090</TEL> <FAX>(++2721) 531 9090</FAX> <EMAIL>Tracey.Wilson@somewhere.com</EMAIL> </PERSONNE> <PERSONNE> <NOM>Jodie Foster</NOM> <ADRESSE>30 Animal Road, New York, USA</ADRESSE> <TEL>(++1) 3000 12345</TEL> <FAX>(++1) 3000 12345</FAX> <EMAIL>Jodie.Foster@somewhere.com</EMAIL> </PERSONNE> <PERSONNE> <NOM>Lorrin Maughan</NOM> <ADRESSE>1143 Winners Lane, London, UK</ADRESSE> <TEL>(++94) 17 12345</TEL> <FAX>(++94) 17 12345</FAX> <EMAIL>Lorrin.Maughan@somewhere.com</EMAIL> </PERSONNE> </PERSONNES> Le HTML généré devra ressembler à ceci:
<HTML> <HEAD> <TITLE>Exo1</TITLE> </HEAD> <BODY> <TABLE BORDER="2"> <TR> <TD>Nom</TD> <TD>Adresse</TD> <TD>Tel</TD> <TD>Fax</TD> <TD>Email</TD> </TR> <TR> <TD>Mark Wilson</TD> <TD>911 Somewhere Circle, Canberra, Australia</TD> <TD>(++612) 12345</TD> <TD>(++612) 12345</TD> <TD>Mark.Wilson@somewhere.com</TD> </TR>... <TABLE> </BODY> </HTML> 2
2 Compter les livres Le but de cet exercice est d utiliser la fonction count de XPath pour transformer un document XML en HTML. On considere le fichier livres.xml ci-dessous et vous devez écrire le fichier xsl permettant d afficher le message suivant: Nombre de livre avec la note 4.5 = 2 Nombre de livre avec la note 4 = 2 <livres> <nom link="http://www.amazon.com/exec/obidos/asin/0789722429">xml by Example</nom> <auteur>benoit Marchal</auteur> <prix>24.99</prix> <promotion>17.49</promotion> <note>4.5</note> <editeur>que</editeur> <nom link="http://www.amazon.com/exec/obidos/asin/0789722427">xml by Example 2</nom> <auteur>benoit Marchal</auteur> <prix>24.99</prix> <promotion>17.49</promotion> <note>4.5</note> <editeur>que</editeur> <nom link="http://www.amazon.com/exec/obidos/asin/0735605628">xml</nom> <auteur>william J. Pardi</auteur> <prix>49.99</prix> <promotion>32.99</promotion> <note>4</note> <editeur>microsoft Press</editeur> <nom link="http://www.amazon.com/exec/obidos/asin/0735605631">xml in Action</nom> <auteur>william J. Pardi</auteur> <prix>39.99</prix> <promotion>31.99</promotion> <note>3.5</note> <editeur>microsoft Press</editeur> <nom link="http://www.amazon.com/exec/obidos/asin/0735605629">xml 2</nom> <auteur>william J. Pardi</auteur> <prix>49.99</prix> 3
<promotion>35.99</promotion> <note>4</note> <editeur>microsoft Press</editeur> </livres> On utilisera pour faire cet exercice la fonction count, qui permet de dénombrer les livres qui possèdent une certaine note (ici, 4.5 et 4). Question: dans votre fichier XSL, modifier ensuite le code en vous plaçant dans le contexte de l élément <livres>. 4
3 Articles sur les pingouins Voici un fichier XML. Nous allons utiliser xsl:template pour définir des règles de transformation d un noeud indépendamment d un contexte donné. <article> <date>28/06/2000 12:30</date> <title>rescued penguins swim home</title> <para><place>cape Town</place> Some 150 penguins unaffected by the oil spill began their long swim from Port Elizabeth in the Eastern Cape back to their breeding habitat at Robben Island near Cape Town on Wednesday. </para> <para>the penguins, who have all been tagged, were transported in a truck hired by the <company>south African National Conservation of Coastal Birds (Sanccob)</company> to Port Elizabeth on Tuesday night. </para> <para>its not known how many more birds will be released from Port Elizabeth after receiving treatment. </para> <para>more than <link ref="www.newsrus.com/oilspill.html">400 tons of fuel oil escaped from the bulk ore carrier Treasure</link> before divers were able to seal the holds.</para> <para>the ship was carrying 130 000 tons of iron ore and 1 300 tons of fuel oil when she sank off the Cape West coast last Friday. </para> <para>a spokesperson for <company>sanccob</company>, Christina Pretorius said the centre had a capacity to treat 1 000 penguins but presently there were in excess of 4 500 birds being rehabilitated and more would be brought to the centre on Wednesday. </para> <source>john Rolfe</source> </article> Question: écrire le fichier XSL qui permet d obtenir le fichier HTML suivant? 28/06/2000 12:30 Rescued penguins swim home Cape Town - Some 150 penguins unaffected by the oil spill began their long swim from Port Elizabeth in the Eastern Cape back to their breeding habitat at Robben Island near Cape Town on Wednesday. The penguins, who have all been tagged, were transported in a truck hired by the South African National Conservation of Coastal Birds (Sanccob) to Port Elizabeth on Tuesday night. Its not known how many more birds will be released from Port Elizabeth after receiving treatment. More than 400 tons of fuel oil escaped from the bulk ore carrier Treasure before divers were able to seal the holds. 5
Essayez chaque modification une par une. Une fois que vous avez testé la modification, revenez à l original avant de tester les modifications suivantes. 1. supprimez le template correspondant à link et expliquez ce qui se passe. 2. supprimez le contenu du template correspondant à place et expliquez ce qui se passe. 3. après l instruction <xsl:template match= / >, modifiez le template <xsl:apply-templates/> afin qu il ne repère que les éléments para et source. 6
4 Toujours les pingouins Pour cet exercice, on utilise l instruction xsl:value-of. Cette instruction n a d effet que sur le noeud dans le contexte. On utilise le fichier pingouins.xml. Il faut remplacer dans le fichier XSL l instruction xsl:applytemplates pour l élément para par une instruction xsl:value-of: <xsl:template match="para"> <p> <xsl:apply-templates/></p> </xsl:template> On utilise l instruction suivante: <xsl:value-of select="."/> Question: quel est le résultat dans ce cas? Proposez une solution alternative. 7
5 Final On utilise le fichier XML des livres: <livres> <nom link="http://www.amazon.com/exec/obidos/asin/0789722429/vbxml">xml by Example</nom> <auteur>benoit Marchal</auteur> <prix>24.99</prix> <promotion>17.49</promotion> <note>4.5</note> <editeur>que</editeur> </livres> Objectif: Ecrivez le fichier XSL qui permet d obtenir le fichier HTML suivant: On envisagera deux méthodes, une procédurale, l autre étant basée sur des règles. 8