Csharp/CSharp Tutorial/XML/Xslt

Материал из .Net Framework эксперт
Версия от 12:17, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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();
    //Create Root Template 
    writer.WriteStartElement("xsl:template");
    writer.WriteAttributeString("match","/");
    writer.WriteStartElement("html");
    writer.WriteStartElement("head");
    writer.WriteStartElement("title");
    writer.WriteString("Hardware | Online Catalog");
    writer.WriteEndElement(); 
    writer.WriteEndElement(); 
    writer.WriteStartElement("xsl:apply-templates");
    writer.WriteEndElement(); 
    writer.WriteEndElement(); 
    writer.WriteEndElement(); 
    
    //Create Inventory Template
    writer.WriteStartElement("xsl:template");
    writer.WriteAttributeString("match","inventory");
    writer.WriteStartElement("body");
    writer.WriteAttributeString("bgcolor","#FFFFFF");
    writer.WriteStartElement("h1");
    writer.WriteString("Hardware");
    writer.WriteEndElement(); 
    writer.WriteStartElement("h2");
    writer.WriteString("Online Catalog");
    writer.WriteEndElement(); 
    writer.WriteStartElement("xsl:apply-templates");
    writer.WriteEndElement();
    writer.WriteEndElement(); 
    writer.WriteEndElement(); 
    writer.WriteEndElement(); 
    writer.WriteEndDocument();
  }
}

Xslt Argument List

using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
class MainClass{
   public static void Main(){
     try{
        XPathDocument doc = new XPathDocument( @"Sample.xml" );
    
        StringWriter sw = new StringWriter();
        XmlTextWriter tw = new XmlTextWriter( sw );
        tw.Formatting = Formatting.Indented;
        tw.Indentation = 4;
    
        XslTransform tr = new XslTransform();
        tr.Load( @"SampleTransform.xslt" );
    
        XsltArgumentList arg = new XsltArgumentList();
        arg.AddParam( "arg", "", "yourParam" );
    
        tr.Transform( doc.CreateNavigator(), arg, tw );
        
        tw.Close();
        sw.Close();
    
     } catch( Exception exc ) {
        Console.WriteLine(exc.Message );
     }
  }
}