XML.com logo

  • Style guide
  • Privacy Policy

Setting and Using Variables and Parameters

February 7, 2001

Bob DuCharme

A variable in XSLT has more in common with a variable in algebra than with a variable in a typical programming language. It's a name that represents a value and, within a particular application of a template, it will never represent any other value -- it can't be reset using anything described in the XSLT Recommendation. (Some XSLT processors offer a special extension function to allow the resetting of variables.)

XSLT variables actually have a lot more in common with constants in many programming languages and are used for a similar purpose. If you use the same value multiple times in your stylesheet, and there's a possibility that you'll have to change them all to a different value, it's better to assign that value to a variable and use references to the variable instead. Then, if you need to change the value when re-using the stylesheet, you only change the value assigned in the creation of that variable.

For example, imagine that we want to turn this XML

into this HTML

The following templates would accomplish this (all file names refer to files in this zip file ),

but if you want to change the three font elements' size attribute to "12pt", it would be too easy to miss one -- especially if the template rules weren't next to each other in the stylesheet. The solution is to use a variable to represent this size value:

When referencing a variable or parameter from a literal result element's attribute, you want the XSLT processor to plug in the variable's value. You don't want a dollar sign followed by the variable's name at that point in the template. To do this, put the variable inside curly braces to make it an attribute value template. To plug a variable's value into the content of a result tree element, instead of an attribute value, use an xsl:value-of instruction.

In the example above, if the $bodyTextSize variables were not enclosed by curly braces, each font start-tag in the result would have looked like this: <font size="$bodyTextSize"> .

The xsl:variable instruction creates a variable. Its name attribute identifies the variable's name, and the value can be specified either as the xsl:variable element's contents (like the "10pt" in the example) or as the value of an optional select attribute in the xsl:variable element's start-tag.

The value of the select attribute must be an expression. This offers two nice advantages:

It shows that the xsl:variable element isn't quite as limited as the constants used by popular programming languages because the variable's value doesn't need to be hardcoded when the stylesheet is written.

The attribute value doesn't need curly braces to tell the XSLT processor "this is an attribute value template, evaluate it as an expression," because it always evaluates an xsl:variable element's select attribute value as an expression.

The two xsl:variable elements below have the same effect as the one in the example above: they set the bodyTextSize variable to a value of "10pt". The bodyTextSize variable has its value assigned in a select attribute instead of in its element content; the value assigned will be the return value of a concat function that concatenates the string "pt" to the result of adding $baseFontSize+2 . What's $baseFontSize ? It's another variable, which is defined above the bodyTextSize variable's xsl:variable element. That value of "8" is added to 2 and concatenated to "pt" to create a value of "10pt" for the bodyTextSize variable, which can then be used just like the bodyTextSize variable in the previous example.

The example above demonstrates some of the options available when using an expression in the select attribute to assign a variable's value. The second xsl:variable element references another variable, does some math, and makes a function call. Variables aren't as limited as many XSLT newcomers might think.

It also demonstrates another nice feature of variables: they don't have to be strings. Once baseFontSize is set to "8", the select value of the bodyTextSize variable's xsl:variable element adds "2" to it and comes up with 10. If the XSLT processor had treated these number as strings, putting "8" and "2" together would get us "82". Instead, the XSLT processor treats the baseFontSize variable as a number. It can treat a variable as any type of object that can be returned by an XSLT expression: a string, a number, a boolean value, or a node set. If an XSLT variable has a value assigned by an xsl:variable element's contents and by a select attribute, the XSLT processor uses the one in the select attribute.

The examples above show "top-level" variables. They're defined with xsl:variable elements that are children of the main xsl:stylesheet element, making them global variables that can be referenced anywhere in the stylesheet.

Variables can be "local" as well -- that is, defined inside of a template rule and only available for use within that template rule. For example, the following templates have the same result as the ones in the examples above except that the font start-tag before the result winery element's content has a value of "12pt" in its size attribute instead of "10pt".

The way these templates assign these size values is different. Instead of one global bodyTextSize variable to use throughout the stylesheet, the two template rules each have their own bodyTextSize variables declared between their xsl:template tags. The first one sets bodyTextSize to a value of "10pt", and that's what gets plugged into the size attribute values for the font tags that start the product , year , and price elements. The second template sets bodyTextSize to "12pt", so the winery and grape element contents copied to the result tree by that template start with font tags that have a size value of "12pt":

That's just a toy example. The next stylesheet uses a selection of the string manipulation functions available in XSLT to right align the result tree versions of the color elements in this document.

The fieldWidth global variable stores the desired column width; the goal is to add spaces before each color value so that the spaces plus the color name add up to this value.

The color element's template rule has two local variables:

The value-length variable stores the length of the color name using the string-length() function.

The padding variable stores the number of spaces required to right-align the color name. It does this by subtracting the value of the local valueLength variable from the global fieldWidth variable.

Once the template rule knows how much space it needs to add to the result tree before adding the color element's contents, it adds that many spaces by using the substring() function to pull that many spaces out of a string of spaces passed to the substring() function as its first argument.

In the result, "red" has nine spaces before it, "blue" has eight, and "yellow" has six:

