Writing C# Code In Xslt

Many things cannot be done in xslt directly due to a lack of manipulation functions. In order to cure that, Microsoft has provided a way to include c# code directly in xslt. Here’s an example:

<?xml version=1.0 encoding=utf-8 ?>

  <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform xmlns:msxsl=urn:schemas-microsoft-com:xslt xmlns:ClassNameHere=dummy namespace here>

    <msxsl:script language=C# implements-prefix=ClassNameHere>

      <![CDATA[

        public string MethodName(/* some parameters here*/)

        {

          return /*something*/;

        }

    ]]>

  </msxsl:script>

  <xsl:template match=//Some Node>

    <xsl:value-of select=ClassNameHere:MethodName(SomeNodeHere)/>

  </xsl:template>

</xsl:stylesheet>

Advertisements