Create Stylesheet : Stylesheet « XML « C# / C Sharp






Create Stylesheet

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

public class CreateStylesheet {
  private const string ns = "http://www.w3.org/1999/XSL/Transform";

  public static void Main(string [] args) {
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
    writer.Formatting = Formatting.Indented;

    writer.WriteStartDocument();

    writer.WriteStartElement("xsl","stylesheet",ns);
    writer.WriteAttributeString("version","1.0");

    writer.WriteStartElement("xsl:output");
    writer.WriteAttributeString("method","html");
    writer.WriteEndElement();

    CreateRootTemplate(writer);
    CreateInventoryTemplate(writer);

    writer.WriteEndElement(); // xsl:stylesheet
    writer.WriteEndDocument();
  }

  private static void CreateRootTemplate(XmlWriter writer) {

    writer.WriteStartElement("xsl:template");
    writer.WriteAttributeString("match","/");

    writer.WriteStartElement("html");

    writer.WriteStartElement("head");

    writer.WriteStartElement("title");
    writer.WriteString("Title 1 | Title 2");
    writer.WriteEndElement(); // title

    writer.WriteEndElement(); // head

    writer.WriteStartElement("xsl:apply-templates");

    writer.WriteEndElement(); // xsl:apply-templates
    writer.WriteEndElement(); // html
    writer.WriteEndElement(); // xsl:template
  }

  private static void CreateInventoryTemplate(XmlWriter writer) {
    writer.WriteStartElement("xsl:template");
    writer.WriteAttributeString("match","yourelement");

    writer.WriteStartElement("body");
    writer.WriteAttributeString("bgcolor","#FFFFFF");

    writer.WriteStartElement("h1");
    writer.WriteString("Title 1");
    writer.WriteEndElement(); // h1

    writer.WriteStartElement("h2");
    writer.WriteString("Title 2");
    writer.WriteEndElement(); // h2

    writer.WriteStartElement("xsl:apply-templates");
    writer.WriteEndElement();

    writer.WriteEndElement(); // body
    writer.WriteEndElement(); // xsl:template
  }
}

           
       








Related examples in the same category