1 // System.Xml.Xsl.XslTransform
\r
3 // Author: Tim Coleman <tim@timcoleman.com>
\r
4 // (C) Copyright 2002 Tim Coleman
\r
7 using System.Xml.XPath;
\r
10 using System.Runtime.InteropServices;
\r
12 namespace System.Xml.Xsl
\r
14 public sealed class XslTransform
\r
19 XmlResolver xmlResolver;
\r
20 string stylesheet_file;
\r
23 #region Constructors
\r
26 public XslTransform ()
\r
34 public XmlResolver XmlResolver {
\r
35 set { xmlResolver = value; }
\r
42 // Loads the XSLT stylesheet contained in the IXPathNavigable.
\r
43 public void Load (IXPathNavigable stylesheet)
\r
45 Load (stylesheet.CreateNavigator ());
\r
48 // Loads the XSLT stylesheet specified by a URL.
\r
49 public void Load (string url)
\r
51 stylesheet_file = url;
\r
54 // Loads the XSLT stylesheet contained in the XmlReader
\r
55 public void Load (XmlReader stylesheet)
\r
57 stylesheet_file = Path.GetTempFileName ();
\r
58 Save (stylesheet, stylesheet_file);
\r
62 // Loads the XSLT stylesheet contained in the XPathNavigator
\r
63 public void Load (XPathNavigator stylesheet)
\r
65 throw new NotImplementedException ();
\r
69 // Loads the XSLT stylesheet contained in the IXPathNavigable.
\r
70 public void Load (IXPathNavigable stylesheet, XmlResolver resolver)
\r
72 throw new NotImplementedException ();
\r
76 // Loads the XSLT stylesheet specified by a URL.
\r
77 public void Load (string url, XmlResolver resolver)
\r
79 throw new NotImplementedException ();
\r
83 // Loads the XSLT stylesheet contained in the XmlReader
\r
84 public void Load (XmlReader stylesheet, XmlResolver resolver)
\r
86 throw new NotImplementedException ();
\r
90 // Loads the XSLT stylesheet contained in the XPathNavigator
\r
91 public void Load (XPathNavigator stylesheet, XmlResolver resolver)
\r
93 throw new NotImplementedException ();
\r
96 [DllImport ("libxslt.so")]
\r
97 static extern IntPtr xsltParseStylesheetFile (string filename);
\r
99 [DllImport ("libxslt.so")]
\r
100 static extern IntPtr xsltApplyStylesheet (IntPtr stylePtr, IntPtr DocPtr, string [] parameters);
\r
102 [DllImport ("libxslt.so")]
\r
103 static extern IntPtr xmlNewDoc (string version);
\r
105 [DllImport ("libxslt.so")]
\r
106 static extern IntPtr xmlParseFile (string filename);
\r
108 [DllImport ("libxslt.so")]
\r
109 static extern int xmlSaveFile (string filename, IntPtr cur);
\r
112 // Transforms the XML data in the IXPathNavigable using
\r
113 // the specified args and outputs the result to an XmlReader.
\r
114 public XmlReader Transform (IXPathNavigable input, XsltArgumentList args)
\r
116 throw new NotImplementedException ();
\r
120 // Transforms the XML data in the input file and outputs
\r
121 // the result to an output file.
\r
122 public void Transform (string inputfile, string outputfile)
\r
124 IntPtr xmlDocument = xmlParseFile (inputfile);
\r
125 IntPtr xmlStylesheet = xsltParseStylesheetFile (stylesheet_file);
\r
126 IntPtr xmlOutput = xmlNewDoc ("1.0");
\r
127 string [] parameters = new string [] {};
\r
129 xmlOutput = xsltApplyStylesheet (xmlStylesheet, xmlDocument, parameters);
\r
131 xmlSaveFile (outputfile, xmlOutput);
\r
135 // Transforms the XML data in the XPathNavigator using
\r
136 // the specified args and outputs the result to an XmlReader.
\r
137 public XmlReader Transform (XPathNavigator input, XsltArgumentList args)
\r
139 throw new NotImplementedException ();
\r
143 // Transforms the XML data in the IXPathNavigable using
\r
144 // the specified args and outputs the result to a Stream.
\r
145 public void Transform (IXPathNavigable input, XsltArgumentList args, Stream output)
\r
147 throw new NotImplementedException ();
\r
151 // Transforms the XML data in the IXPathNavigable using
\r
152 // the specified args and outputs the result to a TextWriter.
\r
153 public void Transform (IXPathNavigable input, XsltArgumentList args, TextWriter output)
\r
155 throw new NotImplementedException ();
\r
159 // Transforms the XML data in the IXPathNavigable using
\r
160 // the specified args and outputs the result to an XmlWriter.
\r
161 public void Transform (IXPathNavigable input, XsltArgumentList args, XmlWriter output)
\r
163 throw new NotImplementedException ();
\r
167 // Transforms the XML data in the XPathNavigator using
\r
168 // the specified args and outputs the result to a Stream.
\r
169 public void Transform (XPathNavigator input, XsltArgumentList args, Stream output)
\r
171 throw new NotImplementedException ();
\r
175 // Transforms the XML data in the XPathNavigator using
\r
176 // the specified args and outputs the result to a TextWriter.
\r
177 public void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output)
\r
179 throw new NotImplementedException ();
\r
183 // Transforms the XML data in the XPathNavigator using
\r
184 // the specified args and outputs the result to an XmlWriter.
\r
185 public void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output)
\r
187 throw new NotImplementedException ();
\r
190 static void Save (XmlReader rdr, string filename)
\r
192 XmlTextWriter writer = new XmlTextWriter (filename, new UTF8Encoding ());
\r
194 while (rdr.Read ()) {
\r
195 switch (rdr.NodeType) {
\r
197 case XmlNodeType.CDATA:
\r
198 writer.WriteCData (rdr.Value);
\r
201 case XmlNodeType.Comment:
\r
202 writer.WriteComment (rdr.Value);
\r
205 case XmlNodeType.DocumentType:
\r
206 writer.WriteDocType (rdr.Value, null, null, null);
\r
209 case XmlNodeType.Element:
\r
210 writer.WriteStartElement (rdr.Name, rdr.Value);
\r
212 while (rdr.MoveToNextAttribute ())
\r
213 writer.WriteAttributes (rdr, true);
\r
216 case XmlNodeType.EndElement:
\r
217 writer.WriteEndElement ();
\r
220 case XmlNodeType.ProcessingInstruction:
\r
221 writer.WriteProcessingInstruction (rdr.Name, rdr.Value);
\r
224 case XmlNodeType.Text:
\r
225 writer.WriteString (rdr.Value);
\r
228 case XmlNodeType.Whitespace:
\r
229 writer.WriteWhitespace (rdr.Value);
\r
232 case XmlNodeType.XmlDeclaration:
\r
233 writer.WriteStartDocument ();
\r