// Collection of code snippets by Arne Vajhøj
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now)
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace E
{
public class Calcer
{
public int Fac(int n)
{
if(n > 1)
{
return n * Fac(n - 1) ;
}
else
{
return 1;
}
}
public int Sum(XPathNavigator xpn)
{
int sum = 0;
foreach(XPathNavigator num in xpn.SelectChildren(XPathNodeType.Element))
{
sum += Fac(num.Select("child::*").Current.ValueAsInt);
}
return sum;
}
}
public class MainClass
{
public static void Main(string[] args)
{
string input =
@"
1
2
3
4
5
";
string transform =
@"
fac()=
Sum=
";
XmlDocument doc = new XmlDocument();
doc.LoadXml(input);
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(new XmlTextReader(new StringReader(transform)), new XsltSettings(true, true), null);
StringWriter sw = new StringWriter();
XmlTextWriter wrt = new XmlTextWriter(sw);
wrt.Formatting = Formatting.Indented;
XsltArgumentList xal = new XsltArgumentList();
xal.AddExtensionObject("urn:arne:cs", new Calcer());
xslt.Transform(doc, xal, wrt);
Console.WriteLine(sw.ToString());
}
}
}