La présentation est en train de télécharger. S'il vous plaît, attendez

La présentation est en train de télécharger. S'il vous plaît, attendez

Chap IX - XSL Transformations

Présentations similaires


Présentation au sujet: "Chap IX - XSL Transformations"— Transcription de la présentation:

1 Chap IX - XSL Transformations
16/04/2017 révision Avril 2014: XPath 1 et 2.0 Muenchan/Doublons Mulipasses EXSL XSLT 2.0 bugs/limit Webkit PFZ state of the art XSLT fonctionnement normalize-space Chap IX - XSL Transformations Production de Documents XSL-FO Processeurs XSLT Erreurs en XSLT Structure d'un XML How XSL works Grouping en 1.0 Exsl Multipasses en 1.0 XSLT 2.0 XPath 2.0 xsl:output xsl;for-each xsl;if test xsl;call-template xsl;variable xsl;param xsl;key xsl:text xsl:attribute xsl:copy xsl:number xsl:processing-instruction xsl:sort xsl:import xsl:strip-space fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg Objectif pédagogique Introduction aux documents hypertextes: langages de définition, manipulation et édition 16/04/2017 Transparent - 1 Langages & Documents ESSI - 2 – Paul Franchi

2 16/04/2017 Sommaire du Cours Chap I - Documents: Historique, Modèles, Standards et Références Chap II - HTML: voir le cours CIP1-CMD & HTML5 Chap III - XML: "eXtensible Markup Language", les Bases Chap IV - DTD: "Document Type Definition" Chap V - CSS: Feuilles de Styles en Cascades& CSS3 Chap VI - DOM, DHTML: "Domain Object Model "& Programmation Dynamique en HTML & SPRY & JQuery Chap VII - XSD: Schémas XML Chap VIII - XML "Advanced" Processeurs et Dialectes: XPath, Xlink Chap IX - XSLT: Transformations XML Chap X - RDF: "Resource Description Framework" Chap IX - AJAX: "Asynchronous JavaScript And XML" Savoir-Faire: Know Hows HTML XHTML CSS JS+DOM MATHML XPATH XLINK XSLT Etat de l'art: HTML5 CSS3 SPRY SVG RDF AJAX XUL 16/04/2017 Transparent 2 Langages & Documents ESSI - 2 – Paul Franchi 2

3 Some Références Web XSLT ASSIST (SI4 - Polytech)
16/04/2017 Some Références Web XSLT ASSIST (SI4 - Polytech) W3C, Mozilla Development Center W3C tutorial : XSL tutoriel developpez.com/cours/langage/xslt/ Edutech XSL tutoriel, edutechwiki Webucator Tutorial Web Developer's Bookmarks Biblio: XSLT(pdf) XSLT xsltfondamental.pdf many issues at EPU's lib 16/04/2017 Transparent 3 Langages & Documents ESSI - 2 – Paul Franchi

4 Documentation et Manuels
Environnements & Docs MDN MSDN XSLT libxslt EXSLT Saxon Editix Tutoriaux du Web W3C : Lenz consulting Edutech XSL tutoriel, edutechwiki Zvon tutorial StackOverFlow 16/04/2017 Transparent 4 ESSI - 2 – Paul Franchi

5 Références Web 16/04/2017 Transparent 5 5
ESSI - 2 – Paul Franchi 5

6 Lexique API – Application Programming Interface
16/04/2017 Lexique API – Application Programming Interface ANSI – American National Standards Institute ASCII – American Standard Code for Information Interchange (128 car.) AJAX – Asynchronous JavaScript & XML CSS – Cascading Style Sheets DHTML – Dynamic HTML DOM – Document Object Model DTD – Document Type Definition HTML – HyperText Markup Language HTTP – HyperText Transfer Protocol ISO - International Standards Organization Mozilla - Fondation (global community for free and open Internet software)) MVC - Model View Controler RDF - Resource Description Framework REST - Representational State Transfer RIA - Rich Internet Application RSS - Really Simple Syndication SGML - Standard Generalized Markup Language SOAP - Simple Access Object Protocol SPRY - Extensions JS pour HTML - Adobe Lab. SVG - Scalable Vector Graphics UNICODE – World wide Code (16 bits) UTF-8 – Unicode version 8 bits URL / URI – Uniform Resource Locator / Identifier XML – eXtensible Markup Language XBL – XML Binding Language (Mozilla) XForms – XML Forms XHTML – HTML 4 en XML XLink – XML Linking Language XPath – XML Path Language XPointer – XML Pointer Language XSL - eXtensible Stylesheet Language XSLT – XSL Transformations XSL-FO – XSL Formatting Objects XSD – XML Schémas Définition Language XUL – XML User Language (Mozilla) W3C – World Wide Web Consortium Web2.0 – Web dit "sémantique" WHATWG - Web Hypertext Application Technology Working Group WSDL - Web Service Description Language 16/04/2017 Transparent 6 Langages & Documents ESSI - 2 – Paul Franchi

7 Chap IX - XSL Transformations
16/04/2017 Chap IX - XSL Transformations Production de Documents XSL-FO Processeurs XSLT Erreurs en XSLT Structure d'un XML How XSL works Grouping en 1.0 Exsl Multipasses en 1.0 XSLT 2.0 XPath 2.0 xsl:output xsl;for-each xsl;if test xsl;call-template xsl;variable xsl;param xsl;key xsl:text xsl:attribute xsl:copy xsl:number xsl:processing-instruction xsl:sort xsl:import xsl:strip-space fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg Objectif pédagogique Introduction aux documents hypertextes: langages de définition, manipulation et édition 16/04/2017 Transparent - 7 Langages & Documents ESSI - 2 – Paul Franchi

8 XSLT – Xml Transformations (W3C 1999)
16/04/2017 XSLT – Xml Transformations (W3C 1999) Un langage de haut niveau pour spécifier des transformations de documents Xml par remplacement de sous-arbres ("tree templates") Affichage, présentation, etc vers HTML, XHTML, PDF, etc. Echange de données au format XML Autres besoins: requêtes, contrôles, etc. Doc Source XML Proc.XSLT XPath DOM XSL-FO Parseur XML Doc Rés. Code Html Xml Source XSLT 16/04/2017 Transparent 8 Langages & Documents ESSI - 2 – Paul Franchi

9 XSL un XML de Transformation
16/04/2017 XSL un XML de Transformation XSL version & XPath 1.0 sous FF sous Chrome; Safari, avec des restrictions sous IE ? sous xsltproc() avec EXSLT XSL version & XPath 2.0 sous Saxon, Editix, Altova sous Oracle, Xalan XSL version 3.0 16/04/2017 Transparent 9 Langages & Documents ESSI - 2 – Paul Franchi

10 XSLT - une transformation simple
16/04/2017 XSLT - une transformation simple Le source XML en entrée Le résultat recherché (en XHTML) <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="message.xsl"?> <doc> <choix>ROUGE </choix> <choix> VERT </choix> <choix> BLEU </choix> </doc> <html> <head> <title>Palette de Couleurs</title> </head> <body> <h1> Palette </h1> <h3> Couleurs possibles </h3> <p> ROUGE </p> <p> VERT </p> <p> BLEU </p> </body> </html> 16/04/2017 Transparent 10 Langages & Documents ESSI - 2 – Paul Franchi

11 XSLT – une transformation (trop) simple
16/04/2017 XSLT – une transformation (trop) simple Le source XSL <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <head> <title> Palette de Couleurs</title> </head> <body> <h1> Palette </h1> <h3> Couleurs possibles </h3> <xsl:apply-templates select="choix"/> </body> </html> </xsl:template> <xsl:template match="choix"> <p><xsl:value-of select="."/></p> </xsl:stylesheet> namespaces avec prefix XPath root "/" # XML root Xpath => Node set Xpath => Pattern current XPath node 16/04/2017 Transparent 11 Langages & Documents ESSI - 2 – Paul Franchi

12 XSLT – Le source XML (W3C )
16/04/2017 XSLT – Le source XML (W3C ) CartePtitDej.xml <?xml version="1.0" encoding="ISO "?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>two of our famous Belgian Waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description>light Belgian waffles covered with strawberries and whipped cream</description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> with fresh berries and whipped cream</description> <food> <name>French Toast</name> <price>$4.50</price> <description>thick slices made from our homemade sourdough bread</description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description>two eggs, bacon or sausage, toast, and our popular hash browns</description> <calories>950</calories> </food> </breakfast_menu> xml 16/04/2017 Transparent 12 Langages & Documents ESSI - 2 – Paul Franchi

13 XSLT – Le source XSL (W3C, trop simple )
16/04/2017 XSLT – Le source XSL (W3C, trop simple ) CartePtitDej.xsl <html xsl:version="1.0"> <body style="font-family:Arial,sans-serif; font-size:12pt; background-color:#EEEEEE"> <xsl:for-each select="breakfast_menu/food"> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold;color:white"> <xsl:value-of select="name"/> </span> <xsl:value-of select="price"/> </div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <xsl:value-of select="description"/> <span style="font-style:italic"> ( <xsl:value-of select="calories"/> calories per serving) </xsl:for-each> </body> </html> Attention à la "casse" des balises XML et des "select xsl xsl 16/04/2017 Transparent 13 Langages & Documents ESSI - 2 – Paul Franchi

