Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: Scientific (Engineering) format supported for multiplication operator? >Thread Next - Re: Scientific (Engineering) format supported for multiplication operator? Re: Scientific (Engineering) format supported for multiplication operator?To: NULL Date: 8/18/2008 1:32:00 PM
barnum@b... wrote:
> We may have a possibility to run this part in .Net (using
> XslCompiledTransform). What's the best way to use an extension
> function there?
You can either use an extension function also implemented using
msxsl:script but with a .NET language like C#
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my="http://example.com/2008/mf"
exclude-result-prefixes="my msxsl"
version="1.0">
<xsl:output method="html" indent="yes"/>
<msxsl:script language="C#" implements-prefix="my">
public double stringToDouble(string s)
{
return double.Parse(s,
System.Globalization.CultureInfo.InvariantCulture);
}
</msxsl:script>
<xsl:template match="root">
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<ul>
<xsl:apply-templates select="foo"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="foo">
<li>
<xsl:value-of select="@a1 * my:stringToDouble(string(@a2))"/>
</li>
</xsl:template>
</xsl:stylesheet>
Or you could implement an extension object that provides the
functionality and pass it in using an XsltArgumentList:
public class MyConverter
{
public double StringToDouble(string s)
{
return double.Parse(s,
System.Globalization.CultureInfo.InvariantCulture);
}
}
XslCompiledTransform xsltProc = new XslCompiledTransform();
xsltProc.Load(@"..\..\XSLTFile2.xslt");
XsltArgumentList argList = new XsltArgumentList();
argList.AddExtensionObject("http://example.com/2008/mc",
new MyConverter());
xsltProc.Transform(@"..\..\XMLFile1.xml", argList,
Console.Out);
where the stylesheet looks like this:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://example.com/2008/mc"
exclude-result-prefixes="my"
version="1.0">
<xsl:template match="root">
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<ul>
<xsl:apply-templates select="foo"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="foo">
<li>
<xsl:value-of select="@a1 * my:StringToDouble(string(@a2))"/>
</li>
</xsl:template>
</xsl:stylesheet>
The extension object is the more efficient solution as using
msxsl:script with the .NET framework creates temporary assemblies you
would need to clean up.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
