Altova XMLSpy 2023 Enterprise Edition

If you are working on the .NET platform on a Windows machine, you can use extension functions written in any of the .NET languages (for example, C#). A .NET extension function can be used within an XPath or XQuery expression to invoke a constructor, property, or method (static or instance) within a .NET class.

 

A property of a .NET class is called using the syntax get_PropertyName().

 

This section is organized into the following sub-sections:

 

.NET: Constructors

.NET: Static Methods and Static Fields

.NET: Instance Methods and Instance Fields

Datatypes: XPath/XQuery to .NET

Datatypes: .NET to XPath/XQuery

 

Form of the extension function

The extension function in the XPath/XQuery expression must have the form prefix:fname().

 

The prefix: part is associated with a URI that identifies the .NET class being addressed.

The fname() part identifies the constructor, property, or method (static or instance) within the .NET class, and supplies any argument/s, if required.

The URI must begin with clitype: (which identifies the function as being a .NET extension function).

The prefix:fname() form of the extension function can be used with system classes and with classes in a loaded assembly. However, if a class needs to be loaded, additional parameters containing the required information will have to be supplied.

 

Parameters

To load an assembly, the following parameters are used:

 

asm

The name of the assembly to be loaded.

ver

The version number (maximum of four integers separated by periods).

sn

The key token of the assembly's strong name (16 hex digits).

from

A URI that gives the location of the assembly (DLL) to be loaded. If the URI is relative, it is relative to the XSLT or XQuery document. If this parameter is present, any other parameter is ignored.

partialname

The partial name of the assembly. It is supplied to Assembly.LoadWith.PartialName(), which will attempt to load the assembly. If partialname is present, any other parameter is ignored.

loc

The locale, for example, en-US. The default is neutral.

 

If the assembly is to be loaded from a DLL, use the from parameter and omit the sn parameter. If the assembly is to be loaded from the Global Assembly Cache (GAC), use the sn parameter and omit the from parameter.

 

A question mark must be inserted before the first parameter, and parameters must be separated by a semi-colon. The parameter name gives its value with an equals sign (see example below).

 

Examples of namespace declarations

An example of a namespace declaration in XSLT that identifies the system class System.Environment:

 

xmlns:myns="clitype:System.Environment"

 

An example of a namespace declaration in XSLT that identifies the class to be loaded as Trade.Forward.Scrip:

 

xmlns:myns="clitype:Trade.Forward.Scrip?asm=forward;version=10.6.2.1"

 

An example of a namespace declaration in XQuery that identifies the system class MyManagedDLL.testClass:. Two cases are distinguished:

 

1.When the assembly is loaded from the GAC:

declare namespace cs="clitype:MyManagedDLL.testClass?asm=MyManagedDLL;

         ver=1.2.3.4;loc=neutral;sn=b9f091b72dccfba8";

 

2.When the assembly is loaded from the DLL (complete and partial references below):

         declare namespace cs="clitype:MyManagedDLL.testClass?from=file:///C:/Altova
         Projects/extFunctions/MyManagedDLL.dll;

 

declare namespace cs="clitype:MyManagedDLL.testClass?from=MyManagedDLL.dll;

 

XSLT example

Here is a complete XSLT example that calls functions in system class System.Math:

 

<xsl:stylesheet version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

   xmlns:xs="http://www.w3.org/2001/XMLSchema"

   xmlns:fn="http://www.w3.org/2005/xpath-functions">

   <xsl:output method="xml" omit-xml-declaration="yes" />

   <xsl:template match="/">

      <math xmlns:math="clitype:System.Math">

         <sqrt><xsl:value-of select="math:Sqrt(9)"/></sqrt>

         <pi><xsl:value-of select="math:PI()"/></pi>

         <e><xsl:value-of select="math:E()"/></e>

         <pow><xsl:value-of select="math:Pow(math:PI(), math:E())"/></pow>

      </math>

   </xsl:template>

</xsl:stylesheet>

 

The namespace declaration on the element math associates the prefix math: with the URI clitype:System.Math. The clitype: beginning of the URI indicates that what follows identifies either a system class or a loaded class. The math: prefix in the XPath expressions associates the  extension functions with the URI (and, by extension, the class) System.Math. The extension functions identify methods in the class System.Math and supply arguments where required.

 

XQuery example

Here is an XQuery example fragment similar to the XSLT example above:

 

 <math xmlns:math="clitype:System.Math">

    {math:Sqrt(9)}

 </math>

 

As with the XSLT example above, the namespace declaration identifies the .NET class, in this case a system class. The XQuery expression identifies the method to be called and supplies the argument.

 

© 2017-2023 Altova GmbH