14 Tree Structure of an XML Document
16/04/2017 Tree Structure of an XML Document from XML in a nutshell 7 types of Nodes The root node (XML # XPath) Element nodes Text nodes Attribute nodes Comment nodes Processing instruction nodes Namespace nodes - CDATA, DOCTYPE, are NOT in the tree - attributes are NOT children - Namespaces are not selectable 16/04/2017 Transparent 14 Langages & Documents ESSI - 2 – Paul Franchi

15 XML Tree & XSLT noeuds texte noeuds vides, blancs, RC, LF....
16/04/2017 XML Tree & XSLT noeuds texte noeuds vides, blancs, RC, LF.... <xsl:strip-space elements="..." /> from W3C <bookstore> TTTT   <book category="COOKING"> TTTT     <title lang="en">Everyday Italian</title>     <author>Giada De Laurentiis</author>     <year>2005</year> TTTTT     <price>30.00</price>   </book>   <book category="CHILDREN">     <title lang="en">Harry Potter</title>     <author>J K. Rowling</author>     <year>2005</year>     <price>29.99</price>   </book> Noeuds texte espaces, LF, RC 16/04/2017 Transparent 15 Langages & Documents ESSI - 2 – Paul Franchi

16 XML Tree & XSLT noeuds texte noeuds vides, blancs, RC, LF....
16/04/2017 XML Tree & XSLT noeuds texte noeuds vides, blancs, RC, LF.... <xsl:strip-space elements="..." /> text: text: text: text: text: text: 16/04/2017 Transparent 16 Langages & Documents ESSI - 2 – Paul Franchi

17 XSL – Principe de fonctionnement
16/04/2017 XSL – Principe de fonctionnement Transformation: DOM (XML) vers output (HTML, XML,Texte) Process: itération sur une liste de noeuds ("current node list") le noeud traité est le noeud courant ("current node") itération par apply-templates , for-each , call-template Matching: une expression XPath définit un ensemble de noeuds ("node set") une sélection ("best match") de pattern pour appliquer les règles de template Templates: user defines rules built-in template rules pattern mode précédence & priority from Evan Lentz version détaillée: How XSL works 16/04/2017 Transparent 17 Langages & Documents ESSI - 2 – Paul Franchi

18 XSL – Règles par défaut pour <xsl:apply-templates>
16/04/2017 XSL – Règles par défaut pour <xsl:apply-templates> R1: En l'absence de règle applicable à un élément, la recherche ("matching") s'applique récursivement au source (dfs: nœuds fils,…) jusqu'aux éléments terminaux ("feuilles" du source XML) R2: En l'absence de règle applicable, sur un nœud terminal de type texte, le texte (#PCDATA) est recopié dans le nœud courant de l'arbre généré R3: Les nœuds de type "processing-instruction" et "commentaire" sont ignorés. Remarque: les nœuds "indentation" ou "espaces" du document XML source sont examinés suivant le mode de "matching" ("select", "(strip | preserve)-space") 16/04/2017 Transparent 18 Langages & Documents ESSI - 2 – Paul Franchi

19 XSL – Built-in template rules
16/04/2017 XSL – Built-in template rules root <xsl:template match="/ | *"> <xsl:apply-templates/> </xsl:template> mode <xsl:template match="/ | *" mode="mode-name"> <xsl:apply-templates mode="mode-name"/> </xsl:template> text & attribute <xsl:template match="text() <xsl:value-of select="."/> processing-instruction & comment <xsl:template match="processing-instruction() | comment() "/> from Evan Lentz version détaillée: How XSL works 16/04/2017 Transparent 19 Langages & Documents ESSI - 2 – Paul Franchi

20 XSL – Template "best" match
16/04/2017 XSL – Template "best" match A template rule can explicitly specify its priority using the optional priority attribute on the xsl:template element. The XSLT processor eliminates rules with lower import precedence. Among the remaining template rules, the XSLT processor eliminates the rules with lower priority. It is an error if there is more than one template rule left. Most processors will at least give a warning if this happens. It is also possible to invoke an imported template rule that has been overridden (“xsl:apply-imports”). from Evan Lentz version détaillée: How XSL works 16/04/2017 Transparent 20 Langages & Documents ESSI - 2 – Paul Franchi

21 XSL – Template Précédence
16/04/2017 XSL – Template Précédence from Evan Lentz Default priority Format of location path pattern Examples -.5 Name test wildcard (any name), or node type test (regardless of name) node() text() comment() processing-instruction() -.25 Namespace-qualified wildcard (regardless of local name) Name test for a particular name, or processing-instruction(Literal) processing-instruction('foo') .5 Any other location path pattern. In other words, any one that includes any of these operators: /, //, or [] / /foo foo/bar foo[2] /foo[bar='bat'] version détaillée: How XSL works 16/04/2017 Transparent 21 Langages & Documents ESSI - 2 – Paul Franchi

22 XSL – Structure d'un source
16/04/2017 XSL – Structure d'un source <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <!-- optional top-level elements, such as: --> <xsl:import href="..."/> <xsl:param name="..."/> <xsl:variable name="..."/> <!-- set of template rules: --> <xsl:template match=" ... "> ... <xsl:value-of select=" ..." /> <xsl:apply-templates select=" ..." /> <xsl:foreach select=" ..."> ... </xsl:foreach> <xsl:call-template name=" ..." /> </xsl:template> </xsl:stylesheet> current node list current node position() last() / current list node variables: portée select: XPath exp => Node set match: pattern templates: mode attribute conflict resolution order precedence import precedence priority attribute 16/04/2017 Transparent 22 Langages & Documents ESSI - 2 – Paul Franchi

23 XSL 1.0 - eXtensible Stylesheet Language.
16/04/2017 XSL eXtensible Stylesheet Language. xsl:output xsl;for-each xsl;if test xsl;call-template xsl;variable xsl;param xsl;key xsl:text xsl:attribute xsl:copy xsl:number xsl:processing-instruction xsl:sort xsl:import xsl:strip-space Eléments Racine (2) <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:transform version="1.0" xmlns:xsl=" Eléments "top-level" (12) <xsl:include> <xsl:import> <xsl:output> <xsl:template> <xsl:attribute-set> <xsl:param> <xsl:variable> <xsl:preserve-space> <xsl:strip-space> <xsl:key> <xsl:namespace-alias> <xsl:decimal-format> Eléments "instructions" (22) <xsl:fallback> <xsl:comment> <xsl:processing-instruction> <xsl:element> <xsl:apply-templates> <xsl:value-of> <xsl:attribute> <xsl:number> <xsl:variable> <xsl:text> <xsl:call-template> <xsl:for-each> <xsl:choose> <xsl:if> <xsl:otherwise> <xsl:when> <xsl:with-param> <xsl:sort> <xsl:copy> <xsl:copy-of> <xsl:message> In/out template In template fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg 16/04/2017 Transparent 23 Langages & Documents ESSI - 2 – Paul Franchi

24 XSL – Programmation par Règles
16/04/2017 XSL – Programmation par Règles xsl:output xsl;for-each xsl;if test xsl;call-template xsl;variable xsl;param xsl;key xsl:text xsl:attribute xsl:copy xsl:number xsl:processing-instruction xsl:sort xsl:import xsl:strip-space racine < xsl:stylesheet … > … </xsl:stylesheet> Inclusion & Importation de règles < xsl:import href="Package.xsl" /> < xsl:include href="Package.xsl" /> application des règles à un noeud < xsl:apply-templates select="A/B" /> < xsl:apply-import /> sélection d'un nœud de contexte < xsl:template match= "catalog/cd" > </ xsl:template > extraction de valeurs <xsl:value-of-select="." /> <xsl:value-of-select="catalog/cd/artist" /> commentaire <xsl:comment> … </xsl:comment> fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg Expressions XPath 16/04/2017 Transparent 24 Langages & Documents ESSI - 2 – Paul Franchi

25 XSL – Programmation Fonctionnelle (Impérative)
16/04/2017 XSL – Programmation Fonctionnelle (Impérative) xsl:output xsl;for-each xsl;if test xsl;call-template xsl;variable xsl;param xsl;key xsl:text xsl:attribute xsl:copy xsl:number xsl:processing-instruction xsl:sort xsl:import xsl:strip-space définition d'un template < xsl:template name= "List"> … </ xsl:template> application par appel < xsl:call-templates name="List" /> affectation de variables <xsl:variable name='nom_var' select="val" /> <xsl:variable name='nom_var' select="pathExp" /> <xsl:variable name='epreuves' select="//Epreuve" /> utilisation de variables <xsl:value-of select="sum( $epreuve_s/Note) div $coeff " /> test < xsl:if test ="ANNEE > 2000 " > …< /xsl:if > < xsl:if test ="XPath_Exp" > <!-- si Element existe --> … < /xsl:if > < xsl:choose> …<xsl:when test =" " >…< /xsl:when >… </xsl:choose > itération <xsl:for-each> …</xsl:for-each > fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg NO real loops real variables 16/04/2017 Transparent 25 Langages & Documents ESSI - 2 – Paul Franchi

26 Attention à la "casse" des balises XML et des "select"
16/04/2017 XSLT - le XML catalogCD.xml Appliquer un style XSL <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="catalogCD.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> . .. </catalog> Attention à la "casse" des balises XML et des "select" 16/04/2017 Transparent 26 Langages & Documents ESSI - 2 – Paul Franchi

27 Attention à la "casse" des balises XML et des "select"
16/04/2017 XSLT – (v1: incomplet) <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </table> </body> </html> </xsl:template> </xsl:stylesheet> Expresssions XPath le premier seulement du Node set Attention à la "casse" des balises XML et des "select" xsl 16/04/2017 Transparent 27 Langages & Documents ESSI - 2 – Paul Franchi

28 XSLT – (v2: impérative, avec test)
16/04/2017 XSLT – (v2: impérative, avec test) <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> xsl 16/04/2017 Transparent 28 Langages & Documents ESSI - 2 – Paul Franchi

29 XSLT – (v3: impérative, avec tri)
16/04/2017 XSLT – (v3: impérative, avec tri) <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> sort uniquement dans for-each apply-templates xsl 16/04/2017 Transparent 29 Langages & Documents ESSI - 2 – Paul Franchi

30 xsl XSLT – (v4, avec choix) choose when
16/04/2017 XSLT – (v4, avec choix) <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td> <xsl:value-of select="title"/> </td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/> </td> </xsl:when> <xsl:otherwise> <td> <xsl:value-of select="artist"/> </td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> choose when xsl 16/04/2017 Transparent 30 Langages & Documents ESSI - 2 – Paul Franchi

31 XSLT – (v5 & v6: déclarative)
16/04/2017 XSLT – (v5 & v6: déclarative) <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <!-- essayer sans les 2 règles ci-dessous --> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span><br /> </xsl:stylesheet> xsl xsl 16/04/2017 Transparent 31 Langages & Documents ESSI - 2 – Paul Franchi

32 Attention à la "casse" des balises XML et des "select
16/04/2017 XSLT – Le source XML catalogCD.xml Appliquer un style XSL <?xml version="1.0" encoding=" UTF-8 "?> <?xml-stylesheet type="text/xsl" href="catalogCD.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> . .. </catalog> Attention à la "casse" des balises XML et des "select 16/04/2017 Transparent 32 Langages & Documents ESSI - 2 – Paul Franchi

33 xsl xsl xsl XSLT – XML vers XML
16/04/2017 XSLT – XML vers XML Version 1 : "matching" par défaut total <?xml version="1.0" encoding=" UTF-8 "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" encoding="ISO " /> <xsl:template match="/"> <catalog> <titre> Mon catalogue de CD </titre> <liste> <xsl:apply-templates> </liste> </catalog> </xsl:template> </xsl:stylesheet> <racine> du XML produit ici tous les noeuds XML sont traités donc indetations et espaces xsl voir le source généré xsl comparer avec xsl 16/04/2017 Transparent 33 Langages & Documents ESSI - 2 – Paul Franchi

34 xsl XSLT – XML vers XML <racine> du XML produit
16/04/2017 XSLT – XML vers XML Version 2 : "matching" par défaut avec "select" <?xml version="1.0" encoding=" UTF-8 "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" encoding="ISO " /> <xsl:template match="/"> <catalog> <titre> Mon catalogue de CD </titre> <liste> <xsl:apply-templates select=".//ARTIST" /> </liste> </catalog> </xsl:template> <xsl:template match="ARTIST"> <artiste><xsl:apply-templates /></artiste> </xsl:stylesheet> <racine> du XML produit Ici seuls les noeuds "sélectionnés" sont traités donc ni les indentations, ni les espaces xsl voir le source généré 16/04/2017 Transparent 34 Langages & Documents ESSI - 2 – Paul Franchi

35 XSL – ' . ' vs current() from xmlfr.org
16/04/2017 XSL – ' . ' vs current() "." est un raccourci XPath (Self::* ) qui désigne le noeud courant de l'expression XPath "current()" est une fonction XSLT qui désigne le noeud contexte de l'instruction XSLT from xmlfr.org <xsl:template match="/foo/bar"> <!-- current() désigne un élément "/foo/bar"-->   <xsl:value-of select="/foo[.='bar']" > <!--    ici ^     "." désigne /foo     -->   <xsl:value-of select="/foo/bar2[.='bar']"> <!--              ici ^     "." désigne un élément /foo/bar2   -->   </xsl:template> 16/04/2017 Transparent 35 Langages & Documents ESSI - 2 – Paul Franchi

36 XSL – context nodes from lenzconsulting
16/04/2017 XSL – context nodes from lenzconsulting <xsl:template match="order"> <!-- current node is an "order" element --> <p>Order: <xsl:value-of select="number"/></p> <xsl:for-each select="item"> <!-- current node changes to an "item" element --> <p>Item: <xsl:value-of select="name"/></p> </xsl:for-each> <!-- current node changes back to the "order" element --> <p>Total: <xsl:value-of select="total"/></p> </xsl:template> 16/04/2017 Transparent 36 Langages & Documents ESSI - 2 – Paul Franchi

37 XPath 1.0 & XSLT select="//A" select="A//B" select="A/*/B"
16/04/2017 XPath 1.0 & XSLT select="//A" select="A//B" Parcours TOUT le DOM ou le sous DOM de A select="A/*/B" les petits fils B de A les A qui ont un attribut 'name' les fils de A qui ont un attribut 'name les A qui ont un fils avec un attribut 'name select="*[name() = $NAME]" les éléments dont le nom (balise) est égal à la valeur de $NAME <xsl:element name= "{$NAME}" titre= "{$TITRE}" /> crée un élément de nom $NAME et d'attribut $TITRE ici { } obligatoirs 16/04/2017 Langages & Documents ESSI - 2 – Paul Franchi

38 XSL 1.0 Pfz's Top XPath expression
16/04/2017 select="count(preceding-sibling::*[name() = name(current())] )" la "position" du noeud courant parmi ses fréres de même "tag" = $AN] //COURS[Les_Publics/PUBLIC=$PUB]" dans une ANNEE, tous les COURS ayant un certain PUBLIC Dans 'publics.xml', les <column> 'public' des <table> 'publics' /COURS[./Les_Publics/PUBLIC = $PUB]]" les COURS dans une variable (EXSLT) avec un certain PUBLIC /COURS[./Les_Publics/PUBLIC = $PUB] [generate-id() = des COURS dans une variable (EXSLT) ... avec unicité select="/xs:schema/xs:element = des élements de schémas XSD position()/ current list node XSL 1.0 Pfz's Top XPath expression 16/04/2017 Langages & Documents ESSI - 2 – Paul Franchi

39 Fonctions XSLT, XPath & XQuery
16/04/2017 Fonctions XSLT, XPath & XQuery from w3schools.com Accessor Error and Trace Numeric String AnyURI Boolean Duration/Date/Time QName Node Sequence Context Functions Reference fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg 16/04/2017 Transparent 39 Langages & Documents ESSI - 2 – Paul Franchi

40 XSL – Fonctions from w3schools.com current() document()
16/04/2017 XSL – Fonctions from w3schools.com Name Description current() Returns the current node document() Used to access the nodes in an external XML document element-available() Tests whether the element specified is supported by the XSLT processor format-number() Converts a number into a string function-available() Tests whether the function specified is supported by the XSLT processor generate-id() Returns a string value that uniquely identifies a specified node key() Returns a node-set using the index specified by an <xsl:key> element system-property() Returns the value of the system properties unparsed-entity-uri() Returns the URI of an unparsed entity fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg 16/04/2017 Transparent 40 Langages & Documents ESSI - 2 – Paul Franchi

41 XSL – Variables Variables à affectation unique
16/04/2017 Variables à affectation unique ne peuvent pas être modifiées peuvent être recréées à chaque exécution d'un apply ou d'un for-each Globales portée valide sur tout le document xsl Locales portée limitée au sous arbre XSL de l'élément (et non pas XML) Types de base number string boolean Node Set supporte les expressions XPath Node (tree) Fragment ne supporte pas les expressions XPath en XSL 1.0 supporté avec EXSLT XSL – Variables NO real variables 16/04/2017 Transparent 41 Langages & Documents ESSI - 2 – Paul Franchi

42 XSLT – Variables Globales
16/04/2017 <xsl:stylesheet version="1.0" xmlns:xsl=" <!-- Variables Globales --> <!-- number --> <xsl:variable name="var1" select="number(10.2)"/> <xsl:variable name="var2" >15.2</xsl:variable> <!-- string --> <xsl:variable name="var3" >truc</xsl:variable> <xsl:variable name="var4" select="string('truc')"/> <!-- booleen --> <xsl:variable name="var5" select="$var1 < $var2"/> <!-- Node Set autorise les XPath expressions --> <xsl:variable name="var11" select="/racine"/> <xsl:variable name="var12" select="//valeur"/> <!-- Node Fragment N'autorise PAS les XPath expressions--> <xsl:variable name="var" > <truc>premier truc <chose> la chose </chose></truc> <truc>second truc</truc> </xsl:variable> <!-- voir la suite ...--> XSLT – Variables Globales .xsl 16/04/2017 Transparent 42 Langages & Documents ESSI - 2 – Paul Franchi

43 XSLT – Variables Globales
16/04/2017 <xsl:template match="/"> <racine> <result topic="Variables Globales"> <titre> Integer, String, Boolean </titre> <xsl:value-of select="$var1"/> <xsl:text> &return; </xsl:text> <xsl:value-of select="$var2"/> <xsl:text> &return; </xsl:text> <xsl:value-of select="$var3"/> <xsl:text> &return; </xsl:text> <xsl:value-of select="$var4"/> <xsl:text> &return; </xsl:text> <xsl:value-of select="$var5"/> <xsl:text> &return; </xsl:text> <titre> Node Set </titre> <xsl:value-of select="$var11/valeur[1]"/> <xsl:value-of select="$var12[2]"/> <!-- Node Set autorise les XPath expressions --> <xsl:if test="$var11/valeur[1]=$var12[1]"> <xsl:text>$var11/valeur[1] = $var12[1]</xsl:text> </xsl:if> <xsl:text> &return; </xsl:text> <titre> Node Fragment </titre> <!-- Node Fragment N'autorise PAS les XPath expressions--> <xsl:value-of select="$var"/> <!-- select="$var/truc" impossible --> </result> <!-- voir la suite ...--> XSLT – Variables Globales .xsl 16/04/2017 Transparent 43 Langages & Documents ESSI - 2 – Paul Franchi

44 XSLT – Variables Globales
16/04/2017 <!-- voir le début...--> <result topic="Name Functions"> <titre> fonction name() </titre> ** <xsl:value-of select="name(current())"/> ** ** <xsl:value-of select="name(*)"/> ** ** <xsl:value-of select="name(*/*)" /> ** <titre> fonction local-name() </titre> ** <xsl:value-of select="local-name(*)"/> ** ** <xsl:value-of select="local-name(*/*)" /> ** ** <xsl:value-of select="local-name(//valeur)"/> ** <titre> fonction namespace-uri() </titre> ** <xsl:value-of select="namespace-uri(*)"/> ** ** <xsl:value-of select="namespace-uri(*/*)"/> ** ** <xsl:value-of select="namespace-uri(//valeur)"/> ** </result> </racine> </xsl:template> </xsl:stylesheet> XSLT – Variables Globales .xsl 16/04/2017 Transparent 44 Langages & Documents ESSI - 2 – Paul Franchi

45 XSLT variables locales
16/04/2017 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" version="1.0" indent="yes"/> <xsl:template match="Mois"> <xsl:variable name="mID" <xsl:variable name="mHREF" <!-- {Accolades} dans les valeurs d'attributs supporte les fonctions non valide dans les select ou les match --> <div class="Mois"> <a href="{$mHREF}"> <div id="{$mID}" class="Bouton"> <xsl:value-of </div> </a> </xsl:template> </xsl:stylesheet> XSLT variables locales .xsl 16/04/2017 Transparent 45 Langages & Documents ESSI - 2 – Paul Franchi

46 .xsl XSLT - Attributs supprime ' ' et '\n'
16/04/2017 XSLT - Attributs <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" version="1.0" indent="yes"/> <xsl:variable name="DIR"> <xsl:value-of </xsl:variable> <img> <xsl:attribute name="src"> <xsl:value-of select="normalize-space(node())" /> <xsl:text>.jpg</xsl:text></xsl:attribute> </img> <xsl:element name="input" use-attribute-sets="attrs"/> <xsl:template match="Bib"></xsl:template> <xsl:attribute-set name="attrs"> <xsl:attribute name="onClick"> doIt(2, ' <xsl:value-of select="$DIR"/>/<xsl:value-of </xsl:attribute></xsl:attribute-set> </xsl:stylesheet> supprime ' ' et '\n' attribute AVANT les Enfants .xsl 16/04/2017 Transparent 46 Langages & Documents ESSI - 2 – Paul Franchi

47 XSLT - normalize-space()
16/04/2017 XSLT - normalize-space() normalize-space() permet d'utiliser une valeur texte d'élement sans tenir compte des espaces "blancs" par ex. pour tester une égalité. <img> <xsl:attribute name="src"> <xsl:value-of select="normalize-space(node())" /> <xsl:text>.jpg</xsl:text> </xsl:attribute> </img> <xsl:template > <P> <xsl:value-of select="normalize-space(.)" /> </P> </xsl:template> .... <xsl:when test="normalize-space(.) = 'SI4' "> ..... supprime ' ' et '\n' 16/04/2017 Transparent 47 Langages & Documents ESSI - 2 – Paul Franchi

48 XSLT - strip (preserve) -space
16/04/2017 XSLT - strip (preserve) -space <xsl:strip-space > permet d'élimeiner ou de conserver les "blancs" pour une catégorie d'élements (QName). <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:strip-space elements="country company price year" /> <xsl:preserve-space elements="title artist" /> <xsl:template match="/">   <html>   <body>   <xsl:for-each select="catalog/cd">     <p>     <xsl:value-of select="title" />     <xsl:value-of select=" year " />     ..... supprime ou conserve les "blancs" 16/04/2017 Transparent 48 Langages & Documents ESSI - 2 – Paul Franchi

49 .xsl XSLT Calculs Fact V1 <xsl:template match="/">
16/04/2017 <xsl:template match="/"> <calculs> <xsl:apply-templates select="//fact" /> </calculs> </xsl:template> <xsl:template match="fact"> <!-- Appel avec n et 1 --> <xsl:call-template name="F"> <xsl:with-param name="n" /> <xsl:with-param name="f" select="1" /> </xsl:call-template> <xsl:template name="F"> <!-- Template récursif --> <xsl:param name="n" /> <xsl:param name="f" /> <xsl:choose> <xsl:when test="$n = 1"> <fact res="{$f}" > *<xsl:value-of select="$f" />* </fact> </xsl:when> <xsl:otherwise> <xsl:with-param name="n" select="$n - 1" /> <xsl:with-param name="f" select="$f * $n" /> </xsl:otherwise> </xsl:choose> </xsl:stylesheet> XSLT Calculs Fact V1 .xsl 16/04/2017 Transparent 49 Langages & Documents ESSI - 2 – Paul Franchi

50 .xsl XSLT Calculs Fact V2 <xsl:template match="/">
16/04/2017 <xsl:template match="/"> <calculs> <xsl:apply-templates select="//fact" /> </calculs> </xsl:template> <xsl:template match="fact"> <!-- Appel avec n et 1 --> <!-- la variable permet de valuer un attribut Avant l'appel --> <xsl:variable name="RES" > <xsl:call-template name="F"> <xsl:with-param name="n" select="."/> </xsl:call-template> </xsl:variable> <fact n="{.}" > <xsl:attribute name="res"> <xsl:value-of select="$RES"/> </xsl:attribute> *<xsl:value-of select="$RES"/>* </fact> <!-- voir suivant pour F--> </xsl:stylesheet> XSLT Calculs Fact V2 attribute AVANT les Enfants .xsl 16/04/2017 Transparent 50 Langages & Documents ESSI - 2 – Paul Franchi

51 16/04/2017 XSLT Calculs Fact V2 <!-- voir début avant --> <xsl:template name="F"> <!-- Template récursif --> <xsl:param name="n"/> <xsl:if test="$n = 1">1</xsl:if> <xsl:if test="$n > 1"> <xsl:variable name="r"> <xsl:call-template name="F"> <xsl:with-param name="n" select="$n - 1"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$n * $r"/> </xsl:if> </xsl:template> </xsl:stylesheet> .xsl 16/04/2017 Transparent 51 Langages & Documents ESSI - 2 – Paul Franchi

52 .xsl XSLT Calculs Commande <xsl:template match="/">
16/04/2017 <xsl:template match="/"> <commande cat="?"> <xsl:apply-templates select="commande/produit"/> <p>nb de lignes produit : <xsl:value-of select="count(commande/produit)" /> </p> <p> nb d'exemplaires en tout : <xsl:value-of select="sum(commande/produit/nb)" /> <xsl:call-template name = "total" > <xsl:with-param name="noeuds" select="//produit"> </xsl:with-param> <xsl:with-param name="s_total" select="0"> </xsl:call-template> </commande> </xsl:template> <!-- voir suivant pour produit et total --> </xsl:stylesheet> XSLT Calculs Commande .xsl 16/04/2017 Transparent 52 Langages & Documents ESSI - 2 – Paul Franchi

53 .xsl XSLT Calculs Commande <xsl:template match="produit">
16/04/2017 <!-- voir début avant --> <xsl:template match="produit"> <produit> <xsl:value-of /> -- Qté: <xsl:value-of select="nb" /> -- PU: <xsl:value-of select="prix" />€ </produit> </xsl:template> XSLT Calculs Commande <xsl:template name="total"> <xsl:param name="noeuds" /><xsl:param name="s_total" /> <total> <xsl:choose><xsl:when test="$noeuds[position()>1]"> <xsl:call-template name = "total" > <xsl:with-param name="noeuds" select="$noeuds[position()>1]" /> <xsl:with-param name="s_total" select="$s_total+(number($noeuds[1]/nb)*number($noeuds[1]/prix))"/> </xsl:call-template> </xsl:when> <xsl:otherwise>total de la commande : <xsl:value-of select="$s_total +(number($noeuds[1]/nb)*number($noeuds[1]/prix))" />€ </xsl:otherwise></xsl:choose> </total> </xsl:template> </xsl:stylesheet> .xsl 16/04/2017 Transparent 53 Langages & Documents ESSI - 2 – Paul Franchi

54 XSL Commande avec key et generate-id
16/04/2017 <xsl:key name="R" match="produit" <xsl:template match="/"> <xsl:apply-templates select="commande"/> </xsl:template> <xsl:template match="commande"> <xsl:variable name="Cat" select= "count(//produit[generate-id(.) = )" /> <commande cat="{$Cat}"> <xsl:apply-templates select= "//produit[generate-id(.) = /> </commande> <xsl:template match="produit"> <produit> <xsl:value-of ' : ', ', ', PU: ', /> <xsl:text>&return;</xsl:text> </produit> </xsl:stylesheet> XSL Commande avec key et generate-id .xsl 16/04/2017 Transparent 54 Langages & Documents ESSI - 2 – Paul Franchi

55 XSL – Production de Documents
16/04/2017 XSL – Production de Documents Définition du format de sortie < xsl:output method= " " version=" " encoding=" " indent=" " /> Duplication <xsl:copy> … </xsl:copy> <xsl:copy-of> … </xsl:copy-of> Création de noeuds <xsl:text> … </xsl:text> <xsl:element> … </xsl:element> <xsl:attribute name="att" > …</ xsl:attribute > <xsl:comment> …</xsl:comment> Création de liens <xsl:key name=" " match= "/A/B" <a href="#{generate-id(artist)}"> Intégration multi-documents avec document (') <xsl:value-of select= /> xsl:output xsl;for-each xsl;if test xsl;call-template xsl;variable xsl;param xsl;key xsl:text xsl:attribute xsl:copy xsl:number xsl:processing-instruction xsl:sort xsl:import xsl:strip-space fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg 16/04/2017 Transparent 55 Langages & Documents ESSI - 2 – Paul Franchi

56 .xsl XSLT et Namespace <?xml version="1.0" encoding="UTF-8"?>
16/04/2017 XSLT et Namespace <?xml version="1.0" encoding="UTF-8"?> <racine> <ex xmlns= " > <valeur> °°° </valeur> °°° </ex> </racine> <xsl:stylesheet version="1.0" xmlns:xsl = xmlns:ldoc = " > <!-- Variables Globales --> <!-- Node Set autorise les XPath expressions --> <xsl:variable name="var11" select="/racine"/> <xsl:variable name="var12" select="//ldoc:valeur"/> <xsl:template > °°° </xsl:template> </xsl:stylesheet> .xsl 16/04/2017 Transparent 56 Langages & Documents ESSI - 2 – Paul Franchi

57 from learn-xslt-tutorial.com/
16/04/2017 XSLT et Key from learn-xslt-tutorial.com/ <?xml version="1.0" encoding="UTF-8"?> <beatles> <beatle link=" <name> <firstname>Paul</firstname> <lastname>McCartney</lastname> </name> <role>Singer</role> </beatle> °°° </ beatles > <xsl:stylesheet version="1.0" xmlns:xsl = > <xsl:key name="keyBeatle" match="beatle" use="name/firstname"/> <xsl:param name="FirstName" select="'John'"/> <xsl:template match="/"> <Match> <xsl:value-of <xsl:value-of </Match> </xsl:template> </xsl:stylesheet> .xsl 16/04/2017 Transparent 57 Langages & Documents ESSI - 2 – Paul Franchi

58 from learn-xslt-tutorial.com/
16/04/2017 XSLT et generate-id() <xsl:stylesheet version="1.0" xmlns:xsl = > <xsl:key name="keySong" match="Song" <xsl:template match="/"> <IDs><ID> <xsl:value-of select="Songs/Song[2]"/> <xsl:value-of select="generate-id(Songs/Song[2])"/> </ID> <ID> <xsl:value-of select="Songs/Song[.='Satisfaction']"/> <xsl:value-of select="generate-id(Songs/Song[.='Satisfaction'])"/> </ID></IDs> <ID> <xsl:value-of Stones']"/> <xsl:value-of select="generate-id( Stones'])"/> <xsl:value-of select="key('keySong','Rolling Stones')"/> <xsl:value-of select="generate-id(key('keySong','Rolling Stones'))"/> </ID> </xsl:template> </xsl:stylesheet> .xsl from learn-xslt-tutorial.com/ 16/04/2017 Transparent 58 Langages & Documents ESSI - 2 – Paul Franchi

