2

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>
2
  • as far as I know there is no way to call an outside function in xsl. One possible approach although challenging is to create a custom XSL processor with additional functions to call your desired C# functions Commented Aug 2, 2012 at 6:27
  • I saw some codes like, creating a custom class in C# and adding the methods in that class.... Then these methods were called from xslt code.... But, I cant understand them.... Commented Aug 2, 2012 at 9:15

3 Answers 3

8

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....

Sign up to request clarification or add additional context in comments.

3 Comments

I executed your code.... The xml file remains the same.... I want the original xml to be modified.... I dont want to create a new xml.... If the original xml is "nirav.xml", the output should also be "nirav.xml", but with the "Mr." concatenated to each name....
Great Nirav.... I got the solution.... Ur code creates a new xml named "books1.xml" and stores in C:Program Files(x86)/Common files/microsoft shared/ devserver/10.0.... Thanks a lot for taking ur time to help me....
Hi Nirav, I've another challenge for you.... Please see stackoverflow.com/questions/11791180/… Thanks in advance....
1

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");

1 Comment

Hi Ria, I cannot use the first method as there are too many values.... Can you give me a small example using the second method.... I am new to this, so please give a sample code for xslt file and aspx.cs file for the above xml....
0

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...

3 Comments

Hi Nirav, I need to do the concatenation in C# code and not in xslt.... Kindly see this post, stackoverflow.com/questions/3994418/… It will give you more clear idea.... Sorry for the inconvenience....
Yes, I got your point. I am posting another answer. I am sure that, that answer will solve your query.
You can refer my answer to your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.