Result Tree Fragment, Nodeset and msxsl:script Element Options

codeling Posts: 1057 Points: 4443
Posted: Monday, September 18, 2017 1:30:58 PM

The following example uses an embedded script to calculate the circumference of a circle given its radius.

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public class Sample {

  private const String filename = "number.xml";
  private const String stylesheet = "calc.xsl";
  private const String output = "output.xml";

  public static void Main() {

    // Compile the style sheet.
    XsltSettings xslt_settings = new XsltSettings();
    xslt_settings.EnableScript = true;
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load(stylesheet, xslt_settings, new XmlUrlResolver());

    // Load the XML source file.
    XPathDocument doc = new XPathDocument(filename);

    // Create an XmlWriter.
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;
    settings.Indent = true;
    XmlWriter writer = XmlWriter.Create(output, settings);

    // Execute the transformation.
    xslt.Transform(doc, writer);

<?xml version='1.0'?>


<xsl:stylesheet version="1.0" xmlns:xsl=""
  <msxsl:script language="C#" implements-prefix="user">
  public double circumference(double radius){
    double pi = 3.14;
    double circ = pi*radius*2;
    return circ;
  <xsl:template match="data">
      <xsl:for-each select="circle">
          <xsl:copy-of select="node()"/>
            <xsl:value-of select="user:circumference(radius)"/>


<circles xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">  

codeling Posts: 1057 Points: 4443
Posted: Monday, September 18, 2017 1:39:30 PM

When using the msxsl:script element, we strongly recommend that the script, regardless of language, be placed inside a CDATA section. Because the script can contain operators, identifiers, or delimiters for a given language, if it is not contained within a CDATA section, it has the potential of being misinterpreted as XML. The following XML shows a template of the CDATA section where code can be placed.

Users browsing this topic