59 from learn-xslt-tutorial.com/
16/04/2017 XSLT et generate-id() <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="GenerateID.xsl"?> <Songs Source=" <Song Artist="Led Zeppelin"> Stairway To Heaven </Song> Artist="Rolling Stones"> Satisfaction Artist="The Dominoes"> Layla °°° </Songs> from learn-xslt-tutorial.com/ 16/04/2017 Transparent 59 Langages & Documents ESSI - 2 – Paul Franchi

60 XSL – Transformations vers Text, Html, Xml
16/04/2017 XSL – Transformations vers Text, Html, Xml Xml  Html, Html5 <xsl:output method=" html" encoding=" UTF-8 " /> Xml  Xml <xsl:output method="xml" encoding=" UTF-8 " /> Xml  Xhtml, SVG, MathML Xml  XSD, XSL Xml  Text , DTD <xsl:output method="text" encoding="UTF-8" /> Xml  Java, C, SQL, PHP 16/04/2017 Transparent 60 Langages & Documents ESSI - 2 – Paul Franchi

61 XSLT – à partir de HTML ou Texte
16/04/2017 XSLT – à partir de HTML ou Texte sur Html, XMéliser le source fermer les balises non fermées: <img,> <br> , ... substituer les caractères spéciaux   , ... éliminier les inutiles: head, meta, link sur Html, avec un processeur XSLT appliquer directement sur Html, sous un navigateur encapsuler dans un source.xml avec la XSLT sur fichier Texte encapsuler le source avec quelques balises xml (marquage) voir tokenize() en XSL 2.0 16/04/2017 Transparent 61 Langages & Documents ESSI - 2 – Paul Franchi

