5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
9 // (C) 2003 Atsushi Enomoto
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
36 using System.Xml.XPath;
40 using QName = System.Xml.XmlQualifiedName;
42 namespace Mono.Xml.Xsl.Operations {
43 internal class XslAttribute : XslCompiledElement {
45 string calcName, calcNs, calcPrefix;
46 // XmlNamespaceManager nsm;
50 public XslAttribute (Compiler c) : base (c) {}
52 protected override void Compile (Compiler c)
54 if (c.Debugger != null)
55 c.Debugger.DebugCompile (c.Input);
57 XPathNavigator nav = c.Input.Clone ();
58 nsDecls = c.GetNamespacesToCopy ();
60 c.CheckExtraAttributes ("attribute", "name", "namespace");
62 name = c.ParseAvtAttribute ("name");
64 throw new XsltCompileException ("Attribute \"name\" is required on XSLT attribute element", null, c.Input);
65 ns = c.ParseAvtAttribute ("namespace");
67 calcName = XslAvt.AttemptPreCalc (ref name);
68 calcPrefix = String.Empty;
70 if (calcName != null) {
71 int colonAt = calcName.IndexOf (':');
72 calcPrefix = colonAt < 0 ? String.Empty : calcName.Substring (0, colonAt);
73 calcName = colonAt < 0 ? calcName : calcName.Substring (colonAt + 1, calcName.Length - colonAt - 1);
76 XmlConvert.VerifyNCName (calcName);
77 if (calcPrefix != String.Empty)
78 XmlConvert.VerifyNCName (calcPrefix);
79 } catch (XmlException ex) {
80 throw new XsltCompileException ("Invalid attribute name", ex, c.Input);
84 if (calcPrefix != String.Empty) {
85 calcPrefix = c.CurrentStylesheet.GetActualPrefix (calcPrefix);
86 if (calcPrefix == null)
87 calcPrefix = String.Empty;
90 if (calcPrefix != String.Empty && ns == null)
91 calcNs = nav.GetNamespace (calcPrefix);
93 calcNs = XslAvt.AttemptPreCalc (ref ns);
95 if (c.Input.MoveToFirstChild ()) {
96 value = c.CompileTemplateContent (XPathNodeType.Attribute);
97 c.Input.MoveToParent ();
101 public override void Evaluate (XslTransformProcessor p)
103 if (p.Debugger != null)
104 p.Debugger.DebugExecute (p, this.DebugInput);
106 string nm, nmsp, prefix;
108 nm = calcName != null ? calcName : name.Evaluate (p);
109 nmsp = calcNs != null ? calcNs : ns != null ? ns.Evaluate (p) : String.Empty;
110 prefix = calcPrefix != null ? calcPrefix : String.Empty;
113 // It is an error. We must recover by not emmiting any attributes
114 // (unless we throw an exception).
117 int colonAt = nm.IndexOf (':');
120 prefix = nm.Substring (0, colonAt);
121 nm = nm.Substring (colonAt + 1);
124 if (nmsp == String.Empty &&
125 prefix == XmlNamespaceManager.PrefixXml)
126 nmsp = XmlNamespaceManager.XmlnsXml;
127 else if (nmsp == String.Empty) {
128 nmsp = (string) nsDecls [prefix];
134 if (prefix == "xmlns")
135 prefix = String.Empty; // Should not be allowed.
137 XmlConvert.VerifyName (nm);
139 p.Out.WriteAttributeString (prefix, nm, nmsp,
140 value == null ? "" : value.EvaluateAsString (p));