O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

.NET Version

The .NET version works similarly, although we need two C# files to use the extension. The first creates the XslCompiledTransform object and associates the extension function with it. The second file actually implements the extension, creating the hidden word graphic.

Here’s the first C# file, which sets up the transformation object:

/*
 * Main.cs 
 */

using System;
using System.Text;
using System.Xml;
using System.Xml.Xsl;

namespace com.oreilly.xslt
{
  class MainClass
  {
    static void Main(string[] args)
    {
      if (args.Length < 3)
      {
        System.Console.WriteLine("\nUsage: HiddenWordExample xml-file " +
          "xsl-file output-file");
        System.Console.WriteLine("\n  Example: HiddenWordExample " +
          "blank.xml hidden-word-test.xsl results.html");
      }
      else
      {
        // Create the stylesheet object and the XMLWriter that
        // writes the output to a file
        XslCompiledTransform stylesheet = new XslCompiledTransform();
        XmlTextWriter xWriter =
            new XmlTextWriter(args[2], Encoding.UTF8);

        // Use an XsltSettings object that allows executing scripts
        // (we need this for extensions), then load the stylesheet
        XsltSettings settings = new XsltSettings(true, true);
        stylesheet.Load(args[1], settings, new XmlUrlResolver());

        // Create an extension object
        HiddenWord hw = new HiddenWord();

        // We set up the extension function with an XsltArgumentList object.
        XsltArgumentList argList = new XsltArgumentList();
        argList.AddExtensionObject("http://www.oreilly.com/xslt", hw);

 // With everything in place, we call the Transform() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required