Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT variable into the curly braces

Tags:

xslt

What is the meaning of curled brackets {} in the following sample (in preceding lines, the variable $fieldName is initialized and populated with string):

<xsl:element name="{$fieldName}">     <xsl:apply-templates select="field"/> </xsl:element> 
like image 330
user3010912 Avatar asked Feb 28 '14 21:02

user3010912


People also ask

How do I assign a value to a 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!

Can we reassign a value to variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.

What is a curly braces in programming?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.

Does C++ use curly braces?

Curly braces (also referred to as just "braces" or as "curly brackets") are a major part of the C++ programming language. They are used in several different constructs, outlined below, and this can sometimes be confusing for beginners. An opening curly brace { must always be followed by a closing curly brace } .


2 Answers

You can use these curly braces (attribute value templates) whenever you need to compute an expression in attributes which would otherwise treat the contents as text.

For example, suppose you have a XML source this one:

<link site="www.stackoverflow.com"/> 

and you would like to generate an HTML link from it like

<a href="http://www.stackoverflow.com">Click here</a> 

If you simply read the contents of @site into the href attribute like this:

<xsl:template match="link">     <a href="http://@site">Click here</a> </xsl:template> 

it won't work, since it will be treated as plain text and you will get:

<a href="http://@site">Click here</a> 

But if you wrap the @site in curly braces:

<xsl:template match="link">     <a href="http://{@site}">Click here</a> </xsl:template> 

It will be treated as XPath, will be executed and you will get:

<a href="http://www.stackoverflow.com">Click here</a> 

If it weren't for the curly braces, you would need to use <xsl:attribute> in <a> containing an <xsl:value-of> to obtain the same result:

<xsl:template match="link">     <a>         <xsl:attribute name="href">             <xsl:text>http://</xsl:text><xsl:value-of select="@site"/>         </xsl:attribute>         <xsl:text>Link</xsl:text>     </a> </xsl:template> 

In your example, the name atrribute of <xsl:element> requires a string. To treat that string as an XPath expression and replace it with the result of the variable $fieldName, you either place it within curly braces as you did, or you use the <xsl:attribute> element as above:

<xsl:element>     <xsl:attribute name="name">         <xsl:value-of select="$fieldName"/>     </xsl:attribute>     <xsl:apply-templates select="field"/> </xsl:element/> 
like image 72
helderdarocha Avatar answered Sep 24 '22 13:09

helderdarocha


These are called Attribute Value Templates. See Here for details w3.org

Definition: In an attribute that is designated as an attribute value template, such as an attribute of a literal result element, an expression can be used by surrounding the expression with curly brackets ({}).

An attribute value template consists of an alternating sequence of fixed parts and variable parts. A variable part consists of an XPath expression enclosed in curly brackets ({}). A fixed part may contain any characters, except that a left curly bracket must be written as {{ and a right curly bracket must be written as }}.

like image 33
PhillyNJ Avatar answered Sep 22 '22 13:09

PhillyNJ