62 XSLT vers XHTML <xsl:output method = "xml" doctype-system =
16/04/2017 XSLT vers XHTML <xsl:output method = "xml" doctype-system = " doctype-public = "-//W3C/DTD/XHTML1.0Transitional//EN" encoding = " UTF-8 " indent="yes" /> />   <xsl:template match = "thing"> <html xmlns=" °°° </ html > </xsl:template> 16/04/2017 Transparent 62 Langages & Documents ESSI - 2 – Paul Franchi

63 XSLT vers HTML5 <xsl:output method = "html"
16/04/2017 XSLT vers HTML5 <xsl:output method = "html" doctype-system = "about:legacy-compat" encoding = "UTF-8" indent = "yes" /> />   <xsl:template match = "thing"> <html > °°° </ html > </xsl:template> 16/04/2017 Transparent 63 Langages & Documents ESSI - 2 – Paul Franchi

64 XSLT vers SVG from edutechwiki <xsl:output method="xml"
16/04/2017 XSLT vers SVG from edutechwiki <xsl:output method="xml" indent="yes" standalone="no" doctype-public="-//W3C//DTD SVG 1.1//EN" doctype-system= " media-type="image/svg" /> <xsl:template match = "thing"> <svg xmlns = " width = "200" height = "200" > °°° </svg> </xsl:template> 16/04/2017 Transparent 64 Langages & Documents ESSI - 2 – Paul Franchi

