Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: can I declare a variable globally and later assign a value to it

Tags:

What can I do to make this code work?

<xsl:choose>   <xsl:when test='type = 6'>     <xsl:variable name='title' select='root/info/title' />   </xsl:when>   <xsl:when test='type = 7'>     <xsl:variable name='title' select='root/name' />   </xsl:when>   <xsl:otherwise>     <xsl:variable name='title'>unknown</xsl:variable>   </xsl:otherwise> </xsl:choose>  <div class='title'>   <xsl:value-of select='$title'/> </div> 

This doesn't work because when i do <xsl:value-of select='$title'/>, $title is out of scope. I tried to add the line <xsl:variable name='title'/> outside of the scope, but that won't work either, because then when i call <xsl:variable name='title' select='root/info/title' /> for example, i already have set this variable before. How should I solve this?

like image 413
Jules Colle Avatar asked Nov 17 '10 13:11

Jules Colle


People also ask

How do I assign a value to a global variable in XSLT?

XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

What is difference between Param and variable in XSLT?

The difference is that the value of an xsl:param could be set outside the context in which it is declared.

How do I find the value of a variable in XSLT?

I second @scunliffe - if $var exists but is empty, test="$var" will still return true. However, you may need to write it test="not($var = '')" if you are using earlier versions of XSL.

What is Number () in XSLT?

Definition and Usage. The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.


2 Answers

You can move the choose inside the setting of the variable, like this:

<xsl:variable name="title">   <xsl:choose>     <xsl:when test='type=6'>       <xsl:value-of select="root/info/title" />     </xsl:when>     ...   </xsl:choose> </xsl:variable>  <div class='title'>   <xsl:value-of select="$title" /> </div> 
like image 170
carols10cents Avatar answered Sep 22 '22 22:09

carols10cents


<xsl:choose>    <xsl:when test='type = 6'>      <xsl:variable name='title' select='root/info/title' />    </xsl:when>    <xsl:when test='type = 7'>      <xsl:variable name='title' select='root/name' />    </xsl:when>    <xsl:otherwise>      <xsl:variable name='title'>unknown</xsl:variable>    </xsl:otherwise>  </xsl:choose>   <div class='title'>    <xsl:value-of select='$title'/>  </div>  

This doesn't work

This is a FAQ:

You are defining several variables, each named $title and each useless, because it goes out of scope immediately.

The proper way in XSLT 1.0 to define a variable based on conditions is:

<xsl:variable name="vTitle">     <xsl:choose>       <xsl:when test='type = 6'>         <xsl:value-of select='root/info/title' />       </xsl:when>       <xsl:when test='type = 7'>         <xsl:value-of  select='root/name' />       </xsl:when>       <xsl:otherwise>         <xsl:value-of select="'unknown'"/>       </xsl:otherwise>     </xsl:choose> </xsl:variable> 

Another way of defining the same variable: In this particular case you want the variable to have a string value. This can be expressed in a more compact form:

<xsl:variable name="vTitle2" select= "concat(root/info/title[current()/type=6],         root/name[current()/type=7],         substring('unknown', 1 div (type > 7 or not(type > 5)))        ) "/> 

Finally, in XSLT 2.0 one can define such a variable even more conveniently:

<xsl:variable name="vTitle3" as="xs:string" select=  "if(type eq 6)     then root/info/title     else if(type eq 7)             then root/name             else 'unknown'  "/> 
like image 42
Dimitre Novatchev Avatar answered Sep 22 '22 22:09

Dimitre Novatchev