I could have done this without any local variables; in fact, when I originally wrote this stylesheet, I did without them. As with any programming language, using local variables made it easier to break down the problem into pieces and to make the relationship of those pieces easier to understand.

The xsl:param instruction is just like xsl:variable with one important difference: its value is only treated as a default value and can be overridden at runtime. All the stylesheet examples up to this point would work the same way if you substituted xsl:param elements for their xsl:variable elements, but you would have the option of overriding the values when calling their templates.

For example, let's take one of the earlier examples and make the substitution. Here is how it looks as a complete stylesheet:

If we run it as shown with the same source document, it produces the same result as the previous section's version that used xsl:variable instead of xsl:param :

However, if we pass the stylesheet a value of "8pt" to use for bodyTextSize , it substitutes this new value for all uses of this parameter:

Of course, I'm skimming over one important detail here: how do you pass the alternative value for the parameter to the stylesheet? The XSLT Recommendation doesn't tell us. In fact, it deliberately tells us that it's not going to tell us. Just as the W3C's XSL Working Group wanted to leave the potential methods for giving input to and getting output from an XSLT processor as open as possible, they also didn't want to limit how the processors will be told a new value for a global parameter setting. (As we'll see, not all parameters are global like the bodyTextSize one above; they can also be local to template rules.) So, it's up the particular XSLT processor's designer. To pass the new value of "8pt" to the stylesheet when using the Saxon XSLT processor, the command line might look like this:

(It's actually one command split over three lines to fit on the page here. When really using Saxon or any other Java-based XSLT processor, it makes your life easier to store everything before the "xq338.xml" in that command line in a Windows batch file, a UNIX shell script, or your operating system's equivalent. Then you can pass it the important parameters each time you run it with no need to type the full Java library names for the XSLT processor and XML parser.)

The only difference between applying the xq348.xsl stylesheet to the xq338.xml document this way and running it with the bodyTextSize default value is the addition of the "bodyTextSize=8pt" part at the end. Other XSLT processors may require a different syntax when passing a new parameter value along from the command line, but they would still create the same result when using this stylesheet and input.

Local parameters are even more useful in template rules than XSLT local variables are, because the flexibility of passing one or more values to a template lets that template adapt to different situations. Named templates that don't take advantage of this can still operate as functions or subroutines, but when you use named templates that do, you can start treating XSLT like a real programming language. For example, the ability of named templates to call themselves with parameters makes recursion and all the power associated with it possible.

How we pass a new value to a template rule's local parameter isn't quite the open question that it is with global parameters because XSLT provides the xsl:with-param instruction for just this purpose. You can use this element in an xsl:apply-templates element to assign a new value to a parameter in a template being applied, but it's more commonly used when calling a named template with the xsl:call-template instruction. For example, the first template rule in the following stylesheet has a name attribute and not a match attribute. Instead of the XSLT processor looking for nodes where it can apply this template, the processor will wait until the template is explicitly called with an xsl:call-template instruction.

The second and third template rules, which have match patterns of "chapter/title" and "section/title", call the first template by its name of "titles" using xsl:call-templates elements. These xsl:call-templates elements don't need any children, but they have them here: xsl:with-param elements to pass parameter values to the named templates. The "titles" template rule will use these values to override the default value of "h4" when it's called. The with-param instruction in the "chapter/title" template rule is saying "pass along the value 'h1' for the headerElement parameter", and the one in the "section/title" template rule is passing the value 'h2'. For this input document,

the "titles" template is called when the XSLT processor finds each of the two title element nodes. The "titles" named template uses the passed values to create the h1 and h2 elements in the result:

Just as an xsl:param element can specify its default value as either content between its start- and end-tags or as the value of a select attribute, the xsl:with-param element can indicate the value to pass using either method. The two xsl:with-param elements in the example above use the two different methods to demonstrate this.

The XSLT processor evaluates the xsl:with-param element's select value as an expression just like it does with the xsl:param element's select attribute value. This is why the third template above needs single quotation marks around the value of "h2" even though it's also enclosed by double quotation marks. The double quotation marks serve a different purpose: to tell the XML parser where the select attribute value starts and ends. The inner single quotation marks tell the XSLT processor that the value is a literal string and not an expression to evaluate.

You don't have to specify a hardcoded string like "h1" or "h2" as the value of the parameter to pass in an xsl:with-param element. You can put the result of one or more functions in there, or even an XPath expression that retrieves a value from somewhere in the document (or even from another document, using the document() function). This ability opens up an even broader range of possibilities for how you use parameter passing in XSLT.

  • Skip to main content
  • Select language
  • Skip to search

Gecko support

The <xsl:variable> element declares a global or local variable in a stylesheet and gives it a value. Because XSLT permits no side-effects, once the value of the variable has been established, it remains the same until the variable goes out of scope

Required Attributes

Optional attributes.

Top-level or instruction. If it occurs as a top-level element, the variable is global in scope, and can be accessed throughout the document. If it occurs within a template, the variable is local in scope, accessible only within the template in which it appears.

XSLT, section 11.

Document Tags and Contributors

  • XSLT_Reference

404 Not found

404 Not found

  • xsl:variable
  • xsl:where-populated
  • xsl:with-param

