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?-->
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>

 

</catalog>

 

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="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Title</th>
                        <th>Parmeter</th>
                        <th>Call method</th>
                    </tr>
                    <xsl:for-each select="catalog/cd">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td>
                                <xsl:value-of select="$testparm"/>
                            </td>
                            <td>
                                <xsl:value-of select="myUtils:FormatName(title,artist)"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

 

.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)
    {
        Test();
    }
    private void Test()
    {
        XmlDocument xdc = new XmlDocument();
        xdc.Load(filename);

 

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

 

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

 

        // setup the transform, for onrender
        XslTransform xslt = new XslTransform();
        xslt.Load(stylesheet);
       
        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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Xml id="xmlTest" runat="server"></asp:Xml>
    </div>
    </form>
</body>
</html>

 

Download : XSLT_CallMethod_Example.zip (2.87 kb)

 

 

分享到: 更多
不允许评论