Variables in XSLT
<xsl:variable> element is used to declare variables in XSLT.
After declaring a variable, you can assign the required values to it. To access the value stored in a variable, prefix the variable name with the $ character.
Creating Variables
Variables in XSLT are categorized as local and global variables.
Global variables are created using the <xsl:variable> element
Local variables are declared within the body of a particular template, within the <xsl:template> element. These variables can be accessed only within the template in which they were declared.
syntax for the <xsl:variable> element is
<xsl:variable name = Qname select =Expression as = sequence-type >
template body
</xsl:variable>
name: Represents the name of a variable, and is a qualified name such as the XML name. The qualified name can have the name of the namespace as a prefix.
select: Holds the value of a variable or an expression. This is an optional attribute.
For example, the following is the syntax to assign the value returned to a variable:
<xsl:variable name="CameraName" select=" 'XYZ' "/>
In the above syntax, a variable, CameraName, is declared, and the value, XYZ, is assigned to the variable.
Example:
Camera.xml File
<?xml version="1.0"?>
<Camera>
<CameraName>XYZ</CameraName>
<Price type=USD>75</Price>
</Camera>
Camera.xsl Stylesheet
the code of an XSLT file that uses variables to retrieve and display information from the Camera.xml file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Camera">
<xsl:variable name="cname" select="CameraName"/>
<xsl:variable name="cprice" select="Price"/>
</xsl:template>
</xsl:stylesheet>
the CameraName and Price information of the Camera.xml file are selected and assigned to the variables, cname and cprice, respectively. The select attribute is used to assign the values.
The select attribute is optional, and if not present, the value of the variable is the data within the <xsl:variable> element.
<xsl:variable name="BookName"> Science </xsl:variable>
syntax to declare a variable of the integer datatype
<xsl:variable name="Marks" as="xs:integer*" select="90"/>
In the above syntax, a variable, Marks, of the integer datatype is declared, and the value, 90, is assigned to it.
Accessing Variables
To access the value stored in a variable, use the $ character.
$Marks
Note
In XSLT, you cannot change the value of the variable dynamically.
Cameras.xml File
<?xml version="1.0"?>
<CameraInformation>
<Camera>
<CameraName>XYZ</CameraName>
<Price type="USD">75</Price>
</Camera>
<Camera>
<CameraName>PQR</CameraName>
<Price type="USD">80</Price>
</Camera>
<Camera>
<CameraName>HIJ</CameraName>
<Price type="USD">85</Price>
</Camera>
</CameraInformation> Cameras.xsl File
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<CAMInformation>
<xsl:apply-templates/>
</CAMInformation>
</xsl:template>
<xsl:template match="Camera">
<xsl:variable name="cname" select="CameraName"/>
<xsl:variable name="cprice" select="Price"/>
<xsl:value-of select="$cname"/> <xsl:value-of select="$cprice"/>
</xsl:template>
</xsl:stylesheet>
two variables, cname and cprice, are created and the values of CameraName and Price are stored in these variables. To display these values in the resultant document, use the $ character.
No comments:
Post a Comment