xsl:value-of ¶

Evaluates an expression as a string, and outputs its value to the current result tree.

Available in XSLT 1.0 and later versions. Available in all Saxon editions.

  • Category : instruction
  • Content : sequence-constructor
  • Permitted parent elements : any XSLT element whose content model is sequence-constructor ; any literal result element

Attributes ¶

Details ¶.

If the select expression evaluates to a sequence containing more than one item, the result depends on whether a separator attribute is present. If the separator is absent when running in 1.0 mode, then only the first item is considered. When running in 2.0 mode, all the items are output. The separator defaults to a single space if the select attribute is used, or to a zero-length string if a sequence constructor is used.

Links ¶

  • XSLT 2.0 Specification
  • XSLT 3.0 Specification

See also ¶

  • xsl:copy-of

Chapter 8. Working with Variables

XSLT provides the functionality to create special values that can be declared and used by expressions in other elements in the stylesheet. These values are attached, or bound, to named objects called variables. A variable in XSLT can be either global (available throughout the stylesheet) or local (available only to the element where it is declared), and it must be declared before it can be used.

In this chapter, we will discuss the two ways to declare variables in XSLT, using either <xsl:variable> or <xsl:param> , and the process of calling, or referencing, variable values. We will also discuss <xsl:with-param> , which allows a template to override the value of a variable that was previously declared using an <xsl:param> element.

8.1 Declaring and Binding Variables

Variables are declared in an XSLT stylesheet with either the <xsl:variable> element or the <xsl:param> element. Binding a value to a variable is the process of assigning the variable to a value. Therefore, when a value is assigned to a variable, it is said to be bound to that variable. Declaring a variable and binding a value to a variable happen simultaneously. It is not possible to declare a variable without also assigning a value to it. It may help to remember that, when a variable is declared, it refers to the physical location of the <xsl:variable> or <xsl:param> in the stylesheet, and when a variable is bound, it refers to the processor's instantiation of the value of that variable.

Variable values are bound at the point in the stylesheet where they are declared, not where they are called. This means that any evaluation of an expression in a variable uses the context of the variable declaration as its context. In other words, where the variable is declared is very important because the value of the variable is tied to the current context.

Global variables declared with <xsl:variable> are not variable in the true sense of the word. They are bound to a value using the root node as the context, and they keep their assigned value throughout the processing of the stylesheet. Once a global variable that is declared with <xsl:variable> is bound to a value, that variable will remain the same. However, it is possible to declare a local variable of the same name to temporarily override that global variable value.

Global variables that are declared using the <xsl:param> element are variable because they have an initial default value that can be overridden with the <xsl:with-param> element during the processing of a template.

Local variables (declared with either <xsl:variable> or <xsl:param> ) are also variable because they are reinstantiated; that is, their values are reassigned according to the current context during the processing of a template.

8.1.1 The <xsl:variable> Top-Level Element

The <xsl:variable> element is used to declare a variable with a fixed value, and can function both as a top-level element and as an instruction element. When declared as a top-level element, the variable is considered to be global. When declared as an instruction element, it is considered to be local. The <xsl:variable> element accepts two attributes: the name attribute, which is required, and the select attribute, which is optional. The following element model definition shows the content of the <xsl:variable> element as a template, but the content is not allowed if the select attribute contains an expression:

The required name attribute of the <xsl:variable> element is used to identify the variable and takes the form of a QName. A variable reference uses the same name to retrieve the value of the variable (see Section 8.3 for more information on variable references).

The optional select attribute contains an expression and is used to bind the value of the variable to the object that is the result of evaluating the expression. If the select attribute is used, the content of the <xsl:variable> element must be empty.

If there is no select attribute, the content of the <xsl:variable> element is a template, and the value bound to the variable is the result of instantiating that template. Note that the result of instantiating a template in an <xsl:variable> element is a result tree fragment, or RTF (see Section 8.2 for more information on result tree fragments).

The value bound to a variable is available to the expressions in other elements based on where it is declared. When declared within an <xsl:template> element at the instruction element level, it is a variable local to that context only, and it is only available to the functions and elements that follow it, within that template. In other words, the value of that variable is only valid inside the realm of the template where it is declared, and is only available to elements that occur after its declaration. On the other hand, if a variable is declared with an <xsl:variable> as a top-level element, the value of the variable is available globally to the remaining elements in the stylesheet following its declaration.

8.1.2 The <xsl:param> Top-Level Element

The <xsl:param> element is used to declare a variable with a default value. It accepts two attributes: the name attribute, which is required, and the select attribute, which is optional. The content of the <xsl:param> element is a template, but the content is not allowed if the select attribute contains an expression.

The value of the variable declared using the <xsl:param> element is only a default value, which is used unless another explicit value is supplied before the variable is referenced, or called. This default value can be modified prior to use by the <xsl:with-param> element, which is discussed in the following section.

Although not explicitly categorized as an instruction element in the element model definition shown below, the <xsl:param> element is allowed at the same level as instruction elements. [1] However, when used inside a template, it must occur before any other elements in the template. When declared as a top-level element, the variable is considered to be global; when declared at the instruction level, the variable is considered to be local.