65 .xsl XSLT vers SVG from edutechwiki
16/04/2017 XSLT vers SVG <xsl:stylesheet version="1.0" xmlns:xsl= xmlns=" > <xsl:output method="xml" indent="yes" standalone="no" doctype-public="-//W3C//DTD SVG 1.1//EN" doctype-system= " media-type="image/svg" /> <!-- must remove white spaces within the list element, otherwise count will not work --> <xsl:strip-space elements="list"/> <xsl:template match="/"> <svg xmlns=" width="500" height="500" > <!-- à suivre ...--> from edutechwiki .xsl 16/04/2017 Transparent 65 Langages & Documents ESSI - 2 – Paul Franchi

66 .xsl XSLT vers SVG from edutechwiki
16/04/2017 XSLT vers SVG <!-- draw a rectangle over the whole SVG drawing space --> <rect x="1" y="1" width="400" height="500" fill="yellow" stroke="blue"/> <xsl:apply-templates/> </svg> </xsl:template> <xsl:template match="list"> <rect x="30" y="405" width="{30 * count(item)}" height="20" fill="black" stroke="red"/> <xsl:template match="item"> <rect x="{30*position()}" y="{ }" width="30" height="{.}" fill="red" stroke="black"/> </xsl:stylesheet> from edutechwiki .xsl 16/04/2017 Transparent 66 Langages & Documents ESSI - 2 – Paul Franchi

67 processinginstruction
16/04/2017 XSLT – XML vers XML + CSS en associant une feuille CSS <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" encoding="ISO " /> <xsl:template match="/"> <xsl:processing-instruction name="xml-stylesheet"> href="catalogCD.css" type="text/css" </xsl:processing-instruction> <catalog> <titre> Mon catalogue de CD </titre> <liste> <xsl:apply-templates> </liste> </catalog> </xsl:template> </xsl:stylesheet> processinginstruction sous "/" xsl voir le source généré 16/04/2017 Transparent 67 Langages & Documents ESSI - 2 – Paul Franchi

68 XSLT – call template with parameters
16/04/2017 XSLT – call template with parameters <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" .... <xsl:template match="CD" > <xsl:if <xsl:call-template name="faireunlien"> <xsl:with-param name="texte" select="concat(TITLE,' - ', ARTIST)" /> <xsl:with-param name="url" /> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template name="faireunlien"> <xsl:param name="url" /> <xsl:param name="texte" /> <a href="{$url}"> <xsl:value-of select="$texte" /> </a> </xsl:stylesheet> from Webucator xsl voir le source généré 16/04/2017 Transparent 68 Langages & Documents ESSI - 2 – Paul Franchi

