Wednesday, January 24, 2007

What ever I know about XSL-T


In the recent days I have been working on XSl.
I have summarized about XSL just in terms of Syntax and usage in this page.

eXtensible Stylesheet Language Transformation:


Its a Stylesheet Programming language which is used to format a html file. We cannot display an XML document as such in a web browser. It has to be formatted. In short we can call this as a XML based Stylesheet language.Its similar but a bit advanced to CSS (Cascading Style Sheet). XSL is bit complex in understanding as it is more of declarative language. Most common belief that a programmer has in his mind will not work with XSL.

For e.g.: Variable is more of a constant here. I will explain it in details in the coming sessions.

Namespace in XSL:

You can have your own namespace declared in the stylesheet or you can use a generalized namespace say “XSL”
Lets see the how it can be declared:

<xn:stylesheet xmlns:xn = “http://www.w3.org/1999/XSL/Transform”></xn:stylesheet>

How to declare a function is xsl:

In XSL functions are called as templates. You can use templates to separate the content and presentation.

Lets see how to declare a template:

<xn:template name="templatename">

</xn:template>


How to call template from the main template:

<xn:call-template name="template"/>


How to pass parameters to template:

<xn:call-template name="template">

<xn:with-param name="paramname" select="param value”/>

</xn:call-template>


How to access the parameters in the template:
<xn:template name="templatename">

<xn:param name="paramname"/>

</xn:template>
Now you have the parameter in your template. If you want to use it you can use it with the parameter name with $ in front of it.


For eg: if you want to get a substring after a new line character in the template. You can do like this
substring-after($paramname,' '& #xa;')


Variables in XSL:
Variables in XSL are bit confusing to the users who work with High Level languages. Here variables are equivalent to “Constants” in most other languages. Once you declare a variable it cannot be modified in future. It can be used as a reference to actual value as in case the value is changed in future, it will reduce the rework time.


How to declare a variable in XSL:
<xn:variable name=”variablename” select=”value”/>

<xn:variable name=”variable”>value</xn:variable>
For-eg:

<xn:variable name=”user” select=”Dhanesh”/>


IF in XSL:
In Xsl if statement can be used to check some particular condition. If doesn’t have an else statement.
How to use if:
<xn:if test="condition”>

</xn:if>


For-e.g.
<xn:if test=”string-length($variable) & gt; 0”)>

<!—do some thing-->

</xn:if>

How to Use If else in XSL:
There is no direct if else statement in XSL. Still we can use another option when, otherwise.
Syntax:

<xn:choose>

<xn:when test="condition ">

<!—do some thing-->

</xn:when><xn:otherwise>

<!—do some thing-->

</xn:otherwise>

</xn:choose>

For Eg:


<xn:choose>

<xn:when test="contains($string,’:’)">

<!—do some thing-->

</xn:when>

<xn:otherwise>

<!—do some thing-->

</xn:otherwise>

</xn:choose>

For each in XSL:
If you have an array of elements or attributes and if you want to iterate with in the array is to use for each.
<xn:for-each select="IteratingArray">

</xn:for-each>

Some function used in XSL:

1) substring-after($variable-name,’delimiter’)
Retrieves the string after the delimiter in the variable


2) substring-before($variable-name,’delimiter’)
Retrieves the string before the delimiter in the variable.


3) contains($variable-name,’delimiter’)
Return if the delimiter is there in the variable or not


4) string-length($variable-name): Return the length of the string.

Please let me know if there is any modification ,Corrections or suggestion in this post .

1 comment:

Anonymous said...

Good One buddy..!!