The required name attribute of the <xsl:param> element is used to identify the variable and takes the form of a QName. A variable reference uses the same name to retrieve the value of the variable (see Section 8.3).

The optional select attribute contains an expression and is used to bind the value of the variable to the object that is the result of evaluating the expression. If the select attribute is used, the content of the <xsl:param> element must be empty.

If there is no select attribute, the content of the <xsl:param> element is a template, and the value bound to the variable is the result of instantiating that template. Note that the result of instantiating the template in an <xsl:param> element is an RTF (see Section 8.2).

The value bound to the variable is available to the expressions in other elements based on where it is declared. When declared within an <xsl:param> element at the instruction element level, it is a variable local to that context only, and only available to the functions and elements that follow it. In other words, the value of that variable is only valid inside the realm of the template where it is declared, and is only available to elements that occur after its declaration. On the other hand, if the variable is declared with an <xsl:param> as a top-level element, the value of the variable is available globally to the remaining elements in the stylesheet following its declaration.

8.1.3 The <xsl:with-param> Element

The <xsl:with-param> element is used to override the value of a variable declared with <xsl:param> . It has the same basic model, shown in the element model definition below, as <xsl:variable> and <xsl:param> . However, a variable must be declared using <xsl:param> prior to using <xsl:with-param> to override it.

The required name attribute of the <xsl:with-param> element is used to identify the variable and takes the form of a QName. A variable reference uses the same name to retrieve the value of the variable (see Section 8.3). Note that a variable with the same name must have been declared using <xsl:param> prior to using this element.

The optional select attribute contains an expression and is used to bind the value of the variable to the object that is the result of evaluating the expression. If the select attribute is used, the content of the <xsl:with-param> element must be empty.

If there is no select attribute, the content of the <xsl:with-param> element is a template, and the value bound to the variable is the result of instantiating that template. Note that the result of instantiating the template in an <xsl:with-param> element is an RTF (see Section 8.2).

Using <xsl:with-param> makes the value of a parameter more flexible. After you have declared a variable with a default value using <xsl:param> , you can use the <xsl:with-param> element to reset that value within the current template's context. In other words, <xsl:with-param> gives a context-specific value for the variable, which affects only the template being called.

The <xsl:with-param> element is neither a top-level element nor an instruction element. It can only be used within the <xsl:call-template> element or the <xsl:apply-templates> elements. The template called or applied using either <xsl:call-template> or <xsl:apply-templates> is therefore the only context that uses the new value of the variable declared with <xsl:with-param> . The original value for the variable declared with <xsl:param> is overridden for the context of the template being called. It is important to note that <xsl:with-param> can only be used to affect the values of variables already declared using <xsl:param> . If there is no declaration of the original variable in place for a template, it is ignored.

8.2 Result Tree Fragments

When a variable declaration element contains a template, the content of the template is instantiated and the resulting object is an RTF. This creates a new kind of object, different from the four standard object types (node-set, string, Boolean, and number). An RTF is a node-set that contains one node, whose children are the nodes that are produced by the instruction elements in the template.

Although an RTF is technically a node-set, there are restrictions on how this new type of node-set can be accessed or used. The way in which the variable is referenced in subsequent template rules determines how that RTF will be written to the actual output result tree. If the RTF is output using <xsl:copy-of> , the nodes in the RTF are sent to the output, including the tags and text content. Otherwise, the output will be the text content of the RTF, without any markup.

When a variable is bound to an RTF, a portion of the actual output result tree is being bound. Additional processing on RTFs resulting from variables is allowed, but not all functions are available. Only functions that can be applied to strings are allowed on RTFs. For example, using count() on an RTF will return an error. Operations that involve patterns and predicates are not allowed. Also, an RTF cannot be further processed using any / , // , or [] tokens in it in other words, no absolute location path tokens, no descendant axis abbreviations, and no predicates. Otherwise, operations performed of the type permitted on strings, when performed on RTFs, behave just as they would on any other equivalent node-set.

Example 8-1 shows a variable that contains a template with three LREs, each containing an instruction element.

Example 8-1 Variable declaration with template content.

The result of instantiating the variable is a node-set containing the three <num> nodes. Using <xsl:copy-of select="$numvar"> returns the nodes and their content:

The value of the node-set, when extracted using <xsl:value-of select="$numvar"> , is the string value of a concatenation of its children; in this case, the string is " 123 ." However, using a node-set function like <xsl:value-of select="count($numvar)"> is an error because node-set operations on RTFs are not allowed.

In an example using Markup City, it is possible to store nodes from the input in a variable as follows:

This example stores all the <block> elements and their text children in the variable as a single node-set. The value of the new node-set can then be extracted using <xsl:value-of select="$blocks"> , which returns the concatenation of the text in each block.

Using <xsl:copy-of select="$blocks"> retrieves the entire set of <block> nodes, including their children text nodes, as shown in Example 8-2.

Example 8-2 Extracting a node-set in a variable using <xsl:copy-of> .

The template inside the <xsl:variable> element shown in this example is retrieved when the variable is referenced, or called, using the variable reference, $blocks .

8.3 Using Variable References