69 xsl XSLT – "Double Passe pour ToC" from Webucator
16/04/2017 XSLT – "Double Passe pour ToC" <?xml version="1.0" encoding="UTF-8"?> <xsl:template match="/" °°° <toc> Table of Liens <xsl:apply-templates select="//CD" mode="toc"/> </toc> <xsl:apply-templates /> </xsl:template> <xsl:template match="CD" mode="toc"> <xsl:call-template name="faireunlien"> </xsl:call-template> .... </xsl:stylesheet> from Webucator <xsl:template match="CD"> <node> <xsl:value-of select="TITLE" /> [<xsl:value-of select="ARTIST" />] <xsl:apply-templates select="YEAR | COUNTRY | COMPANY " /> </node> </xsl:template> </xsl:stylesheet> xsl voir le source généré 16/04/2017 Transparent 69 Langages & Documents ESSI - 2 – Paul Franchi

70 xsl XSLT – Numérotation from Webucator voir le source généré
16/04/2017 XSLT – Numérotation <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="text"/> .... <xsl:template match="CATALOG//*"> <xsl:for-each select="ancestor::*"> <xsl:text> </xsl:text> <!-- tabulation --> </xsl:for-each> <xsl:number level="multiple" count="*" from="CATALOG"/> <xsl:number level="multiple" count="*" format="I.A.i.a. "/> <xsl:value-of select="text()"/> <xsl:text> </xsl:text> <!-- retour chariot --> </xsl:template> </xsl:stylesheet> from Webucator xsl voir le source généré 16/04/2017 Transparent 70 Langages & Documents ESSI - 2 – Paul Franchi

71 XSLT – la copie "intégrale" Xml vers Xml
16/04/2017 XSLT – la copie "intégrale" Xml vers Xml <?xml version="1.0" encoding="UTF-8?> <!-- copie XSL "intégrale" de XML vers XML 4 versions à tester --> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" version="1.0" indent="yes"/> <xsl:template match=" node() "> <!-- tous les types de nœuds ou attributs --> <xsl:copy> <xsl:apply-templates | * "/> <!-- les nœuds 'éléments' ou attributs --> </xsl:copy> </xsl:template> </xsl:stylesheet> XPath root "/" # XML root xsl xsl xsl xsl node() (tous types) # * (Element ) voir le source généré 16/04/2017 Transparent 71 Langages & Documents ESSI - 2 – Paul Franchi

72 XSLT – la copie "rebalisée" Xml vers Xml
16/04/2017 XSLT – la copie "rebalisée" Xml vers Xml <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" version="1.0" indent="yes"/> <xsl:template <xsl:copy> <xsl:apply-templates </xsl:copy> </xsl:template> <xsl:template match="comment()" > <COM> <xsl:value-of select="." /> </COM> <xsl:template match="processing-instruction()" > <STYLE> <xsl:value-of select="." /> </STYLE> </xsl:stylesheet> xsl 16/04/2017 Transparent 72 Langages & Documents ESSI - 2 – Paul Franchi

73 XSLT – la copie "reStylée" Xml vers Xhtml
16/04/2017 <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="xml" version="1.0" indent="yes"/> <xsl:template <xsl:processing-instruction name="xml-stylesheet"> href="bib.css" type="text/css" </xsl:processing-instruction> <html xmlns=" <style>COM{} STYLE{} Authors Author{} </style> <body> <xsl:copy> <xsl:apply-templates </xsl:copy> </body> </html> <xsl:template match="comment()" > <COM><xsl:value-of select="." /> </COM> </xsl:template> <xsl:template match="processing-instruction()" > <STYLE> <xsl:value-of select="." /> </STYLE> </xsl:stylesheet> attribute AVANT les Enfants XSLT – la copie "reStylée" Xml vers Xhtml xsl 16/04/2017 Transparent 73 Langages & Documents ESSI - 2 – Paul Franchi

