5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
9 // (C) 2003 Atsushi Enomoto
14 using System.Collections;
15 using System.Collections.Specialized;
17 using System.Xml.Schema;
18 using System.Xml.XPath;
22 using Mono.Xml.Xsl.Operations;
24 using QName = System.Xml.XmlQualifiedName;
26 namespace Mono.Xml.Xsl {
28 public class XslStylesheet {
29 public const string XsltNamespace = "http://www.w3.org/1999/XSL/Transform";
30 public const string MSXsltNamespace = "urn:schemas-microsoft-com:xslt::script";
34 XslStylesheet importer;
36 ArrayList imports = new ArrayList ();
38 Hashtable spaceControls = new Hashtable ();
39 // [string stylesheet-prefix]=>string result-prefix
40 Hashtable namespaceAliases = new Hashtable ();
42 Hashtable parameters = new Hashtable ();
44 Hashtable keys = new Hashtable();
46 MSXslScriptManager msScripts = new MSXslScriptManager ();
47 XslTemplateTable templates;
49 // stylesheet attributes
51 XmlQualifiedName [] extensionElementPrefixes;
52 XmlQualifiedName [] excludeResultPrefixes;
53 StringDictionary stylesheetNamespaces = new StringDictionary ();
55 // below are newly introduced in XSLT 2.0
57 // xsl:import-schema should be interpreted into it.
58 XmlSchemaCollection schemas = new XmlSchemaCollection ();
59 // [QName]=>XslCharacterMap
60 Hashtable characterMap = new Hashtable ();
61 // [QName]=>XslDateFormat
62 Hashtable dateFormats = new Hashtable ();
63 // [QName]=>XslFunction
64 Hashtable functions = new Hashtable ();
65 // [QName]=>XslSortKey
66 Hashtable sortKeys = new Hashtable ();
68 string xpathDefaultNamespace = "";
69 XslDefaultValidation defaultValidation = XslDefaultValidation.Lax;
71 public XmlQualifiedName [] ExtensionElementPrefixes {
72 get { return extensionElementPrefixes; }
75 public XmlQualifiedName [] ExcludeResultPrefixes {
76 get { return excludeResultPrefixes; }
79 public StringDictionary StylesheetNamespaces {
80 get { return stylesheetNamespaces; }
83 public ArrayList Imports {
84 get { return imports; }
87 public Hashtable SpaceControls {
88 get { return spaceControls; }
91 public Hashtable NamespaceAliases {
92 get { return namespaceAliases; }
95 public Hashtable Parameters {
96 get { return parameters; }
99 public MSXslScriptManager ScriptManager{
100 get { return msScripts; }
103 public XslTemplateTable Templates {
104 get { return templates; }
107 public Hashtable Keys {
111 public XslStylesheet (Compiler c)
114 c.PushStylesheet (this);
116 templates = new XslTemplateTable (this);
117 if (c.Input.NamespaceURI != XsltNamespace) {
118 // then it is simplified stylesheet.
119 Templates.Add (new XslTemplate (c));
121 version = c.Input.GetAttribute ("version", "");
122 extensionElementPrefixes = c.ParseQNameListAttribute ("extension-element-prefixes");
123 excludeResultPrefixes = c.ParseQNameListAttribute ("exclude-result-prefixes");
124 if (c.Input.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
126 if (c.Input.Value == XsltNamespace)
128 this.stylesheetNamespaces.Add (c.Input.Name, c.Input.Value);
129 } while (c.Input.MoveToNextNamespace (XPathNamespaceScope.Local));
130 c.Input.MoveToParent ();
132 ProcessTopLevelElements ();
138 public XslKey FindKey (QName name)
140 XslKey key = Keys [name] as XslKey;
143 for (int i = Imports.Count - 1; i >= 0; i--) {
144 key = ((XslStylesheet) Imports [i]).FindKey (name);
151 private XslStylesheet (Compiler c, XslStylesheet importer) : this (c)
153 this.importer = importer;
156 private void HandleInclude (string href)
158 c.PushInputDocument (href);
159 ProcessTopLevelElements ();
160 c.PopInputDocument ();
163 private void HandleImport (string href)
165 c.PushInputDocument (href);
166 imports.Add (new XslStylesheet (c, this));
167 c.PopInputDocument ();
170 private void HandleTopLevelElement ()
172 XPathNavigator n = c.Input;
173 switch (n.NamespaceURI)
180 HandleInclude (c.GetAttribute ("href"));
183 HandleImport (c.GetAttribute ("href"));
185 case "preserve-space":
186 AddSpaceControls (c.ParseQNameListAttribute ("elements"), XmlSpace.Preserve, n);
190 AddSpaceControls (c.ParseQNameListAttribute ("elements"), XmlSpace.Default, n);
193 case "namespace-alias":
194 namespaceAliases.Add (c.GetAttribute ("stylesheet-prefix", ""), c.GetAttribute ("result-prefix", ""));
197 case "attribute-set":
198 c.AddAttributeSet (new XslAttributeSet (c));
202 keys.Add (c.ParseQNameAttribute ("name"), new XslKey (c));
209 case "decimal-format":
210 c.CompileDecimalFormat ();
214 templates.Add (new XslTemplate (c));
217 c.AddGlobalVariable (new XslGlobalVariable (c));
220 c.AddGlobalVariable (new XslGlobalParam (c));
224 case MSXsltNamespace:
228 msScripts.AddScript (n);
235 private void ProcessTopLevelElements ()
237 if (c.Input.MoveToFirstChild ()) {
239 if (c.Input.NodeType == XPathNodeType.Element) {
240 Debug.EnterNavigator (c);
241 this.HandleTopLevelElement();
242 Debug.ExitNavigator (c);
244 } while (c.Input.MoveToNext ());
246 c.Input.MoveToParent ();
250 private void AddSpaceControls (QName [] names, XmlSpace result, XPathNavigator styleElem)
252 foreach (QName name in names)
253 spaceControls.Add (name, result);
259 public enum XslDefaultValidation