som i følgende hack:
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 Hack
{
private TextWriter tw;
public Hack(TextWriter tw)
{
this.tw = tw;
}
public string Hello()
{
tw.Write("**** Hello ****");
return "";
}
}
public class MainClass
{
public static void Main(string[] args)
{
string input =
@"<allnum>
<num>1</num>
<num>2</num>
<num>3</num>
<num>4</num>
<num>5</num>
</allnum>";
string transform =
@"<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='
http://www.w3.org/1999/XSL/Transform' xmlns:ms='urn:schemas-microsoft-com:xslt'
xmlns:csinline='urn:arne:csinline'
xmlns:cs='urn:arne:cs'
xmlns:hack='urn:arne:hack'
version='1.0'>
<ms:script language='C#' implements-prefix='csinline'>
<![CDATA[
public string Header()
{
return ""Faculty:"";
}
]]>
</ms:script>
<xsl:template match='/'>
<xsl:value-of select='csinline:Header()'/>
<xsl:value-of select='hack:Hello()'/>
<xsl:for-each select='//num'>
fac(<xsl:value-of select='.'/>)=<xsl:value-of select='cs:Fac(number(.))'/>
</xsl:for-each>
Sum=<xsl:value-of select='cs:Sum(//allnum)'/>
</xsl:template>
</xsl:stylesheet>";
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());
xal.AddExtensionObject("urn:arne:hack", new Hack(sw));
xslt.Transform(doc, xal, wrt);
Console.WriteLine(sw.ToString());
}
}
}