|
I. Introduction XSLT has many features worthy of a high-level language: variables, parameters, tests, loops, functions, including a XSLT stylesheet in another load of several XML documents in a single XSLT stylesheet, Search of XML tags according to numerous criteria, and so on. If you have not read my introduction to XSLT, go quickly read under penalty not understand certain passages. II. Functions XSLT II.A. List of functions XSLT | Function XSLT | Description | | xsl: for-each | "loop" | | xsl: if | If conditional | | xsl: choose, xsl: when and xsl: otherwise | Suite codes conditional instruction switch to C | | xsl: template, xsl: call-template, xsl: param and xsl: with-param | declare and call a function, with or without parameter (s). | | xsl: number | Numbering / meter | II.B. Loops with xsl: for-each I have lied (oh the villain). In fact, xsl: for-each is not really a loop as a "for" language C. This function will take all the nodes of a request XPATH, and will apply their treatment. Let's already the source file: <liste> <invite> Moi </ invite> <invite> Amélie </ invite> <invite> Marie </ invite> <invite> Jéméry </ invite> </ list> Then the XSLT code that will generate a list HTML: <ul> <xsl:for-each> <li> <xsl:apply-templates select="." /> </li> </for-each> </ul> II.C. Function xsl: if The instruction xsl: if possible to run or not certain parts of the code. Example of instruction if: <xsl:if test="nom == 'Victor'"> <xsl:text> Wishes, it looks like my first name! </ xsl: text> </xsl:if> Ok, but where is the xsl: else? Uh ... Sorry, there's no :-( We must use xsl: choose: - /
II.D. Function xsl: choose The function xsl: choose can perform different codes according to different conditions. Example of instruction choose: <xsl:choose> <xsl:when test="name= 'Victor'"> <xsl:text> Hi Victor, it boume? </ xsl: text> </xsl:when> <xsl: when test = "name = 'Pierre' "> <xsl:text> chisel Done ... sorry, it was zero. </ xsl: text> </xsl:when> <xsl: when test = "name = 'Pierre' "> <xsl:text> Message impersonal and cold: Hello. </ xsl: text > </xsl:when> </xsl:choose> It often uses xsl: choose as an alternative to xsl: if to have xsl: other (which replaces xsl: else that does not exist). II.E. A function without parameters (xsl: template and xsl: call-template) To declare a function, it uses the xsl: template. Example: <xsl:template name="hello_world"> <xsl:text>Hello World !</xsl:text> </xsl:template>
It will now call our function with xsl: call-template: <xsl:call-template name="hello_world" /> That's all ... Ben Yeah. In fact, the entire beast!
II.F. A function with parameters (xsl: param and xsl: with-param) <xsl:template name="affiche_somme"> <xsl:param name="a" select="0" /> <xsl:param name="b" select="0" /> <xsl:text>a = </xsl:text> <xsl:value-of select="$a" /> <xsl:text>, b = </xsl:text> <xsl:value-of select="$b" /> <xsl:text>, et a+b = </xsl:text> <xsl:value-of select="$a + $b" /> <xsl:text>.</xsl:text> </xsl:template> When writing <xsl:param name="a" select="0" />: 0 is the default setting a. Call this miracle of programming, always via xsl: call-template: <xsl:call-template name="affiche_somme"> <xsl:with-param name="a" select="173" /> <xsl:with-param name="b">9001</xsl:with-param> </xsl:call-template> You know what, the worst is that it works! This produces an output of style: a = 173, b = 9001, and a + b = 9174.
III. Bouts of useful codes III.A. A real loop I like to tell you right away: XSLT is not done to make loops! It is a language that uses recursive algorithms, not iterative. It is much cleaner, but less natural. But sometimes, we still need loops. And yes, they never leave us, we programmer :-) I therefore offers this little piece of code that could perhaps save your life (who knows?): <xsl:template name="boucle"> <xsl:param name="debut" select="0" /> <xsl:param name="fin" select="0" /> <xsl:text>i = </xsl:text> <xsl:value-of select="$debut" /> <br/> <xsl:if test="$debut <$ end"> <xsl:call-template name="boucle"> <xsl:with-param name="debut" select="($debut)+1" /> <xsl:with-param name="fin" select="$fin" /> </xsl:call-template> </xsl:if> </xsl:template> Prototype of a function: loop ($ debut, $ fin). It will display the numbers $ debut to $ end. What to say? Ah yes, "<" bizarre. Well that is still in XML, and "alt" can not be written as it stood. We must write in its encoded form "<". That's all! IV. Functions XPATH | Function XPATH | Description | | position () | Position a tag in the XML tree | | count (xpath) | the number of élèments a request XPATH | V. String (XPATH) V.A. List of functions | Function XPATH | Description | | concat (a, b, [c, d ,...]) | Concatenation channels | | substring (a, debut [length]) | Extract part of the chain | | substring-before (txt, token) | The foregoing sub-chain token | | substring-after (txt, token) ` | The following sub-chain token | | string-length (txt) | length of the chain txt | | translate (txt before, after) | transforms the characters forward by those in the chain after txt | V.B. Example (1) It will say that the current node (.) Contains my email: victor.stinner + haypocalc.com (read '@' and not'+'). Example usage functions concat, substring-after and substring-before: <xsl:value-of select="concat( substring-before(.,'@'), 'ON', substring-after(.,'@'))" /> Well, yes, you can write on several lines if you like, hey hey hey! Released: "victor.stinner ON haypocalc.com" cool, that's what I wanted to do (hide my email) ;-)
V.C. Example (2) Let's say the current node (.) Contains the name of an image: "bibi_phoque.gif." It will replace the extension '. Gif'. Png. Example use functions and substring length: <xsl:value-of select="concat(substring(.,length(.)-4),'.png')" /> Well, the function xsl: string-before "could be" more suited than substring. Except ... if the name of the image has a point! "bibi.phoque.gif." VI. Conditions (XPATH) | Function XPath | Description | | True () | True | | False () | False | | not (cond) | contrary to the condition cond | | "a & b" or "a and b" | a and b | | "| b" or "a or b" | a or b | | a == b | a equal b? | | a! = b | a different b? | | a <b | a lower b?(alt read a, b, but we must write unchanged) | | a <= b | has less than or equal b? (read a <= b) | | a> b | a higher b? (read a> b) | | a> = b | a greater than or equal b? (read a> = b) | VI. Mathematics | Function XPath | Description | | sum (xpath) | Sum of élèments XPath | | floor (n) | Rounding default | | ceiling (n) | Rounded by excesses | | round (n) | Rounded to the nearest | | a + b | Sum of a and b | | a- b | Difference a and b | | a * b | product of a and b | | a div b | Quotient of a by b | | a mod b | remains in the entire division has b | | -x | Opposed x |
|