Memento XSL

Table des matières :

Entête XML avec feuille de style XSL

<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet href="xxx.xsl" type="text/xsl" ?> <!DOCTYPE xml SYSTEM "https://loribel.com/toto.dtd">

Code HTML / Javascipt pour parser un fichier XML (avec IE)

<html> <body> <script type="text/javascript"> // chargement du fichier XML var xml = new ActiveXObject("Microsoft.XMLDOM") xml.async = false xml.load("toto.xml")
// chargement du fichier XSL var xsl = new ActiveXObject("Microsoft.XMLDOM") xsl.async = false xsl.load("toto.xsl")
// transformation en Html document.write(xml.transformNode(xsl)) </script> </body> </html>

Exemple de fichier XSL

<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" />
<xsl:template match="/"> <xsl:value-of select="aaa" /> <xsl:copy-of select="bbb" /> <xsl:apply-templates select="bbb" />
<xsl:value-of select="." /> <xsl:value-of select="aaa/text()" /> </xsl:template> </xsl:stylesheet>

Instructions XSL (xsl:if, xsl:for-each, xsl:template, xsl:choose, ...)

xsl:template match - xsl:apply-templates

xsl:template - xsl:call-template

xsl:for-each

xsl:if

xsl:choose

xsl:variable

xsl:number

Links - <a href="xxx"

generate-id

xsl-text

Travailler avec des nombres (calculer, formatter, ...)

  • Addition : +
  • Soustraction : -
  • Multiplication : *
  • Division : div
  • Modulo : mod

<xsl:value-of select="$1 + $2"/> <xsl:value-of select="value1 - value2"/> <xsl:value-of select="value1 div value2"/> <xsl:value-of select="value1 * value2"/> <xsl:value-of select="value mod 3"/>
http://www.laltruiste.com/

Templates replace

Template replace

<xsl:template name="replace"> <xsl:param name="stringIn" /> <xsl:param name="charsIn" /> <xsl:param name="charsOut" /> <xsl:choose> <xsl:when test="contains($stringIn, $charsIn)"> <xsl:value-of select=" concat(substring-before($stringIn, $charsIn), $charsOut)"/> <xsl:call-template name="replaceCharsInString"> <xsl:with-param name="stringIn" select= "substring-after($stringIn, $charsIn)"/> <xsl:with-param name="charsIn" select="$charsIn"/> <xsl:with-param name="charsOut" select="$charsOut"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$stringIn"/> </xsl:otherwise> </xsl:choose> </xsl:template>


Article extrait du site Loribel.com.
https://loribel.com/info/memento/xsl.html