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>
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!
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.
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.
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 } .
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/>
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 }}.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With