I have a xml file and an xslt file in my website project. When I run the site, I need to call a c# function from xslt and alter the values in xml file.... Given below is my xml file.... I need to add a text (say "Mr.") in front of every firstname through a c# code.... After adding, it should reflect in the xml file.... Also, as a next step, I need to add another node in the xml file (say age) through another c# function.... Please note that the c# function should be called from my xslt file.... Can anyone help me with a simple code for this????
<?xml version="1.0" encoding="utf-8" ?>
<root>
<employee>
<firstname>Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
Yes, you can call C# function from .xsl file. Please refer following code.
This is your input XML File:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
<employee>
<firstname>Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
Formatting function in a C# class is like this:
public class MyXslExtension
{
public string FormatName(string name)
{
return "Mr. " + name;
}
}
Apply following xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:myUtils="pda:MyUtils">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="vQ">Mr. </xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="employee/firstname">
<xsl:element name="firstname">
<xsl:value-of select="myUtils:FormatName(.)" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
And C# Functin to call Formatting function is like this:
private void button3_Click(object sender, EventArgs e)
{
XsltArgumentList arguments = new XsltArgumentList();
arguments.AddExtensionObject("pda:MyUtils", new MyXslExtension());
using (StreamWriter writer = new StreamWriter("books1.xml"))
{
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("transform.xslt");
transform.Transform("books.xml", arguments, writer);
}
}
And the out put is:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
<employee>
<firstname>Mr. Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Mr. Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
I have referred this link to answer your question.
Hope this will help you.
Please mark +1 if it is useful to you....
Add the XSL style sheet reference to your XML document, like this:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
<employee>
<firstname>Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
or use XslTransform
class to transforms XML data using an XSLT
from .NET:
//Create the XslTransform object.
XslTransform xslt = new XslTransform();
//Load the stylesheet.
xslt.Load("output.xsl");
//Transform the file.
xslt.Transform("books.xml", "books.html");
Apply following .xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="vQ">Mr. </xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="employee/firstname">
<xsl:element name="firstname">
<xsl:value-of select="concat($vQ, .)"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Input is:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
<employee>
<firstname>Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
Out put is:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
<employee>
<firstname>Mr. Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Mr. Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
and C# function is like this:
private void button3_Click(object sender, EventArgs e)
{
XslTransform xslt = new XslTransform();
xslt.Load("transform.xslt");
xslt.Transform("books.xml", "books1.xml");
}
Hope this will help you...