CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Doing XSLT Transformation in ASP.Net

By Satheesh Babu
Posted On May 11,2010
Article Rating:
Be first to rate
this article.
No of Comments: 1
Category: ASP.Net
Print this article.

Doing XSLT Transformation in ASP.Net

 

XSLT stands for Extensible Stylesheet Language Transformations. It is technology developed by W3C to convert the XML documents to HTML/XHTML document in order to present it in a browser. We can also use this technology to convert the XML data to a different XML format.

 

XML is one of the widely used data exchange format for variety of reasons (Lets not get into Why XML and its advantages to concentrate on our subject). At times, we need to present the XML data to users in a browser or even convert a XML format into a different format for some application purposes. Seeing the need, W3C answered it with a new transformation technology called as XSLT Transformations.

 

To do the transformation, we need to define a set of transformation rules using XSL (Extensible Stylesheet Language) language. i.e. to convert or transform a xml file to HTML we need to define transformation rules which dictates the XSLT engine to convert a XML node or element to a output format(in HTML/XHTML/ or XML).  Hence, to transform a XML file we need to define xsl file which has some conversion rules. We will understand this clearly in later part of this article.

 

To understand this easily, we will use sample xml file that contains employee information’s. Refer below,

<?xml version="1.0" encoding="utf-8"?>

<Employees>

  <Employee>

    <ID>100</ID>

    <FirstName>Bala</FirstName>

    <LastName>Murugan</LastName>

    <Dept>Production Support</Dept>

  </Employee>

  <Employee>

    <ID>101</ID>

    <FirstName>Peter</FirstName>

    <LastName>Laurence</LastName>

    <Dept>Development</Dept>

  </Employee>

  <Employee>

    <ID>102</ID>

    <FirstName>Rick</FirstName>

    <LastName>Anderson</LastName>

    <Dept>Sales</Dept>

  </Employee>

  <Employee>

    <ID>103</ID>

    <FirstName>Ramesh</FirstName>

    <LastName>Kumar</LastName>

    <Dept>HR</Dept>

  </Employee>

  <Employee>

    <ID>104</ID>

    <FirstName>Katie</FirstName>

    <LastName>Yu</LastName>

    <Dept>Recruitment</Dept>

  </Employee>

  <Employee>

    <ID>105</ID>

    <FirstName>Suresh</FirstName>

    <LastName>Babu</LastName>

    <Dept>Inventory</Dept>

  </Employee>

</Employees>

 

Assume, you want to display the above XML data in a browser (something similar to below figure).

 

To do this, we will first develop a XSL file that has some rules to transform the XML data. XSL language will use XPATH query to navigate through node elements and attributes in XML documents. Also, it includes for-each loops, if statement, etc similar to programming languages to assist the transformations.

The below xsl file will select each employee node and emits the HTML specified for the match. Refer below,

EmployeeXSLT.xslt

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

    <xsl:for-each select="//Employee">

      <div style="border:1px black solid;width:300px;margin:1px">

 

        <div>

          <b>Employee ID:</b> <xsl:value-of select="ID"/>             

        </div>

        <div>

          <b>Name:</b> <xsl:value-of select="FirstName"/><xsl:text> </xsl:text><xsl:value-of select="LastName"/>

        </div>

        <div>

          <b>Department:</b> <xsl:value-of select="Dept"/>

        </div>

 

      </div>

    </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>

 

In the above code, <xsl:template match="/"> will return the root element (Employees) in XML file. The //employee will select all the occurrence of employee node under the root element <Employees> and converts the xml to HTML.

To know more XSL and the syntax refer here.

 

XSLT Transformation using ASP.Net

.Netframework includes a class called XslCompiledTransform in System.Xml.Xsl namespace which can be used to perform the transformation. In specific, the XslCompiledTransform class is packed with a method called XslCompiledTransform.Transform() which can be used to the transformation.

The below code can be used to trasform a xml data from an XML file (Employess.xml) using the XSLT file(EmployeeXSLT.xslt).

Refer the below code,

CodeBehind

protected void Page_Load(object sender, EventArgs e)

    {

        string strXSLTFile = Server.MapPath("EmployeeXSLT.xslt");

        string strXMLFile = Server.MapPath("App_Data/Employess.xml");

 

        XmlReader reader = XmlReader.Create(strXMLFile);

        XslCompiledTransform objXSLTransform = new XslCompiledTransform();

        objXSLTransform.Load(strXSLTFile);

        StringBuilder htmlOutput = new StringBuilder();

        TextWriter htmlWriter = new StringWriter(htmlOutput);

        objXSLTransform.Transform(reader, null, htmlWriter);

        ltRss.Text = htmlOutput.ToString();

        reader.Close();

    }

ASPX

<div>

     <asp:Literal ID="ltRss" runat="server"></asp:Literal>

</div>

 

In the above code, the transformed HTML output is displayed in the browser using a Literal control(ltRss).

 

At times, we will require to transform xml data read from database or from other datasources.  The below code does that,

 




Transforming in-memory XML data

protected void Page_Load(object sender, EventArgs e)

    {

//Assume the strXML is read from a database

        string strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Employees>  <Employee>    <ID>100</ID>    <FirstName>Bala</FirstName>    <LastName>Murugan</LastName>    <Dept>Production Support</Dept>  </Employee>  <Employee>    <ID>101</ID>    <FirstName>Peter</FirstName>    <LastName>Laurence</LastName>    <Dept>Development</Dept>  </Employee>  <Employee>    <ID>102</ID>    <FirstName>Rick</FirstName>    <LastName>Anderson</LastName>    <Dept>Sales</Dept>  </Employee>  <Employee>    <ID>103</ID>    <FirstName>Ramesh</FirstName>    <LastName>Kumar</LastName>    <Dept>HR</Dept>  </Employee>  <Employee>    <ID>104</ID>    <FirstName>Katie</FirstName>    <LastName>Yu</LastName>    <Dept>Recruitment</Dept>  </Employee>  <Employee>    <ID>105</ID>    <FirstName>Suresh</FirstName>    <LastName>Babu</LastName>    <Dept>Inventory</Dept>  </Employee></Employees>";

       

        string strXSLTFile = Server.MapPath("EmployeeXSLT.xslt");         

      

        StringReader stringReader = new StringReader(strXML);

        XmlReader reader = XmlReader.Create(stringReader, null);

   

        XslCompiledTransform objXSLTransform = new XslCompiledTransform();

        objXSLTransform.Load(strXSLTFile);

        StringBuilder htmlOutput = new StringBuilder();

        TextWriter htmlWriter = new StringWriter(htmlOutput);

        objXSLTransform.Transform(reader, null, htmlWriter);

        ltRss.Text = htmlOutput.ToString();       

        reader.Close();

    }

 

Using XML control for XSLT Transformation

There is control in Visual Studio toolbar set called XML. You can use this control to display XML content as it is or to transform a XML file using XSLT transformation.

Refer the below code,

    <form id="form1" runat="server">

    <div>   

        <asp:Xml ID="Xml1" runat="server" DocumentSource="~/App_Data/Employess.xml"

            TransformSource="~/EmployeeXSLT.xslt"></asp:Xml>   

    </div>

    </form>

 

The Xml control has 2 properties, DocumentSource and TransformSource. The first property takes the source Xml document and the former takes XSLT file to do the transformation. Execute the page and see it in action.

 

Downloads

Download Source 

Reference

Learning XSLT

 

Conclusion

XML is one of the heavily used data format in software industry. Any system that interacts with multiple other components will always use XML as underlying communication or data format. The introduction of XSLT is another milestone where the XML can be transformed to a presentable format or convert it to a different XML format to fit the destination’s requirements. Thus, .Netframework has its own support to assist the developers for doing XSLT transformations.

Download the source code attached with article to understand it better.


Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
about XSLT
this is article very useful for me.