Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you can call C# function from .xsl file. Please refer following code. </p> <p>This is your input XML File: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?&gt; &lt;root&gt; &lt;employee&gt; &lt;firstname&gt;Kaushal&lt;/firstname&gt; &lt;lastname&gt;Parik&lt;/lastname&gt; &lt;/employee&gt; &lt;employee&gt; &lt;firstname&gt;Abhishek&lt;/firstname&gt; &lt;lastname&gt;Swarnkar&lt;/lastname&gt; &lt;/employee&gt; &lt;/root&gt; </code></pre> <p>Formatting function in a C# class is like this: </p> <pre><code> public class MyXslExtension { public string FormatName(string name) { return "Mr. " + name; } } </code></pre> <p>Apply following xsl: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:variable name="vQ"&gt;Mr. &lt;/xsl:variable&gt; &lt;xsl:template match="@*|node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="employee/firstname"&gt; &lt;xsl:element name="firstname"&gt; &lt;xsl:value-of select="myUtils:FormatName(.)" /&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>And C# Functin to call Formatting function is like this: </p> <pre><code>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); } } </code></pre> <p>And the out put is: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?&gt; &lt;root&gt; &lt;employee&gt; &lt;firstname&gt;Mr. Kaushal&lt;/firstname&gt; &lt;lastname&gt;Parik&lt;/lastname&gt; &lt;/employee&gt; &lt;employee&gt; &lt;firstname&gt;Mr. Abhishek&lt;/firstname&gt; &lt;lastname&gt;Swarnkar&lt;/lastname&gt; &lt;/employee&gt; &lt;/root&gt; </code></pre> <p>I have referred <a href="http://www.pedautreppe.com/post/Calling-C-function-in-XSL.aspx" rel="nofollow noreferrer">this link</a> to answer your question. </p> <p>Hope this will help you.<br> Please mark +1 if it is useful to you....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload