XSLT call method from .net - example


XSLT Abstract

This specification defines the syntax and semantics of XSLT, which is a language for transforming XML documents into other XML documents.


XSLT is designed for use as part of XSL, which is a stylesheet language for XML. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.


XSLT is also designed to be used independently of XSL. However, XSLT is not intended as a completely general-purpose XML transformation language. Rather it is designed primarily for the kinds of transformations that are needed when XSLT is used as part of XSL.


Review detail from http://www.w3.org/TR/xslt




XML example:


<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy?-->
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <company>CBS Records</company>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>




XSLT example:


<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy?-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myUtils="pda:MyUtils">
    <xsl:param name="testparm"/>
    <xsl:template match="/">
                <h2>My CD Collection</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Call method</th>
                    <xsl:for-each select="catalog/cd">
                                <xsl:value-of select="title"/>
                                <xsl:value-of select="$testparm"/>
                                <xsl:value-of select="myUtils:FormatName(title,artist)"/>


.net aspx.cs example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;


public partial class _Default : System.Web.UI.Page
    private const string filename = @"C:\test.xml";
    private const string stylesheet = @"C:\test.xslt";


    protected void Page_Load(object sender, EventArgs e)
    private void Test()
        XmlDocument xdc = new XmlDocument();


        XsltArgumentList xslArg = new XsltArgumentList();
        xslArg.AddParam("testparm", "", "weather");


        xslArg.AddExtensionObject("pda:MyUtils", new _Default());


        // setup the transform, for onrender
        XslTransform xslt = new XslTransform();
        this.xmlTest.Document = xdc;
        xmlTest.TransformArgumentList = xslArg;
        xmlTest.Transform = xslt;
    public string FormatName(string name, string name2)
        return name + "-" + name2;


.net aspx example:


<html ><head runat="server">
    <form id="form1" runat="server">
    <asp:Xml id="xmlTest" runat="server"></asp:Xml>


Download : XSLT_CallMethod_Example.zip (2.87 kb)



分享到: 更多