Regardless of how a variable is declared, either with <xsl:variable> or <xsl:param> , or if it is overridden with <xsl:with-param> , the value bound to the variable is accessed using a variable reference. [2] A variable reference is the mechanism used to retrieve the value of a variable. Variable reference takes the form $variable-name , where "variable-name" is the name of the variable declared using the name attribute on the declaring element. Note that this means the variable name is also by definition a QName.

The template rule in Example 8-3 declares a variable named myparent and gets its value using the name() function. The value of the variable is output with the $myparent variable reference in the <xsl:value-of> element.

Example 8-3 Declaration of a variable with <xsl:variable> .

The result of this template is to output the name of the parent for each <block> element. Variable references can only be used in the context of expressions. However, you cannot use a variable reference directly in a match or use attribute. Some processors do allow variable references inside the predicate of an expression within the match or use attribute. For example, in XT, it is not valid to use $num in a match for the template rule <xsl:template match="$num"/> , but it is valid in the predicate of match for the template rule <xsl:template match="//*[$num]"/> . If the value of $num is 3, the template rule will match every element that has a position of 3. While this functionality is available with some processors, it is not guaranteed to work with all or future versions of processors.

8.3.1 Local vs. Global Variables

The value of a variable is bound to the variable within the context of the declaration. In other words, the value is determined based on where the variable is declared. The value is available to the elements that occur after the declaration has been made, but only within the same context as the declaration. However, if the variable is declared using a top-level element, it is considered a global variable and its value is bound based on the root node as a context. Global variables are available to all elements and their children, regardless of where reference to the variable occurs in the stylesheet.

If a variable is declared within the context of a template rule, that variable is a local variable and is only available to the contents of that template rule. Local variables do not pass on their value to any templates called by the template rule with <xsl:apply-templates> , <xsl:call-template> , or <xsl:apply-imports> . However, variable values can be passed to templates using the <xsl:with-param> element in either <xsl:apply-templates> or <xsl:call-template> .

Example 8-4 shows a template matching on <parkway> that contains a local variable. An additional template matching on <block> tries to use the same variable, but cannot, because the value of the local variable is not defined in the context of <block> .

Example 8-4 Using local variables incorrectly.

Using this example would produce an error because the variable num is declared as a local variable in the context of <parkway> . The template matching on block cannot "see" the value of the variable. Declaring the variable at the top level as a global variable would resolve this problem, as shown in Example 8-5.

Example 8-5 Using global variables.

8.3.2 duplicate declarations (shadowing).

When a value is bound to a variable, the value of the variable becomes static, or fixed, within the context of its binding. The value of a variable can only be changed after it has been bound by rebinding the value with a new declaration or, if it was originally declared using the <xsl:param> element, by using the <xsl:with-param> element to pass a new variable value in its place. Declaring a variable that is a duplicate of a previously declared variable is called shadowing.

According to the XSLT specification, shadowing at the same level by declaring two variables with the same name is not allowed. If this occurs, the results vary depending on the processor used. Some processors may return an error, while others may use the last variable declared. Declaring variables with the same name in different contexts, however, is allowed.

Declaring a local variable with the same name as one that was previously declared at the top level is valid. A local variable with the same name as a global variable temporarily overrides the value of the global variable within the context of the local declaration of the variable. Example 8-6 demonstrates the order of precedence for variables with duplicate names.

Example 8-6 Order of precedence for variables with duplicate names.

In this stylesheet, the variable named num is defined three times, two at the top, or the global, level and once locally within the context of the <parkway> element. The resulting value of the variable is 2 in the global sense, but 3 when it is redeclared as a local attribute inside the template rule for <parkway> .

Some processors will simply ignore the first <xsl:variable> declaration (the one with a value of 1). Other processors will generate an error when any shadowing in the same context occurs.

Variable values declared locally in a template are not inherited by the template rules for the children of the element. So, the template rule for <block> would use the global value of 2, not the local value of 3.

Using our Markup City, Example 8-7 shows a variable declaration that returns a different value based on which element is the parent of the current block.

Example 8-7 Context based <xsl:variable> declaration.

Based on the context, the value of the variable myparent is different depending on which <block> is being processed. Using the name() function with two dots .. (the abbreviation for the parent) in the variable declaration <xsl:variable name="myparent" select="name(..)" /> binds the name of the parent of the context node to the variable. The variable is referenced using <xsl:value-of select="$myparent"/> . A little creative use of <xsl:text> puts in line breaks and the colon, and <xsl:value-of select="text()"/> pulls out the text nodes in the blocks (notice that the first template rule, <xsl:template match="text()"/> , is used to suppress all the text nodes initially). If the context node is a <block> in the <thoroughfare> , the value of the variable is thoroughfare . If the context node is a <block> in the <boulevard> , the value of the variable is boulevard . The local variable myparent is being reinstantiated for each <block> element that it encounters.

8.3.3 Using the <xsl:with-param> Instruction Element

The <xsl:with-param> element can be used to change the value of a variable used in a template rule by passing a new value for that variable from another template rule. The variable that is being passed in must have been previously declared in the context of the template being called, or the new variable will just be ignored. This allows parent templates to pass values into templates for children elements. Recall that variables are limited to the scope of the context where they are declared, so simply declaring the variable in the parent template of an element does not make that variable available to the child element's template.