74 XSLT - Réaliser une importation XML
16/04/2017 XSLT - Réaliser une importation XML <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="copieAvecImport.xsl"?> <Bib dom="XML"> <import href="auteurs.xml" /> <Article> … </Article> </Bib> <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" 1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="imp" select=" " /> <liste> # Auteurs: <xsl:value-of select="count(//Author)" /># <xsl:apply-templates select=" $imp/* " /> </liste> </xsl:stylesheet > .xml <?xml version="1.0" encoding="ISO "?> <AuthorsIndex dom="XML"> <Author aut_id="1">Bernd Amann</Author> <Author aut_id="2">Philippe Rigaux</Author> </AuthorsIndex> .xsl .xml 16/04/2017 Transparent 74 Langages & Documents ESSI - 2 – Paul Franchi

75 XSLT - importer une feuille XSL
16/04/2017 XSLT - importer une feuille XSL <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:import href="cd_catalog6.xsl"/> <xsl:template match="/"> <xsl:apply-imports/> <h2 > avec xsl:import </h2> <!-- généré hors html--> </xsl:template> </xsl:stylesheet > static only .xsl <xsl:stylesheet version="1.0" xmlns:xsl=" 1999/XSL/Transform"> ... </xsl:stylesheet > no xsl:output <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="Import_cd_catalog.xsl"?> <CATALOG"> </ CATALOG > .xsl 16/04/2017 Transparent 75 Langages & Documents ESSI - 2 – Paul Franchi

76 XSLT – Appliquer avec JavaScript
16/04/2017 XSLT – Appliquer avec JavaScript <html> <body> <script type="text/javascript"> // Load XML var xml = new ActiveXObject("Microsoft.XMLDOM") xml.async = false xml.load("cdcatalog.xml") // Load XSL var xsl = new ActiveXObject("Microsoft.XMLDOM") xsl.async = false xsl.load("cdcatalog.xsl") // Transform document.write(xml.transformNode(xsl)) </script> </body> </html> I.E. only .xsl 16/04/2017 Transparent 76 Langages & Documents ESSI - 2 – Paul Franchi

77 XSLT "Fragment" avec JS+DOM
16/04/2017 XSLT "Fragment" avec JS+DOM function transform (xml, xsl, id) { try { // navigateur basé sur Gecko if (window.XSLTProcessor) { var fragment; var xsltProcessor = new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); fragment = xsltProcessor.transformToFragment(xml, document); var target = document.getElementById(id); target.innerHTML = fragment.firstChild.innerHTML ; // pfz //target.appendChild(fragment); //orig //document.replaceChild(target, fragment); //orig // ActiveX pour Internet Explorer } else if (window.ActiveXObject) { target.innerHTML = xml.transformNode(xsl); } } catch (e) {return e;} <script type="text/javascript"> var XML= loadXML(xmlURL); var XSL= loadXML(xslURL); function doTheJob() { transform (XML, XSL, 'ICI'); .... } </script> XSLT .js 16/04/2017 Transparent 77 Langages & Documents ESSI - 2 – Paul Franchi

78 XSL-FO – un dialecte de formatage pour Xml
16/04/2017 XSL-FO – un dialecte de formatage pour Xml Les modèles de mise en page < fo:layout-master-set >  orientation, tailles, découpage, marges, etc. Les séquences de pages < fo:page-sequence >  chapitre ou ensemble continu du document qui doit être affiché sur autant de page que nécessaire Les flux < fo:flow >  un ou plusieurs flux sur la même séquence de page Les paragraphes (ou blocs) < fo:block >  un paragraphe de texte 16/04/2017 Transparent 78 Langages & Documents ESSI - 2 – Paul Franchi

79 XSL-FO – un dialecte de formatage pour Xml
16/04/2017 XSL-FO – un dialecte de formatage pour Xml Le modèle <?xml version="1.0" encoding="ISO "?> <fo:root xmlns:fo=" <fo:layout-master-set> <!– ici les modèles de mise en page --> </ fo:layout-master-set> <!– une suite de page --> <fo:page-sequence master='ce_modele'> <!– un flux --> <fo:flow name='xsl-region' font-size='12pt' > <!– un paragraphe --> <fo:block > ICI LE CONTENU TEXTE </fo:block > </fo:flow > </fo:page-sequence > </fo:root> 16/04/2017 Transparent 79 Langages & Documents ESSI - 2 – Paul Franchi

80 compatible avec HTML, XHTML, CSS, DOM, JS, XML, et Dialectes XUL
16/04/2017 XSLT : savoir-faire XSL Production de Documents XML vers XML XML vers (X)HTML XML vers Text Styler avec HTML+CSS avec XML + CSS Transformer XML: Duplication, Extraction, Filtrage, Importation RDF vers XML SVG vers XHTML MathML vers XHTML XML vers DHTML avec JS+DOM XML vers DXML modifier DOM du XML source modifier DOM du XSL source compatible avec HTML, XHTML, CSS, DOM, JS, XML, et Dialectes XUL .xsl .xsl sur le Web W3Schools - XSL Tutorial XSLT- developpez.com XSL avec Café con leche Edutech XSL tutoriel, edutechwiki .xsl .xsl 16/04/2017 Transparent 80 Langages & Documents ESSI - 2 – Paul Franchi

81 .xsl XSLT "inside" XSL XSL XSL RefsWeb TechnosWeb DocExShow
16/04/2017 XSLT "inside" XSL RefsWeb TechnosWeb DocExShow XSLT ASSIST ss PHP/SQL(SI4 project) ShowML & SI4 Project Log Rendus SI4 Sites Web (ébauches) PFZ 2011 "state of the art" sous HTML5 xsl:output xsl;for-each xsl;if test xsl;call-template xsl;variable xsl;param xsl;key xsl:text xsl:attribute xsl:copy xsl:number xsl:processing-instruction xsl:sort xsl:import xsl:strip-space .xsl XSL fonctions current() position()  name() generate-id() document() normalize-space() Namespace mode svg XSL 16/04/2017 Transparent 81 Langages & Documents ESSI - 2 – Paul Franchi

82 Quelques Environnements XSLT
16/04/2017 Quelques Environnements XSLT Mozilla - Firefox 1.5 => 28.0 SAFARI 5.0 => 7.0 I.E. 6. = > I.E. 10, Google Chrome avec des restrictions sur les fichiers "locaux" Editix XSLT Altova XSLT 2.0 Le projet Apache – APIs Java Xerces – analyseurs XML, Dom, Sax, Xpath Xalan – processeur XSLT 1.0 FOP – formateur XSL-FO vers PDF Le projet Gnome (GNU) – Lib C/C libxml2 – analyseurs XML, Dom, Sax, Xpath libxslt – processeur XSLT 1.0 xmllint & xmlcatalog 16/04/2017 Transparent 82 Langages & Documents ESSI - 2 – Paul Franchi

83 Quelques Processeurs XSLT
16/04/2017 Quelques Processeurs XSLT xsltproc() XSL 1.0 ss linux, Win, Mac OS Saxon CE, HE - XSL 2.0 & Xpath 2.0 Xalan pour C++ & Java XML Parser for C, for Java, for PL/SQL Koala XSL Engine avec Sax 1.0 et Dom 1.0 16/04/2017 Transparent 83 Langages & Documents ESSI - 2 – Paul Franchi

84 XSLT: méthologie et performances
16/04/2017 XSLT: méthologie et performances Développer en "modèle réduit" utiliser une version (ou plus) réduite du source XML supportant le dialecte (DTD, XSD) à traiter Préférer la programmation par règles à la programmation impérative "apply-template" vs "call-remplate" "match" vs "for-each" ne pas abuser des select="//Elem" Développer en pas à pas valider "template" by "template" formatter le code produit (<xsl:text>&return;</xxsl/text>) analyser le code produit ('output') Tester en "vraie grandeur" supportant la définition du dialecte à traiter et valider la complexité sauf Webkit 16/04/2017 Transparent 84 Langages & Documents ESSI - 2 – Paul Franchi

85 XSLT: sources d'erreurs
16/04/2017 XSLT: sources d'erreurs la feuille XSL est introuvable (path faux dans href) code XSL mal formé (syntaxe XML) construction XSL non valide (dialecte "Transform") restriction "top level" restrictions "choose", "attribute", "processing-instruction," "sort", "param", etc portée des variables locales vs globales expression XPath mal formée ou Node Fragment (XSLT 1.0) Node Set vide et utilisé fonctions XSL: utilisation non conforme sémantique XML produit non valide ordre des "import", "templates" expression XPath fausse récursion infinie (boucle) <xsl:apply-templates select="$VAR" /> <xsl:apply-templates select="." /> 16/04/2017 Transparent 85 Langages & Documents ESSI - 2 – Paul Franchi

86 Quelques "trucs" à savoir ...
16/04/2017 Quelques "trucs" à savoir ... XSLT côté serveur Google Chrome ne supporte que les XSLT "servies", donc pas de fichiers locaux Google Chrome impose une "same-origin policy" qui bloque les AJAX sur fichiers locaux I.E. refuse le mélange fichiers locaux et distants dans une XSLT Peut nécessiter d'exporter le bon type MIME dans .htaccess Avec FF, pour utiliser des feuilles XSL, non placées dans la sous arborescence XML Changer dans "about:config" "fileuri strict security policy" à "false" Avec Webkit (Safari & Chrome), ne pas générer de RETURN pour formater le xml produit bug sur les conditions de match = "ELEM = $VAR ]" 16/04/2017 Transparent 86 Langages & Documents ESSI - 2 – Paul Franchi

87 Tree Structure of an XML Document
16/04/2017 Tree Structure of an XML Document from XML in a nutshell 7 types of Nodes The root node (XML # XPath) Element nodes Text nodes Attribute nodes Comment nodes Processing instruction nodes Namespace nodes - CDATA, DOCTYPE, are NOT in the tree - attributes are NOT children - Namespaces are not selectable 16/04/2017 Transparent 87 Langages & Documents ESSI - 2 – Paul Franchi

88 How XSLT works ... Processing Model Applying Template Rules Patterns
16/04/2017 How XSLT works ... Processing Model Applying Template Rules Patterns Conflict Resolution for Template Rules Priority. Modes Built-In Template Rules Template Rule Content Literal Result Elements Attribute Value Templates How XPath Context Is Initialized Whitespace Stripping Serializing the Result Tree Disabling Output Escaping XSLT Elements by Use Case from Evan Lentz 16/04/2017 Transparent 88 Langages & Documents ESSI - 2 – Paul Franchi

89 How XSLT works ... from Evan Lentz Processing Model
16/04/2017 How XSLT works ... from Evan Lentz Processing Model All XSLT processing consists of iterations over lists of nodes. At any given point in the execution of a stylesheet there is a current node for that iteration, and there is a current node list being iterated over. The current node list is the ordered list of nodes being iterated over, and the current node is a member of that list. There are two mechanisms for iterating over a list of nodes: xsl:apply-templates and xsl:for-each. xsl:apply-templates is an XSLT instruction that iterates over a given node-set. It invokes the best-matching template rule for each of the nodes in the node-set 16/04/2017 Transparent 89 Langages & Documents ESSI - 2 – Paul Franchi

90 How XSLT works ... from Evan Lentz Template Rules
16/04/2017 How XSLT works ... from Evan Lentz Template Rules An XSLT stylesheet contains a set of template rules. Broadly speaking, there are two kinds of template rules: Those you define Those that XSLT defines for you, a.k.a. the built-in template rules XSLT defines a built-in template rule for each of the seven types of node. This ensures that any call to xsl:apply-templates will never fail to find a matching template rule for the current node, even if your stylesheet contains no explicit template rules at all (an empty stylesheet). 16/04/2017 Transparent 90 Langages & Documents ESSI - 2 – Paul Franchi

91 How XSLT works ... from Evan Lentz Applying Template Rules
16/04/2017 How XSLT works ... from Evan Lentz Applying Template Rules Whenever xsl:apply-templates is called, the XSLT processor examines the patterns of all the stylesheet's template rules. For each node being iterated over, it first finds all the template rules with patterns that match the node and then instantiates the best-matching template rule among them. Starting from the first virtual call to <xsl:apply-templates select="/"/>, the recursion continues as long as the xsl:apply-templates instruction appears inside an instantiated template rule and is given a non-empty list of nodes to process. This process continues until there are no more nodes to process. At that point, the entire result tree has been constructed. The select attribute on xsl:apply-templates is optional: <xsl:apply-templates/> When absent, it is short for: <xsl:apply-templates select="node()"/> This instruction applies templates to the child nodes of the current node. In other words, it populates the current node list with the set of nodes returned by the node() expression, and it iterates over them in document order, invoking the best-matching template rule for each node. 16/04/2017 Transparent 91 Langages & Documents ESSI - 2 – Paul Franchi

92 How XSLT works ... from Evan Lentz Patterns
16/04/2017 How XSLT works ... from Evan Lentz Patterns XSLT patterns appear most commonly inside the xsl:template element's match attribute. (They also appear in certain attributes of the xsl:key and xsl:number elements.) The allowed syntax for a pattern is a subset of the allowed syntax for XPath expressions (see Chapter 2, “The XPath Language”). In other words, every pattern is also a syntactically valid expression, but not every expression is a valid pattern. A pattern consists of one or more location path patterns separated by |. A location path pattern is a location path that exclusively uses either the child or attribute axis in each of its steps. The // operator (in its abbreviated form only) can also be used. Finally, a location path pattern can also start with an id() or key() function call with a literal string argument. 16/04/2017 Transparent 92 Langages & Documents ESSI - 2 – Paul Franchi

93 How XSLT works ... from Evan Lentz Pattern What it matches /
16/04/2017 How XSLT works ... from Evan Lentz Pattern What it matches / the root node the root element only if its name is doc and it has a format attribute with the value simple bar any bar element foo/bar any bar element whose parent is a foo element id('xyz')/foo any foo element whose parent is an element that has an ID-typed attribute with the value xyz section//para any para element that has a section element ancestor @foo any attribute named foo @* any attribute node() any child node (i.e., element, text, comment, or processing instruction) text() any text node * any element xyz:* any element in the namespace designated by the xyz prefix *[not(self::xyz:*)] any element that is not in the namespace designated by the xyz prefix para[2] any para element that is the second para child of its parent para[last()] any para element that is the last para child of its parent 16/04/2017 Transparent 93 Langages & Documents ESSI - 2 – Paul Franchi

94 How XSLT works ... from Evan Lentz
16/04/2017 How XSLT works ... from Evan Lentz Conflict Resolution for Template Rules When a given node matches the patterns of more than one template rule, the XSLT processor decides which template rule to instantiate according to its rules for conflict resolution. For example, it is quite common to have a stylesheet that includes two template rules like these: <xsl:template match="foo"> <! > </xsl:template> <xsl:template match="*"> <! > </xsl:template> The first template rule matches foo elements. The second matches any element. That means that a foo element will match both template rules. But the XSLT processor has to pick only one of them. Assuming that the stylesheet containing these rules isn't imported into another stylesheet that overrides them, the XSLT processor will pick the template rule with the foo pattern. Based on a comparison of the patterns * and foo, it determines that foo has higher priority. Generally speaking, the more specific a pattern is, the higher priority it has. 16/04/2017 Transparent 94 Langages & Documents ESSI - 2 – Paul Franchi

95 How XSLT works ... from Evan Lentz
16/04/2017 How XSLT works ... from Evan Lentz Conflict Resolution for Template Rules However, before patterns are ever examined for their relative priority, the XSLT processor first eliminates all matching template rules that have lower import precedence. Basically, template rules in an imported stylesheet have lower import precedence than template rules in the importing stylesheet. For the precise rules on how import precedence is determined, see the “xsl:import” section in Chapter 4. Thus, there are two steps in this process of elimination: The XSLT processor eliminates rules with lower import precedence. Among the remaining template rules, the XSLT processor eliminates the rules with lower priority. It is an error if there is more than one template rule left. If that happens, the XSLT processor can either signal the error or recover by invoking the matching template rule that occurs last in the stylesheet. Most processors will at least give a warning if this happens 16/04/2017 Transparent 95 Langages & Documents ESSI - 2 – Paul Franchi

96 How XSLT works ... from Evan Lentz Priority
16/04/2017 How XSLT works ... from Evan Lentz Priority A template rule can explicitly specify its priority using the optional priority attribute on the xsl:template element. The value of the priority attribute may be any decimal number—positive or negative. The higher the number in the priority attribute, the higher the priority of the template rule. Modes Both the xsl:template and xsl:apply-templates elements can have an optional mode attribute. Modes let you partition sets of template rules into different independent scopes. For example, this instruction will only consider template rules associated with the foo mode: 16/04/2017 Transparent 96 Langages & Documents ESSI - 2 – Paul Franchi

97 How XSLT works ... from Evan Lentz Built-In Template Rules
16/04/2017 How XSLT works ... from Evan Lentz Built-In Template Rules By definition, the built-in template rules have lower import precedence than any template rules that you explicitly define. Thus, explicit template rules always override built-in template rules. The built-in rules come in handy when you don't specify an explicit template rule to match a particular node. The built-in template rule for root nodes and element nodes is to apply templates to children. The explicit formulation of this rule is: <xsl:template match="/ | *"> <xsl:apply-templates/> </xsl:template> Many stylesheets include an explicit template rule that matches the root node: <xsl:template match="/">.... This allows you to take control of processing right off the bat; however, it isn't required. Instead, you could rely on the built-in template rule for root nodes and elements to recursively apply templates until they reach a node for which you have defined an explicit template rule. For each mode that's used in a stylesheet, XSLT also automatically defines an equivalent built-in template rule for root nodes and elements that automates continued processing of children within the same mode: <xsl:template match="/ | *" mode="mode-name"> <xsl:apply-templates mode="mode-name"/> </xsl:template> 16/04/2017 Transparent 97 Langages & Documents ESSI - 2 – Paul Franchi

98 How XSLT works ... from Evan Lentz Built-In Template Rules:
16/04/2017 How XSLT works ... from Evan Lentz Built-In Template Rules: The built-in template rule for text nodes and attribute nodes is to create a text node with the string-value of the node. The explicit formulation of this rule is: <xsl:template match="text() <xsl:value-of select="."/> </xsl:template> The net effect of this rule is that text nodes, by default, are copied to the result tree. This is evident if you try executing an empty stylesheet—an xsl:stylesheet element that contains no explicit template rules. The result tree consists of one large text node—a concatenation of all text nodes in the source tree. The built-in template rule for processing instructions and comments is to do nothing: <xsl:template match="processing-instruction() | comment()"/> The built-in template rule for namespace nodes is also to do nothing. Since no pattern can match a namespace node, there is no explicit formulation of this rule, and it cannot be overridden. 16/04/2017 Transparent 98 Langages & Documents ESSI - 2 – Paul Franchi

99 What XSLT can do ... Appliquer un style CSS (Cible)
16/04/2017 Cible: XML (X)HTML SVG XSD XSL CSS DTD txt Sources: XML (X)HTML SVG XSD XSL Dialecte Appliquer un style CSS (Cible) Récupérer un style CSS (Source) Accepter un Namespace (Source) Créer un Namespace (Cible) Re(dé)baliser Copie partielle Copie modifiée Créer de nouveaux éléments (Cible) Importer XML (plusieurs sources) Génération et Filtrage de Données Calculer Restructurer relever / applatir regrouper / séparer éliminer les doublons What XSLT can do ... 16/04/2017 Transparent 99 Langages & Documents ESSI - 2 – Paul Franchi

100 Grouper et éliminer les doublons avec XSLT 1.0
16/04/2017 Grouper et éliminer les doublons avec XSLT 1.0 Munchean method <xsl:key name="keyContinent" match="//country" <xsl:template match="/"> <xsl:element name="world"> <xsl:for-each select="//country [ generate-id(.) = ] "> <xsl:sort data-type="text" order="ascending"/> <xsl:variable name="continent" <xsl:apply-templates = $continent]" mode="group"> <xsl:sort data-type="text" </xsl:apply-templates></xsl:for-each> </xsl:element></xsl:template> XSL 16/04/2017 Transparent 100 Langages & Documents ESSI - 2 – Paul Franchi

101 Conversion avec node-set()
16/04/2017 XSLT avec EXSLT Extension exsl Conversion avec node-set() <xsl:stylesheet version="1.0" xmlns:xsl= xmlns:exsl=" extension-element-prefixes="exsl" > <xsl:variable name= "TREE" > <a> <b> <c> <d /> </c> </b> </a> </ xsl:variable > <xsl:apply-templates select="exsl:node-set($TREE)/* "/> <xsl:variable name="SEMESTRE" select="exsl:node-set($SEMESTRES)/*SEM"/> XSL 16/04/2017 Transparent 101 Langages & Documents ESSI - 2 – Paul Franchi

102 Multipasses en XSLT 1.0 XSL voir Design Patterns
16/04/2017 Multipasses en XSLT 1.0 voir Design Patterns <xsl:template match="/"> <variable name="XML_1"> <xsl:apply-templates mode="passe1" /> </variable> <variable name="XML_2"> <xsl:apply-templates mode="passe2" select=" exsl:node-set($XML_1) /* " /> <xsl:apply-templates mode="passe3" select=" exsl:node-set($XML_2) /* " /> </xsl:template> XSL 16/04/2017 Transparent 102 Langages & Documents ESSI - 2 – Paul Franchi

103 Conversion of result tree fragments to node-sets
16/04/2017 XSLT from xml.com Uses XPath 2.0 Conversion of result tree fragments to node-sets Multiple output documents <xsl:variable name= "TREE" > <xsl:apply-templates select="SOME" /> </ xsl:variable > <xsl:apply-templates select="$TREE"/> <xsl:result-document href="{$uri}" format="textFormat"> <xsl:value-of select="."/> </xsl:result-document> 16/04/2017 Transparent 103 Langages & Documents ESSI - 2 – Paul Franchi

104 Built-in support for grouping
16/04/2017 XSLT from xml.com Built-in support for grouping User-defined functions <xsl:for-each-group select="//city" <xsl:apply-templates select="current-group()" /> </xsl:for-each-group> XPath 2.0 <xsl:function name="str:reverse"> <xsl:param name="sentence"/> <xsl:result select= "if (contains($sentence, ' ')) then concat(str:reverse(substring-after($sentence, ' ')), ' ', substring-before($sentence, ' ')) else $sentence"/> </xsl:function> 16/04/2017 Transparent 104 Langages & Documents ESSI - 2 – Paul Franchi

105 tokenize() pour scanner un text
16/04/2017 XSLT tokenize() pour scanner un text <xsl:function name="str:csv2xml"> <xsl:param name="texte"/> <xsl:for-each select = "tokenize($texte, ';')"> <data> <xsl:value-of select="." /> </data> </xsl:for-each > </xsl:function> 16/04/2017 Transparent 105 Langages & Documents ESSI - 2 – Paul Franchi

106 Every expression returns a sequence. Operations on sequences
16/04/2017 XPath from xml.com Every expression returns a sequence. Operations on sequences Conditional expressions Quantifier, Inter, Diff, Union sum(for $x in /order/item return $x/price * $x/quantity) if ($widget1/unit < $widget2/unit) then $widget1 else $widget2 some $x in /students/student/name satisfies $x = "Fred" $x intersect /foo/bar from carton 16/04/2017 Transparent 106 Langages & Documents ESSI - 2 – Paul Franchi

107 Stylesheet Modularity (Package)
16/04/2017 XPath Support for Streaming. Stylesheet Modularity (Package) Higher-Order Functions Text Manipulations from w3.org/ from Stylusstudio 16/04/2017 Transparent 107 Langages & Documents ESSI - 2 – Paul Franchi


Télécharger ppt "Chap IX - XSL Transformations"

Présentations similaires


Annonces Google