Xplore XSLT with Visual Studio…

Assalamualaykum Wr Br…Today we explore XSLT which stands for XSL stands for eXtensible Stylesheet Language. It is a language used to design and apply style sheets especially for XML documents. Originally the research started to provide style sheet technology to XML using XSL, but finally ended up with three more divisions of XSL. So, XSL now consists of three parts namely XSLT, XPath and XSL-FO.

XSLT is a language for transforming XML documents (even today, several programmers call XSLT as XSL).

XPath is a language to filter, search or sort information available in XML documents.

XSL-FO is a language for formatting XML documents.

In this article we mainly focus on XSLT, which stands for XSL Transformations. Lets begin with simple xml file with its associated xslt as shown below:
simplexml

Please notice that at line 2 in above image we have HREF=”SAMPLE1.XSLT” which illustrates the browser to parse the xml document as per the standards mentioned in xslt file which is shown below:
simplexslt
When I try to open the simple.xml file in browser then it is displayed as:
simpleoutput

Let us explore the xml document first few lines which delivers the mapping with xslt as shown:

<xml:stylesheet type=”text/xsl” href=”sample1.xslt”?>

This is the only magic needed in any XML file to be transformed to any XSLT. The above statement instructs browser to link the current XML document with “sample1.xslt” and present (transform) it accordingly. So, in general we will not see the XML document as a tree structure anymore. It will be presented (transformed) in a different way as specified in “sample1.xslt”.
Now, let us study the most important lines in sample1.xslt.

<xsl: stylesheet version=”1.0″ xmlns:xslt=”http://www.w3.org/1999/XSL/Transform”&gt;

The above line defines that it is basically an XSLT document used for certain transformation.

<xsl:template match=”/”/>

The above line specifies the format (template) applicable to the root of XML document. We can also specify individual templates for each and every XML element as well. The attribute ‘match’ can be provided with any XPath expression.

<xslt: value-of select=”Employees/Employee/Name”/>

And finally, the above line states that it has to get the value (text) present in that path (Employees/Employee/Name) of XML document (tree) starting from root. In this case it starts from root (/), then searches for Employees element (first element), within that Employee child element (first child element) and finally the child element Name (first child element) of Employee element. This path could be replaced with any XPath expression.

Now very interesting part begins now…
Create a new project and add simple.xml and simple1.xslt files to your solution as shown below:
vsxslt
its xslt file in visual studio is shown as:
vsxslt1
Now hard code little code in page load as shown below:
vsxslt2
vsxslt3
Now Execute the code and output is as similar to shown above…hope enjoyed this post…
Jazakallah Khair…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s