For example, suppose we wanted to divide up our city by precinct based on the kind of street, either a thoroughfare or a boulevard . We could change the value of the " precinct" variable defined in the <block> template using an <xsl:with-param> element inside an <xsl:apply-templates> element, as shown below.

In this case, the <block> s that are in <thoroughfare> s would use the "precinct" value 5, but the <block> s in any other context would use the default value declared in the <block> template rule, 4. Normally, templates would not be able to pass the value of a variable to other templates, but <xsl:with-param> allows this to happen. Example 8-8 shows a complete stylesheet.

8.4 Comparing <xsl:variable> and <xsl:param>

Syntactically, there is no difference between <xsl:variable> and <xsl:param> . The content model of both elements is the same. The way these elements declare variables is the same. However, the value of the variable declared using <xsl:param> is only a default that can be changed with the <xsl:with-param> element, while the <xsl:variable> value cannot be changed. The only other difference is that there is a restriction on where the <xsl:param> element can be used. It must always come before any other elements if used within a template rule.

Because <xsl:variable> and <xsl:param> declare variables in the same way, if they are both present in the same context and they both have the same name, they are considered duplicates and result in an error. Duplicate declarations, or shadowing, are discussed in Section 8.3.2.

Using <xsl:param> together with <xsl:variable> raises certain contextual restrictions on syntax. When a variable and parameter are both declared in a template, the <xsl:param> must come first.

8.5 Comparing <xsl:with-param> to <xsl:param> and <xsl:variable>

The <xsl:with-param> element has the same content model and declares a variable in the same manner as <xsl:variable> and <xsl:param> . However, <xsl:with-param> must be used inside either <xsl:apply-templates> or <xsl:call-template> , and using it replaces the value of the variable that was declared using the original <xsl:param> . If there is no <xsl:param> declaration in the template being called, the variable is ignored. The <xsl:with-param> element cannot be used to change the value of a variable declared with <xsl:variable> .

Example 8-8 Extended example using <xsl:param> and <xsl:with-param> .

[1] Technically, <xsl:param> is not a true "instruction element" even though it is allowed at the same level as instruction elements. Instruction elements are executed and replaced by the RTFs that they create, while an <xsl:param> element only defines a default value.

[2] Although variables are defined in XSLT, variable references are the realm of XPath because they are used in expressions.

XSLT and XPATH(c) A Guide to XML Transformations

  • Why Project Risk Management?
  • Managing Project Constraints and Documenting Risks
  • Quantifying and Analyzing Activity Risks
  • Managing Activity Risks
  • Managing Project Risk
  • Setting Environment Variables
  • Converting Between Date-and-Time Values and Seconds
  • Using Patterns to Match Broad Content Types
  • The Effect of Record Deletions on Sequence Generation
  • C.4. Python Resources
  • Objective 6. Find Files and Folders
  • Problem Solving
  • You and GO!
  • Business Running Case
  • Objective 5. Add Records to a Table
  • Troubleshooting AAA
  • Creating a Typography Workspace
  • Going with the Flow
  • When to Track
  • Numbered Lists
  • Sequential Styles
  • Using Abstract Classes and Interfaces
  • Using Inner Classes
  • Using the ArrayList Class
  • Using Regular Expressions
  • Creating Servlets

Primary links

  • HTML Color Chart
  • Knowledge Base
  • Devguru Resume
  • Testimonials
  • Privacy Policy

Font-size: A A A

XSLT » Elements » xsl:variable

The xsl:variable element is used to declare a local or global variable and to give that variable a name and a value. The value can be assigned by either the content of the xsl:variable element or by the select attribute, but not by both. Each variable declaration requires a separate xsl:variable element. Global variables are declared in the top level of the style sheet (as children of the xsl:stylesheet or xsl:transform elements). Local variables are declared within the template body.

Note that once you set a variable's value, there is no provision in the XSLT language to change or modify that value. This is in sharp contrast to most other computer languages which allow you to repeatedly reassign variable values. The xsl:param element can also be used to declare parameters. The only real difference between a variable and a parameter is how the value is assigned. Like all XSLT elements, the xsl:variable element must be closed (well-formed). If the select attribute is present, then this element is self-closing. If the select attribute is not present, then this element is not self-closing and the separate closing element is mandatory.

Explanation:

Here, we create a reuseable table header. This is the code for xslt_example_variable.xsl. We use the DevGuru Staff List XML file for our example with the following header: <?xml-stylesheet type="text/xsl" href="xslt_example_variable.xsl"?>

Language(s): XSLT

  • xsl:for-each
  • xsl:with-param
  • Example Files

xsl assign value of to variable

© Copyright 1999-2018 by Infinite Software Solutions, Inc. All rights reserved.

Trademark Information | Privacy Policy

Event Management Software and Association Management Software

If the select attribute is present, the <xsl:variable> element cannot contain any content. If the select attribute contains a literal string, the string must be within quotes. The following two examples assign the value "red" to the variable "color":

If the <xsl:variable> element only contains a name attribute, and there is no content, then the value of the variable is an empty string:

