Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate xml file by calling a C# function in xslt
    primarykey
    data
    text
    <p>I have a xml file and an xslt file in my website project.</p> <p>xml file:</p> <pre><code>&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>xslt:</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:Concat="urn:XslSample"&gt; &lt;xsl:output method="html" indent="yes"/&gt; &lt;xsl:template match="root"&gt; &lt;xsl:for-each select="employee"&gt; &lt;![CDATA[Concatenated name is ]]&gt; &lt;xsl:value-of select="Concat:GetFullName(firstname,lastname)"/&gt; &lt;xsl:value-of select="age"/&gt; &lt;br /&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>aspx.cs:</p> <pre><code>using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Xml.XPath; using System.Xml.Xsl; using System.Xml; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { MemoryStream objStream = new MemoryStream(); StreamWriter objWriter = new StreamWriter(objStream, System.Text.Encoding.UTF8); XPathDocument doc = new XPathDocument(Server.MapPath("XMLFile.xml")); XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(Server.MapPath("XSLTFile.xslt")); //create the XslArgumentList and new BookUtils object XsltArgumentList argList = new XsltArgumentList(); Concat objUtil = new Concat(); //this tells the argumentlist about BookUtils argList.AddExtensionObject("urn:XslSample", objUtil); //new XPathNavigator XPathNavigator nav = doc.CreateNavigator(); //do the transform trans.Transform(nav, argList, objWriter); //objWriter.Flush(); objStream.Position = 0; StreamReader oReader = new StreamReader(objStream); string strResult = oReader.ReadToEnd(); //objWriter.Close(); //oReader.Close(); Response.Write(strResult); } catch (Exception Ex) { Response.Write(Ex.Message); } } } public class Concat { public Concat() { } public string GetFullName(string firstname, string lastname) { return "Mr." + firstname; } } </code></pre> <p>When I run the site, I need to call a c# function from xslt and alter the values in xml file.... I am adding a text (say "Mr.") in front of every firstname through a c# code.... After adding, it writes the output as a response, but the original xml is not modified. I want that to be reflected in the xml file....</p> <p>xml output needed:</p> <pre><code>&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>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????</p> <p>Final xml required:</p> <pre><code>&lt;root&gt; &lt;employee&gt; &lt;firstname&gt;Mr.Kaushal&lt;/firstname&gt; &lt;lastname&gt;Parik&lt;/lastname&gt; &lt;age&gt;34&lt;/age&gt; &lt;/employee&gt; &lt;employee&gt; &lt;firstname&gt;Mr.Abhishek&lt;/firstname&gt; &lt;lastname&gt;Swarnkar&lt;/lastname&gt; &lt;age&gt;30&lt;/age&gt; &lt;/employee&gt; &lt;/root&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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