In this blog, u will learn how to pass the reference (object) of the class to the xslt as a argument, to powered the xslt to use the functionalities of the aspx methods.
Here, we will use two methods 'Circumference' and 'FullName'
of class 'myClass' by passing their object reference 'obj' to the xslt.
Lets have a look..
cls.xml
-------------------
<?xml version="1.0" encoding="utf-8" ?>
<data>
<circle>
<radius>12</radius>
<first>Ankit</first>
<last>Jaiswal</last>
</circle>
<circle>
<radius>5</radius>
<first>prateek</first>
<last>jaiswal</last>
</circle>
</data>
cls.xslt
----------------------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ankit="obj_reference">
<xsl:template match="data">
<circles>
<xsl:for-each select="circle">
<circle>
<circumference>
<xsl:value-of select="ankit:Circumference(radius)"/>
</circumference>
<FullName>
<xsl:value-of select="ankit:FullName(first,last)"></xsl:value-of>
</FullName>
</circle>
</xsl:for-each>
</circles>
</xsl:template>
</xsl:stylesheet>
program.aspx.cs
-----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;
using System.Text;
using System.IO;
public partial class program: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Method();
}
catch (Exception ex)
{
}
}
private void Method()
{
XslTransform xslt = new XslTransform();
xslt.Load(MapPath("cls.xslt"));
XPathDocument doc = new XPathDocument(MapPath("cls.xml"));
XsltArgumentList xslArg = new XsltArgumentList();
myClass obj = new myClass(); // create object
xslArg.AddExtensionObject("obj_reference", obj);
StringBuilder htmlOutput = new StringBuilder();
TextWriter htmlWriter = new StringWriter(htmlOutput);
xslt.Transform(doc, xslArg, htmlWriter);
string str = htmlOutput.ToString(); // get result in ''str"
}
}
public class myClass
{
private double circ = 0;
public double Circumference(double radius)
{
circ = Math.PI * 2 * radius;
return circ;
}
public string FullName(string FirstName, string LastName)
{
return FirstName + " " + LastName;
}
}
OUTPUT:
<?xml version="1.0" encoding="utf-16"?>
<circles xmlns:ankit="obj_reference">
<circle>
<circumference>75.398223686155035</circumference>
<FullName>Ankit Jaiswal</FullName>
</circle>
<circle>
<circumference>31.415926535897931</circumference>
<FullName>prateek jaiswal</FullName>
</circle>
</circles>
Here, we will use two methods 'Circumference' and 'FullName'
of class 'myClass' by passing their object reference 'obj' to the xslt.
Lets have a look..
cls.xml
-------------------
<?xml version="1.0" encoding="utf-8" ?>
<data>
<circle>
<radius>12</radius>
<first>Ankit</first>
<last>Jaiswal</last>
</circle>
<circle>
<radius>5</radius>
<first>prateek</first>
<last>jaiswal</last>
</circle>
</data>
cls.xslt
----------------------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ankit="obj_reference">
<xsl:template match="data">
<circles>
<xsl:for-each select="circle">
<circle>
<circumference>
<xsl:value-of select="ankit:Circumference(radius)"/>
</circumference>
<FullName>
<xsl:value-of select="ankit:FullName(first,last)"></xsl:value-of>
</FullName>
</circle>
</xsl:for-each>
</circles>
</xsl:template>
</xsl:stylesheet>
program.aspx.cs
-----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;
using System.Text;
using System.IO;
public partial class program: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Method();
}
catch (Exception ex)
{
}
}
private void Method()
{
XslTransform xslt = new XslTransform();
xslt.Load(MapPath("cls.xslt"));
XPathDocument doc = new XPathDocument(MapPath("cls.xml"));
XsltArgumentList xslArg = new XsltArgumentList();
myClass obj = new myClass(); // create object
xslArg.AddExtensionObject("obj_reference", obj);
StringBuilder htmlOutput = new StringBuilder();
TextWriter htmlWriter = new StringWriter(htmlOutput);
xslt.Transform(doc, xslArg, htmlWriter);
string str = htmlOutput.ToString(); // get result in ''str"
}
}
public class myClass
{
private double circ = 0;
public double Circumference(double radius)
{
circ = Math.PI * 2 * radius;
return circ;
}
public string FullName(string FirstName, string LastName)
{
return FirstName + " " + LastName;
}
}
OUTPUT:
<?xml version="1.0" encoding="utf-16"?>
<circles xmlns:ankit="obj_reference">
<circle>
<circumference>75.398223686155035</circumference>
<FullName>Ankit Jaiswal</FullName>
</circle>
<circle>
<circumference>31.415926535897931</circumference>
<FullName>prateek jaiswal</FullName>
</circle>
</circles>
No comments:
Post a Comment