The following example adds a value to the variable "header" by the content of the <xsl:variable> element:

Learn XML with <oXygen/> XML Editor - Free Trial!

XML Tutorial

Xpath tutorial, xslt tutorial, xquery tutorial, xsd data types, web services, xslt elements reference.

The XSLT elements from the W3C Recommendation (XSLT Version 1.0).

XSLT Elements

The links in the "Element" column point to attributes and more useful information about each specific element.

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

  • Developing Applications with Oracle Visual Builder
  • Develop Applications
  • Work with JavaScript Action Chains
  • Built-In Actions

Add an Assign Variable Action

You use an Assign Variable action to assign a local, page, flow, or application variable a value. This action can also be used to create a local variable.

For example, if your action chain sends a request to a GET endpoint, you can use the Assign Variable action to map the response to a page variable that's bound to a page component. Or, suppose you want to capture the ID of an item selected in a list. You could use a Selection event to start an action chain that assigns the selected item’s ID to a variable.

To use an Assign Variable action to create a local variable:

  • Enter the variable's name in the Variable field and hit Enter on your keyboard.
  • Use the Type drop-down to select its data type.

Description of jsac-assign-new-local-var.png follows

To use an Assign Variable action for a value assignment:

  • Drag the action from the Actions palette onto the canvas, dropping the action either at the bottom edge of the action it is to follow, or at the top edge of the action it is to precede.
  • Double-click the action in the Actions palette to add it to an empty canvas or to the end of an action chain.
  • On the canvas, select the action you want the new action to follow, then double-click the new action in the Actions palette.

Description of jsac-assign-variables-action.jpg follows

  • To set the variable's value, hover over the far-right side of the Value property and either click the down arrow to choose the value, or click fx to create an expression for the value.

Description of jsac-assign-another-var.png follows

Use Filter Builder to Create Filter Criteria for an SDP

If you're using an SDP to provide a table or list's data, and you'd like to filter out rows, you can use the Assign Variable action to create and assign the filter criteria to the SDP's filterCriterion property. For further details about using an SDP to filter a table or list's rows, see Filter Data by Filter Criteria .

Description of jsac-assign-action-pick-sdp.png follows

To use the Assign Variable action's Filter Builder to create the filter criterion for an SDP:

Description of jsac-assign-action-filter-builder-link.png follows

  • For the first Attribute textbox, enter the name of the column (field in record, like "city") that you want compared against a specific value (like "Tokyo").
  • For the Operator drop-down list, select the operator for the criterion.

Description of jsac-assign-action-filter-builder.png follows

  • Click Done when you're finished.

Filter Builder's Code Editor

You can use the Filter Builder's Code tab to view and edit the filter's code. After defining a condition on the Builder tab, you will see that the Code tab contains an attribute , op and value property.

Here's an example of a filter with two conditions combined by an AND operator:

  • The Oracle JET operator is " $eq " (it must include the dollar sign (“ $ ”)).
  • The attribute property is set to the name of the field (column) that you want to be evaluated against the value property.
  • The value property ( $page.variables.customerListSDP.filterCriterion.criteria[0].value ) is mapped to a page variable ( $page.variables.filterVar ) that holds the value to be evaluated against each field (column) value.

