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/19/2008 8:07:00 AM On Aug 18, 1:31=A0pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> bar...@bluezone.no 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
> =A0 =A0xmlns:xsl=3D"http://www.w3.org/1999/XSL/Transform"
> =A0 =A0xmlns:msxsl=3D"urn:schemas-microsoft-com:xslt"
> =A0 =A0xmlns:my=3D"http://example.com/2008/mf"
> =A0 =A0exclude-result-prefixes=3D"my msxsl"
> =A0 =A0version=3D"1.0">
>
> =A0 =A0<xsl:output method=3D"html" indent=3D"yes"/>
>
> =A0 =A0<msxsl:script language=3D"C#" implements-prefix=3D"my">
> =A0 =A0 =A0public double stringToDouble(string s)
> =A0 =A0 =A0{
> =A0 =A0 =A0 =A0return double.Parse(s,
> System.Globalization.CultureInfo.InvariantCulture);
> =A0 =A0 =A0}
> =A0 =A0</msxsl:script>
>
> =A0 =A0<xsl:template match=3D"root">
> =A0 =A0 =A0<html lang=3D"en">
> =A0 =A0 =A0 =A0<head>
> =A0 =A0 =A0 =A0 =A0<title>Example</title>
> =A0 =A0 =A0 =A0</head>
> =A0 =A0 =A0 =A0<body>
> =A0 =A0 =A0 =A0 =A0<ul>
> =A0 =A0 =A0 =A0 =A0 =A0<xsl:apply-templates select=3D"foo"/>
> =A0 =A0 =A0 =A0 =A0</ul>
> =A0 =A0 =A0 =A0</body>
> =A0 =A0 =A0</html>
> =A0 =A0</xsl:template>
>
> =A0 =A0<xsl:template match=3D"foo">
> =A0 =A0 =A0<li>
> =A0 =A0 =A0 =A0<xsl:value-of select=3D"@a1 * my:stringToDouble(string(@a2=
))"/>
> =A0 =A0 =A0</li>
> =A0 =A0</xsl:template>
>
> </xsl:stylesheet>
>
> Or you could implement an extension object that provides the
> functionality and pass it in using an XsltArgumentList:
>
> =A0 =A0 =A0public class MyConverter
> =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0public double StringToDouble(string s)
> =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0return double.Parse(s,
> System.Globalization.CultureInfo.InvariantCulture);
> =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0XslCompiledTransform xsltProc =3D new XslCompi=
ledTransform();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0xsltProc.Load(@"..\..\XSLTFile2.xslt");
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0XsltArgumentList argList =3D new XsltArgumentL=
ist();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0argList.AddExtensionObject("http://example.com=
/2008/mc",
> new MyConverter());
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0xsltProc.Transform(@"..\..\XMLFile1.xml", argL=
ist,
> Console.Out);
>
> where the stylesheet looks like this:
>
> <xsl:stylesheet
> =A0 =A0xmlns:xsl=3D"http://www.w3.org/1999/XSL/Transform"
> =A0 =A0xmlns:my=3D"http://example.com/2008/mc"
> =A0 =A0exclude-result-prefixes=3D"my"
> =A0 =A0version=3D"1.0">
>
> =A0 =A0<xsl:template match=3D"root">
> =A0 =A0 =A0<html lang=3D"en">
> =A0 =A0 =A0 =A0<head>
> =A0 =A0 =A0 =A0 =A0<title>Example</title>
> =A0 =A0 =A0 =A0</head>
> =A0 =A0 =A0 =A0<body>
> =A0 =A0 =A0 =A0 =A0<ul>
> =A0 =A0 =A0 =A0 =A0 =A0<xsl:apply-templates select=3D"foo"/>
> =A0 =A0 =A0 =A0 =A0</ul>
> =A0 =A0 =A0 =A0</body>
> =A0 =A0 =A0</html>
> =A0 =A0</xsl:template>
>
> =A0 =A0<xsl:template match=3D"foo">
> =A0 =A0 =A0<li>
> =A0 =A0 =A0 =A0<xsl:value-of select=3D"@a1 * my:StringToDouble(string(@a2=
))"/>
> =A0 =A0 =A0</li>
> =A0 =A0</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.
>
> --
>
> =A0 =A0 =A0 =A0 Martin Honnen --- MVP XML
> =A0 =A0 =A0 =A0http://JavaScript.FAQTs.com/
Thanks, it works perfectly! Just a follow-up question: is the
performance penalty bad?
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
