1. variable xslt 1.1. Statements variables XML file reference <? xml version = "1.0" encoding = "UTF-8"?> <racine> <value> 1 </ value> <value> 2 </ value> </ root> For the following examples of this paragraph we will take this as reference xml The term "variable" can be misleading in that variable xslt <xsl:variable name= ""/> sees its value set dice its initialization, the latter being either by the select attribute containing a xpath, either by values between the tags. etc. .... Examples <xsl:variable name= "var1" select="/A"/> <xsl:variable name= "var2 "> this </ xsl: variable> <xsl:variable name= "var3"> <xsl:value-of select= "."/> </ xsl: variable> Once these tags written, the content will no longer be amended and will be known by the name attribute of the tag question, preceded by the $ sign example <xsl:value-of select="$var2"/> Beware if you can not change its value, does nothing to create a new variable of the same name in another node. See the end of the following paragraph. 1.2. Scope of variable The scope of a variable xslt is intimately linked to the structure of xml xslt, in fact it is exactly the node where it is declared and all descendants of this node. Here examples are certainly more speaking and <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable Name="var1"> a </ xsl: variable> <xsl:template Match="/"> <xsl:variable Name="var2"> b </ xsl: variable> <html> <body> Call first template <br/> <xsl:value-of Select="$var1"/> <br/> <xsl:value-of Select="$var2"/> <br/> <xsl:apply-templates select="//valeur"> </ xsl: apply-templates> </ Body> </ Html> </ Xsl: template> <xsl:template Match="valeur"> <xsl:variable Name="var3"> c </ xsl: variable> Call the second template <br/> <xsl:value-of Select="$var1"/> <br/> <xsl:value-of Select="$var2"/> <br/> <xsl:value-of Select="$var3"/> <br/> </ Xsl: template> </ xsl: stylesheet> xsl: template match = "/" will cause an error processor xslt on the line stressed. Indeed, the variable var2 is reported in the first template but as secondexsl: template match = "value" does not belong has its descendants it is unknown. By cons, after deleting the line in error, that it will var1 who is a beacon racinexsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/ Transform "of the document will, however, recognized throughout the leaves xslt. The result: <html> <body> Call first template <br/> A <br/> b <br/> Call the second template <br/> A <br/> c <br/> Call the second template <b/r> A <br/> c <b/r> </ Body> </ html> Attention what is said here for the templates is true for all nodes. Example: <xsl:if test="current()"> <xsl:variable Name="var4" select="."/> <xsl:value-of Select="$var4"/> </ xsl: if> will work, whereas: <xsl:if test="current()"> <xsl:variable Name="var4" select="."/> </ xsl: if> <xsl:value-of select="$var4"/> will produce an error; var4 disappearing at the exit node xsl: if test = "current ()". Note: The code below is quite valid: <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="var1" select="//valeur[2]"> </ xsl: variable> <xsl:template Match="/"> <html> <body> <xsl:variable name="var1" select="$var1+10"> </ xsl: variable> <xsl:value-of Select="$var1"/> <br/> <xsl:apply-templates select="//valeur"> </ xsl: apply-templates> </ Body> </ Html> </ Xsl: template> <xsl:template Match="valeur"> <xsl:value-of Select="$var1"/> <br/> </ Xsl: template> </ xsl: stylesheet> Indeed, here we create a variable local node xsl: template match ="/", which takes precedence over the previous one. So these are two different variables, and not changing the first. in fact, the result is: <html> <body> 12 <br/> 2 <br/> 2 <br/> </ body> </ html> The value of the variable has not been amended, a variable "local" has simply taken temporarily not on the "overall".
1.3. Content of a variable a variable can contain these different types of data: string, number, boolean, node-set that all belong to Xpath, plus the type node-fragment to it. Number <xsl:variable name="var1" select="number(10.2)"/> <xsl:variable Name="var2"> 15.2 </ xsl: variable> string <xsl:variable name="var3"> trick </ xsl: variable> <xsl:variable name="var4" select="string('truc')"/> booleen <xsl: variable name = "var5" select = "$ var1 <$ var2" /> Another way to declare string or number you may have seen <xsl:variable name="var2"> 15.2 </ xsl: variable> <xsl:variable name="var3"> trick </ xsl: variable> attention this type of writing if it is inconsequential to the display may lead to errors tests.Ceci will be more detailed at the end of node-fragment XSLT file <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="var1" select="number(10.2)"/> <xsl:variable name="var2"> 15.2 </ xsl: variable> <xsl:variable name="var3"> trick </ xsl: variable> <xsl:variable name="var4" select="string('truc')"/> <xsl: variable name = "var5" select = "$ var1 <$ var2" /> <xsl:template match="/"> <html> <body> <xsl:value-of select="$var1"/> <br/> <xsl:value-of select="$var2"/> <br/> <xsl:value-of select="$var3"/> <br/> <xsl:value-of select="$var4"/> <br/> <xsl:value-of select="$var5"/> <br/> </ body> </ html> </ xsl: template> </ xsl: stylesheet> result <html> <body> 10.2 <br> 15.2 <br> trick trick <br> <br> true <br> </ body> </ html> For types node-set, it should be noted that one of their big advantage is that it can be used in expressions xpath for example: <xsl:variable name="var1" select="/racine"/> <xsl:variable name="var2" select="//valeur"/> $ var2 [1] will d? access to the first tag value but $ var1/valeur [1] too. All expressions xpath be used on a variable and a variable can also be used in any expression. XSLT file <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="var1" select="/racine"/> <xsl:variable name="var2" select="//valeur"/> <xsl:template match="/"> <html> <body> <xsl:value-of select="$var2[1]"/> <br> </ br> <xsl:value-of select="$var1/valeur[1]"/> <br> </ br> <xsl:if test="$var1/valeur[1]=$var2[1]"> both terms have the same value. </ xsl: if> </ body> </ html> </ xsl: template> </ xsl: stylesheet> result <html> <body> 1 <br> 1 <br> both terms have the same value. </ body> </ html> And the last guy who is clean is variable node fragment. The aim is to (re-) create xml knots in the variable and not the xpath (especially if they do not exist) example: <xsl:variable name="var"> <truc> the first thing </ trick> <truc> second thing </ trick> </ xsl: variable> attention, no xpath can not be used on a node fragment, [] and / are banned, and only operators on the strings are allowed (length (), substring A value of ........) select this type of variable does make the result that the content of tags, not the tags themselves XSLT file <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="var"> <racine> <truc> the first thing </ trick> <truc> second thing </ trick> </ root> </ xsl: variable> <xsl:template match="/"> <html> <body> <xsl:value-of select="$var"/> </ body> </ html> </ xsl: template> </ xsl: stylesheet> result <html> <body> the first thing second thing </ body> </ html> The interest of this type of variable lies rather in the construction of tree xml, chapter that we will see later. We note simply that the xsl: copy-of it takes into account the presence of tags XSLT file <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="var"> <racine> <truc> the first thing </ trick> <truc> second thing </ trick> </ root> </ xsl: variable> <xsl:template match="/"> <xsl:copy-of select="$var"> </ xsl: copy-of> </ xsl: template> </ xsl: stylesheet> result <? xml version = "1.0" encoding = "UTF-8"?> <racine> <truc> the first thing </ trick> <truc> second thing </ trick> </ root> Note: careful not to abuse this type of writing if you do not want to create a node fragment. Indeed it can disrupt the processor in tests, as follows: XSLT file <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="var"> 2 </ xsl: variable> <xsl:template match="/"> <html> <body> <xsl:value-of select="racine/valeur[$var]"> </ body> </ html </ xsl: template> </ xsl: stylesheet> result <html> <body> 1 </ body> </ html Indeed, here $ var is not interpreted as a nombre.Si you want to keep this writing variable then you will need: <xsl:value-of select="racine/valeur[position()=$var]"> or <xsl:value-of select="racine/valeur[number($var)]"> Finally, it is quite possible, to appeal by apply-templates or call-templates (detail later) to the interior of a variable. The only type of result inaccessible in this case being the node-set. XSLT file <? xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="var"> <xsl:apply-templates select="//valeur"/> </ xsl: variable> <xsl:template match="/"> <html> <body> <xsl:value-of select="$var"/> </ body> </ html> </ xsl: template> <xsl:template match="valeur"> <xsl:value-of select="concat(.,';')"/> </ xsl: template> </ xsl: stylesheet result <html> <body> 1, 2, </ body> </ html>
|