IMAGES

  1. xml

    xsl assign value of to variable

  2. [Solved] Declaring a xsl variable and assigning value to

    xsl assign value of to variable

  3. ️ Xsl variable assign value. How to add two variables in XSLT (XML

    xsl assign value of to variable

  4. XSLT Tutorial

    xsl assign value of to variable

  5. [Solved] Print out value of XSL variable using

    xsl assign value of to variable

  6. Introduction to XSL With Examples

    xsl assign value of to variable

VIDEO

  1. 305 Odds Ratio : Confidence interval and p-value in Excel and R

  2. Which valuation method gives the highest property value? 🤔

  3. 5 Surprising Hurdles Every Newbie Guitarist Faces and How to Conquer Them

  4. assign value to javascript variables ||#google #html #coding #javascript #vairal #programing

  5. Python Programming: Setting the Data Type

  6. String variable in C Language || strcpy() function in c || Learn C language in Urdu || Hindi

COMMENTS

  1. xslt

    You use the xsl:variable statement to create a variable. Either of the following will work <xsl:variable name="cdtitle"><xsl:value-of select="title"/></xsl:variable> <xsl:variable name="cdtitle" select="title"/> They statement in this case would have to be within the loop.

  2. xsl:variable

    Tip: You can add a value to a variable by the content of the <xsl:variable> element OR by the select attribute! Syntax <xsl:variable name="name" select="expression"> <!-- Content:template --> </xsl:variable> Attributes Example 1 If the select attribute is present, the <xsl:variable> element cannot contain any content.

  3. <xsl:variable>

    <xsl:variable> The <xsl:variable> element declares a global or local variable in a stylesheet and gives it a value. Because XSLT permits no side-effects, once the value of the variable has been established, it remains the same until the variable goes out of scope Syntax xml <xsl:variable name=NAME select=EXPRESSION > TEMPLATE </xsl:variable>

  4. Setting and Using Variables and Parameters

    The xsl:variable instruction creates a variable. Its name attribute identifies the variable's name, and the value can be specified either as the xsl:variable element's contents (like the "10pt" in the example) or as the value of an optional select attribute in the xsl:variable element's start-tag.

  5. variable

    The xsl:variable element declares a global or local variable in a stylesheet and gives it a value. Because XSLT permits no side-effects, once the value of the variable has been established, it remains the same until the variable goes out of scope

  6. <xsl:variable> / <xsl:param>

    <xsl:for-each select="/foo/bar"> <xsl:variable name="some-bar" select="."/> <xsl:value-of select="$some-bar"/> </xsl:for-each> Parameters are assigned a value either from the process that invoked the stylesheet (top-level parameter), or from a <xsl:with-param> or from a default value (in which case it behaves as if it was a variable).

  7. XSLT Variable

    To get the value or to output the above statement we can do it as follows: <xsl:value-of select="$animal"/> Mostly we often prefer to use <xsl: variable> in top-level to avoid recalculation while implementation.

  8. xsl:variable ⚡️ XSLT 3.1 with examples

    xsl:variable Used to declare a variable and give it a value. If it appears at the top level (immediately within xsl:stylesheet) it declares a global variable, otherwise it declares a local variable that is visible only within the stylesheet element containing the xsl:variable declaration.

  9. Setting and Using Variables and Parameters

    The xsl:variable instruction creates a variable. Its name attribute identifies the variable's name, and the value can be specified either as the xsl:variable element's contents (like the "10pt" in the example) either as that value of an optional select attribute in who xsl:variable element's start-tag.

  10. xslt

    1 Answer Sorted by: 17 You use the xsl:variable statement go create ampere variable. Is for aforementioned tracking will work <xsl:variable name="cdtitle"><xsl:value-of select="title"/></xsl:variable> <xsl:variable name="cdtitle" select="title"/> They statement in this case would will to be in the cling.

  11. Conditional assignment in XSLT?

    4 IMHO your code is fine. The "problem" with XSLT variables is that they are NOT variables but more resemble constants: once set they cannot be changed anymore. Also, their scope is restricted to anything that follows their definition until the closing tag. In particular they are NOT visible in any ancestor tag.

  12. xsl:value-of ⚡️ XSLT 3.1 with examples

    Here are some examples of expressions that can be used in the select attribute: Expression. Value. TITLE. The character content of the first child TITLE element if there is one. @NAME. The value of the NAME attribute of the current element if there is one. .

  13. Chapter 8. Working with Variables

    Binding a value to a variable is the process of assigning the variable to a value. Therefore, when a value is assigned to a variable, it is said to be bound to that variable. Declaring a variable and binding a value to a variable happen simultaneously. It is not possible to declare a variable without also assigning a value to it.

  14. XSLT >> Elements >> xsl:variable

    The value can be assigned by either the content of the xsl:variable element or by the select attribute, but not by both. Each variable declaration requires a separate xsl:variable element. Global variables are declared in the top level of the style sheet (as children of the xsl:stylesheet or xsl:transform elements).

  15. How to: change the value of the global variable in XSLT

    Answers. XSLT is not an imperative programming language where you can change the value of a variable. If you want to pass a value from one template to another then you can use a parameter e.g. The fact that you need to modify an <xsl:variable> shows that you are not thinking in XSLT.

  16. XSLT <xsl:variable> Element

    The <xsl:variable> element is used to declare a local or global variable. ... Once you have set a variable's value, you cannot change or modify that value! Tip: You can add a value to a variable by the content of the ... the string must be within quotes. The following two examples assign the value "red" to the variable "color": <xsl:variable ...

  17. Assigning a string to a variable depending on condition in xslt

    XSLT talks of "binding" a variable to a value. The difference between "bind" and "assign" is that a variable is bound to a value as soon as it is declared and remains bound to the same value for as long as it is in scope. Of course, the value that you bind it to can be determined by a conditional expression evaluated at run time. -

  18. XSLT Reference

    XSLT Elements. The links in the "Element" column point to attributes and more useful information about each specific element. ... Extracts the value of a selected node: variable: Declares a local or global variable: when: Specifies an action for the <choose> element: with-param: Defines the value of a parameter to be passed into a template ...

  19. Add an Assign Variable Action

    To use an Assign Variable action to create a local variable: Enter the variable's name in the Variable field and hit Enter on your keyboard. Use the Type drop-down to select its data type. If necessary, use the Value field to assign it a value. Description of the illustration jsac-assign-new-local-var.png. To use an Assign Variable action for a ...

  20. Setting a value in a variable on if conditions using XSL

    2 Answers Sorted by: 15 Variables are immutable in XSLT, and cannot be changed once set. The variable declarations in the xsl:choose are simply new declarations that at local in scope to the current block. (They are said to "shadow" the initial variable). What you need to do is this...

  21. struct

    before I though I can not assign whole value to a struct variable, bellow is actually wrong: struct Student student1; student1 = {"Andy", 18}; but can only. ... assign value to member of struct pointer, within multiple struct pointers and arrays. 1 C - trying to assign return value from a function to a variable causes segmentation fault ...

  22. What is the best way to ignore elements in a list assignment?

    Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams