Server‑side transformation

ASP.NET WebForms + XSLT Transformation

Workflow

  1. The C# backend fetches XML string (student data) from the database
  2. Load the XML string into MemoryStream and create an XPathDocument
  3. Read the student.xsl stylesheet from the physical path within the project
  4. Perform XSLT transformation with XslCompiledTransform to output an HTML string
  5. Pass the generated HTML to the frontend page for rendering (you can implement this yourself. XSL is not limited to web scenarios; it can also serve as a template engine)

Prepare your full‑format XML on your own

student.xsl File

Place this file inside your project

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Set output to HTML with UTF‑8 encoding to fix Chinese garbled characters -->
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <meta charset="UTF-8"/>
                <title>Student List</title>
            </head>
            <body>
                <ul>
                    <!-- Path matches XML structure: Root → StudentDB → Student -->
                    <xsl:for-each select="StudentDB/Student">
                        <li>
                            No:<xsl:value-of select="position()"/>
                            Name:<xsl:value-of select="Name"/>
                        </li>
                    </xsl:for-each>
                </ul>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>Code language: HTML, XML (xml)

C# Transformation Code

using System;
using System.Text;
using System.IO;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace XslDemo.Web
{
    public partial class Default : System.Web.UI.Page
    {
        protected string studentHtml = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //1. Get XML string returned from database via BLL layer
                string xml = new BLL.student().GetAllListXml();
                byte[] arr = Encoding.UTF8.GetBytes(xml);
                using (MemoryStream stream = new MemoryStream(arr))
                {
                    XPathDocument xpd = new XPathDocument(stream);
                    string xslPath = Context.Server.MapPath("/student.xsl");

                    // Check whether the XSL file exists
                    if (!File.Exists(xslPath))
                    {
                        studentHtml = "XSL stylesheet file not found!";
                        return;
                    }

                    XslCompiledTransform tran = new XslCompiledTransform();
                    tran.Load(xslPath);

                    StringBuilder sb = new StringBuilder();
                    tran.Transform(xpd, null, new StringWriter(sb));

                    // 【Critical】Assign result to page variable
                    studentHtml = sb.ToString();
                }
            }
        }
    }
}Code language: C# (cs)

Use placeholder replacement if you want output rendered in aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XslDemo.Web.Default" %>
<!DOCTYPE html>
<html>
<body>
    <%--Output HTML generated from XSLT transformation--%>
    <%=studentHtml %>
</body>
</html>Code language: HTML, XML (xml)

Server‑side transformation

Previous:

Leave a Reply

Your email address will not be published. Required fields are marked *