How to use XSLT Variables

If you’re not a programmer, you might assume that introducing variable into a markup language is not fair, but variables in XSLT are not much different than entitles in XML. If you can think that way, you should have no trouble with them.


Using Variable Elements

There are two kinds of variable elements’ xsl: variable and xs l: param. Both are allowed at the top level (a global variable (and in individual templates of the style sheet) . An element is considered a top-level element if it is a child of the xs l: style sheet element. Bear in mind a few general rules when developing style sheet variables:

There can’t be more than one top- level variable with the same name and level of importance An XSLT processor will either deliver an error message or choose the variable that it deems most important.

The value of a variable can be any object returned by expressions or the contents of the element that creates the variable (xsl: variable or xsl: param).

Variables are always called by other elements with a $ symbol. This symbol is not used anywhere else within the XSLT vocabulary’s whenever you see a reference to anything with $ as a leading character, within an expression such as < fo:block font-family =” ($fontFami lyvariable)”>, the reference is a variable.

Unlike programming language variables, variable values cannot be changed dynamically by some function within XSL. This can be disconcerting to programmers accustomed to statements such as “a = if x then b else c ‘’, but makes it easier to conditionally change variable values.

When variables aren’t declared in a document’s top level, but rather in a template, the variable is available to all siblings and their descendants, not including the xsi: variable orxsl: param elements themselves.

A variable can be bound tooneofthe four XPath data types: Boolean, string, number, or node set.

A variable can be bound to a data type exclusive of variable elements, such as a result tree fragment. You can only perform string operations on result tree fragments, and you can’t use the /, //, or [ ] operator son them. Any action performed on a variable has the same effect as it would on the equivalent node set.

Result tree fragments can only be returned by variable expressions if the variable is result tree fragments, if they are the results of expression functions that create result tree fragments, or the result of a system property whose result is a result itree fragment.

Binding with the xsl:variable element

The xsl:variable element is the simplest type of variable. It holds a value that can be referenced some where else in a document. An xsl: variable element has one required attribute-name-that you can use to name your variable.

<xsl:variable name= “colorit:>color</xsl:variable>

The element content is the value of the variable. You can reference the variable somewhere else in your document, such as within a template, as follows:

<xsl:template match=”lespommes”>
<xsl:element name=”APPLE”>
<xsl:attribute
name=”($colorit)”>red</xsl:attribute>
</xsl:element>
</xsl:template>

The preceding example creates a new element, called APPLE, and an attribute for that new element. Note how the attribute takes its name from the value of the previous variable. Theis creates an attribute-color-that is addigned a value of red.

The value of the variable doesn’t involve the content of the attribute, however and wont change if you maneuver the various conditional elements, as a programmer might do with JavaScript or Java. Rather, the value of the variable is exactly as you assign it, in this case, color. You could name the value of the variable within the element name that was created instead and use the $color. Variable as the value of the xsl:element elements name attribute.

 


SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________







FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.