This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslFunctions.cs
1 //
2 // XsltCompiledContext.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 // (C) 2003 Ben Maurer
8 // (C) 2004 Atsushi Enomoto
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Collections;
33 using System.Globalization;
34 using System.Reflection;
35 using System.Text;
36 using System.Xml;
37 using System.Xml.XPath;
38 using System.Xml.Xsl;
39 using Mono.Xml.Xsl;
40
41 using QName = System.Xml.XmlQualifiedName;
42
43 namespace Mono.Xml.Xsl
44 {
45         internal abstract class XPFuncImpl : IXsltContextFunction 
46         {
47                 int minargs, maxargs;
48                 XPathResultType returnType;
49                 XPathResultType [] argTypes;
50
51                 public XPFuncImpl () {}
52                 public XPFuncImpl (int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
53                 {
54                         this.Init(minArgs, maxArgs, returnType, argTypes);
55                 }
56                 
57                 protected void Init (int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
58                 {
59                         this.minargs    = minArgs;
60                         this.maxargs    = maxArgs;
61                         this.returnType = returnType;
62                         this.argTypes   = argTypes;
63                 }
64
65                 public int Minargs { get { return this.minargs; }}
66                 public int Maxargs { get { return this.maxargs; }}
67                 public XPathResultType ReturnType { get { return this.returnType; }}
68                 public XPathResultType [] ArgTypes { get { return this.argTypes; }}
69                 public object Invoke (XsltContext xsltContext, object [] args, XPathNavigator docContext)
70                 {
71                         return Invoke ((XsltCompiledContext)xsltContext, args, docContext);
72                 }
73                 
74                 public abstract object Invoke (XsltCompiledContext xsltContext, object [] args, XPathNavigator docContext);
75                 
76                 public static XPathResultType GetXPathType (Type type, XPathNavigator node) {
77                         switch (Type.GetTypeCode(type)) {
78                         case TypeCode.String:
79                                 return XPathResultType.String;
80                         case TypeCode.Boolean:
81                                 return XPathResultType.Boolean;
82                         case TypeCode.Object:
83                                 if (typeof (XPathNavigator).IsAssignableFrom (type) || typeof (IXPathNavigable).IsAssignableFrom (type))
84                                         return XPathResultType.Navigator;
85                                 
86                                 if (typeof (XPathNodeIterator).IsAssignableFrom (type))
87                                         return XPathResultType.NodeSet;
88                                 
89                                 return XPathResultType.Any;
90                         case TypeCode.DateTime :
91                                 throw new XsltException ("Invalid type DateTime was specified.", null, node);
92                         default: // Numeric
93                                 return XPathResultType.Number;
94                         } 
95                 }
96         }
97         
98         class XsltExtensionFunction : XPFuncImpl 
99         {
100                 private object extension;
101                 private MethodInfo method;
102                 private TypeCode [] typeCodes;
103
104                 public XsltExtensionFunction (object extension, MethodInfo method, XPathNavigator currentNode)
105                 {
106                         this.extension = extension;
107                         this.method = method;
108
109                         ParameterInfo [] parameters = method.GetParameters ();
110                         int minArgs = parameters.Length;
111                         int maxArgs = parameters.Length;
112                         
113                         this.typeCodes = new TypeCode [parameters.Length];
114                         XPathResultType[] argTypes = new XPathResultType [parameters.Length];
115                         
116                         bool canBeOpt = true;
117                         for (int i = parameters.Length - 1; 0 <= i; i--) { // optionals at the end
118                                 typeCodes [i] = Type.GetTypeCode (parameters [i].ParameterType);
119                                 argTypes [i] = GetXPathType (parameters [i].ParameterType, currentNode);
120                                 if (canBeOpt) {
121                                         if (parameters[i].IsOptional)
122                                                 minArgs --;
123                                         else
124                                                 canBeOpt = false;
125                                 }
126                         }
127                         base.Init (minArgs, maxArgs, GetXPathType (method.ReturnType, currentNode), argTypes);
128                 }
129
130                 public override object Invoke (XsltCompiledContext xsltContext, object [] args, XPathNavigator docContext)
131                 {
132                         try {
133                                 ParameterInfo [] pis = method.GetParameters ();
134                                 object [] castedArgs = new object [pis.Length];
135                                 for (int i = 0; i < args.Length; i++) {
136                                         Type t = pis [i].ParameterType;
137                                         switch (t.FullName) {
138                                         case "System.Int16":
139                                         case "System.UInt16":
140                                         case "System.Int32":
141                                         case "System.UInt32":
142                                         case "System.Int64":
143                                         case "System.UInt64":
144                                         case "System.Single":
145                                         case "System.Decimal":
146                                                 castedArgs [i] = Convert.ChangeType (args [i], t);
147                                                 break;
148                                         default:
149                                                 castedArgs [i] = args [i];
150                                                 break;
151                                         }
152                                 }
153
154                                 object result = null;
155                                 switch (method.ReturnType.FullName) {
156                                 case "System.Int16":
157                                 case "System.UInt16":
158                                 case "System.Int32":
159                                 case "System.UInt32":
160                                 case "System.Int64":
161                                 case "System.UInt64":
162                                 case "System.Single":
163                                 case "System.Decimal":
164                                         result = (double) method.Invoke (extension, castedArgs);
165                                         break;
166                                 default:
167                                         result = method.Invoke(extension, castedArgs);
168                                         break;
169                                 }
170                                 IXPathNavigable navigable = result as IXPathNavigable;
171                                 if (navigable != null)
172                                         return navigable.CreateNavigator ();
173
174                                 return result;
175                         } catch (Exception ex) {
176                                 throw new XsltException ("Custom function reported an error.", ex);
177 //                              Debug.WriteLine ("****** INCORRECT RESOLUTION **********");
178                         }
179                 }
180         }
181         
182         class XsltCurrent : XPathFunction 
183         {
184                 public XsltCurrent (FunctionArguments args) : base (args)
185                 {
186                         if (args != null)
187                                 throw new XPathException ("current takes 0 args");
188                 }
189                 
190                 public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
191
192                 public override object Evaluate (BaseIterator iter)
193                 {
194                         return new SelfIterator ((iter.NamespaceManager as XsltCompiledContext).Processor.CurrentNode, null);
195                 }
196         }
197         
198         class XsltDocument : XPathFunction 
199         {
200                 Expression arg0, arg1;
201                 XPathNavigator doc;
202                 
203                 public XsltDocument (FunctionArguments args, Compiler c) : base (args)
204                 {
205                         if (args == null || (args.Tail != null && args.Tail.Tail != null))
206                                 throw new XPathException ("document takes one or two args");
207                         
208                         arg0 = args.Arg;
209                         if (args.Tail != null)
210                                 arg1 = args.Tail.Arg;
211                         doc = c.Input.Clone ();
212                 }
213                 public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
214                 
215                 public override object Evaluate (BaseIterator iter)
216                 {
217                         string baseUri = null;
218                         if (arg1 != null) {
219                                 XPathNodeIterator it = arg1.EvaluateNodeSet (iter);
220                                 if (it.MoveNext())
221                                         baseUri = it.Current.BaseURI;
222                                 else
223                                         baseUri = VoidBaseUriFlag;
224                         }
225
226                         object o = arg0.Evaluate (iter);
227                         if (o is XPathNodeIterator)
228                                 return GetDocument ((iter.NamespaceManager as XsltCompiledContext), (XPathNodeIterator)o, baseUri);
229                         else
230                                 return GetDocument ((iter.NamespaceManager as XsltCompiledContext), o is IFormattable ? ((IFormattable) o).ToString (null, CultureInfo.InvariantCulture) : o.ToString (), baseUri);
231                 }
232                 
233                 static string VoidBaseUriFlag = "&^)(*&%*^$&$VOID!BASE!URI!";
234                 
235                 Uri Resolve (string thisUri, string baseUri, XslTransformProcessor p)
236                 {
237 //                      Debug.WriteLine ("THIS: " + thisUri);
238 //                      Debug.WriteLine ("BASE: " + baseUri);
239                         XmlResolver r = p.Resolver;
240                         
241                         Uri uriBase = null;
242                         if (! object.ReferenceEquals (baseUri, VoidBaseUriFlag) && baseUri != String.Empty)
243                                 uriBase = r.ResolveUri (null, baseUri);
244                                 
245                         return r.ResolveUri (uriBase, thisUri);
246                 }
247                 
248                 XPathNodeIterator GetDocument (XsltCompiledContext xsltContext, XPathNodeIterator itr, string baseUri)
249                 {
250                         ArrayList list = new ArrayList ();
251                         Hashtable got = new Hashtable ();
252                         
253                         while (itr.MoveNext()) {
254                                 Uri uri = Resolve (itr.Current.Value, baseUri != null ? baseUri : /*itr.Current.BaseURI*/doc.BaseURI, xsltContext.Processor);
255                                 if (!got.ContainsKey (uri)) {
256                                         got.Add (uri, null);
257                                         if (uri.ToString () == "") {
258                                                 XPathNavigator n = doc.Clone ();
259                                                 n.MoveToRoot ();
260                                                 list.Add (n);
261                                         } else
262                                                 list.Add (xsltContext.Processor.GetDocument (uri));
263                                 }
264                         }
265                         
266                         return new ListIterator (list, xsltContext, false);
267                 }
268         
269                 XPathNodeIterator GetDocument (XsltCompiledContext xsltContext, string arg0, string baseUri)
270                 {
271                         Uri uri = Resolve (arg0, baseUri != null ? baseUri : doc.BaseURI, xsltContext.Processor);
272                         XPathNavigator n;
273                         if (uri.ToString () == "") {
274                                 n = doc.Clone ();
275                                 n.MoveToRoot ();
276                         } else
277                                 n = xsltContext.Processor.GetDocument (uri);
278                         
279                         return new SelfIterator (n, xsltContext);
280                 }
281         }
282         
283         class XsltElementAvailable : XPathFunction 
284         {
285                 Expression arg0;
286                 XmlNamespaceManager nsm;
287                 
288                 public XsltElementAvailable (FunctionArguments args, IStaticXsltContext ctx) : base (args)
289                 {
290                         if (args == null || args.Tail != null)
291                                 throw new XPathException ("element-available takes 1 arg");
292                         
293                         arg0 = args.Arg;
294                         nsm = ctx.GetNsm ();
295                 }
296                 
297                 public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
298
299                 public override object Evaluate (BaseIterator iter)
300                 {
301                         QName name = XslNameUtil.FromString (arg0.EvaluateString (iter), nsm);
302
303                         return (
304                                 (name.Namespace == Compiler.XsltNamespace) &&
305                                 (
306                                         //
307                                         // A list of all the instructions (does not include top-level-elements)
308                                         //
309                                         name.Name == "apply-imports" ||
310                                         name.Name == "apply-templates" ||
311                                         name.Name == "call-template" ||
312                                         name.Name == "choose" ||
313                                         name.Name == "comment" ||
314                                         name.Name == "copy" ||
315                                         name.Name == "copy-of" ||
316                                         name.Name == "element" ||
317                                         name.Name == "fallback" ||
318                                         name.Name == "for-each" ||
319                                         name.Name == "message" ||
320                                         name.Name == "number" ||
321                                         name.Name == "processing-instruction" ||
322                                         name.Name == "text" ||
323                                         name.Name == "value-of" ||
324                                         name.Name == "variable"
325                                 )
326                         );
327                 }
328         }
329
330         class XsltFormatNumber : XPathFunction 
331         {
332                 Expression arg0, arg1, arg2;
333                 XmlNamespaceManager nsm;
334                 
335                 public XsltFormatNumber (FunctionArguments args, IStaticXsltContext ctx) : base (args)
336                 {
337                         if (args == null || args.Tail == null || (args.Tail.Tail != null && args.Tail.Tail.Tail != null))
338                                 throw new XPathException ("format-number takes 2 or 3 args");
339                         
340                         arg0 = args.Arg;
341                         arg1 = args.Tail.Arg;
342                         if (args.Tail.Tail != null) {
343                                 arg2= args.Tail.Tail.Arg;
344                                 nsm = ctx.GetNsm ();
345                         }
346                 }
347                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
348                 
349                 public override object Evaluate (BaseIterator iter)
350                 {
351                         double d = arg0.EvaluateNumber (iter);
352                         string s = arg1.EvaluateString (iter);
353                         QName nm = QName.Empty;
354                         
355                         if (arg2 != null)
356                                 nm = XslNameUtil.FromString (arg2.EvaluateString (iter), nsm);
357                         
358                         try {
359                                 return (iter.NamespaceManager as XsltCompiledContext).Processor.CompiledStyle
360                                 .LookupDecimalFormat (nm).FormatNumber (d, s);
361                         } catch (ArgumentException ex) {
362                                 throw new XsltException (ex.Message, ex, iter.Current);
363                         }
364                 }
365         }
366         
367         class XsltFunctionAvailable : XPathFunction 
368         {
369                 Expression arg0;
370                 XmlNamespaceManager nsm;
371                 
372                 public XsltFunctionAvailable (FunctionArguments args, IStaticXsltContext ctx) : base (args)
373                 {
374                         if (args == null || args.Tail != null)
375                                 throw new XPathException ("element-available takes 1 arg");
376                         
377                         arg0 = args.Arg;
378                         nsm = ctx.GetNsm ();
379                 }
380                 
381                 public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
382                 
383                 public override object Evaluate (BaseIterator iter)
384                 {
385                         
386                         string name = arg0.EvaluateString (iter);
387                         int colon = name.IndexOf (':');
388                         // extension function
389                         if (colon > 0)
390                                 return (iter.NamespaceManager as XsltCompiledContext).ResolveFunction (
391                                         XslNameUtil.FromString (name, nsm),
392                                         null) != null;
393                         
394                         return (
395                                 //
396                                 // XPath
397                                 //
398                                 name == "boolean" ||
399                                 name == "ceiling" ||
400                                 name == "concat" ||
401                                 name == "contains" ||
402                                 name == "count" ||
403                                 name == "false" ||
404                                 name == "floor" ||
405                                 name == "id"||
406                                 name == "lang" ||
407                                 name == "last" ||
408                                 name == "local-name" ||
409                                 name == "name" ||
410                                 name == "namespace-uri" ||
411                                 name == "normalize-space" ||
412                                 name == "not" ||
413                                 name == "number" ||
414                                 name == "position" ||
415                                 name == "round" ||
416                                 name == "starts-with" ||
417                                 name == "string" ||
418                                 name == "string-length" ||
419                                 name == "substring" ||
420                                 name == "substring-after" ||
421                                 name == "substring-before" ||
422                                 name == "sum" ||
423                                 name == "translate" ||
424                                 name == "true" ||
425                                 // XSLT
426                                 name == "document" ||
427                                 name == "format-number" ||
428                                 name == "function-available" ||
429                                 name == "generate-id" ||
430                                 name == "key" ||
431                                 name == "current" ||
432                                 name == "unparsed-entity-uri" ||
433                                 name == "element-available" ||
434                                 name == "system-property"
435                         );
436                 }
437         } 
438
439         class XsltGenerateId : XPathFunction 
440         {
441                 Expression arg0;
442                 public XsltGenerateId (FunctionArguments args) : base (args)
443                 {
444                         if (args != null) {
445                                 if (args.Tail != null)
446                                         throw new XPathException ("generate-id takes 1 or no args");
447                                 arg0 = args.Arg;
448                         }
449                 }
450                 
451                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
452                 public override object Evaluate (BaseIterator iter)
453                 {
454                         XPathNavigator n;
455                         if (arg0 != null) {
456                                 XPathNodeIterator itr = arg0.EvaluateNodeSet (iter);
457                                 if (itr.MoveNext ())
458                                         n = itr.Current.Clone ();
459                                 else
460                                         return string.Empty; // empty nodeset == empty string
461                         } else
462                                 n = iter.Current.Clone ();
463                         
464                         StringBuilder sb = new StringBuilder ("Mono"); // Ensure begins with alpha
465                         sb.Append (XmlConvert.EncodeLocalName (n.BaseURI));
466                         sb.Replace ('_', 'm'); // remove underscores from EncodeLocalName
467                         sb.Append (n.NodeType);
468                         sb.Append ('m');
469
470                         do {
471                                 sb.Append (IndexInParent (n));
472                                 sb.Append ('m');
473                         } while (n.MoveToParent ());
474                         
475                         return sb.ToString ();
476                 }
477                 
478                 int IndexInParent (XPathNavigator nav)
479                 {
480                         int n = 0;
481                         while (nav.MoveToPrevious ())
482                                 n++;
483                         
484                         return n;
485                 }
486         } 
487         
488         class XsltKey : XPathFunction 
489         {
490                 Expression arg0, arg1;
491                 XmlNamespaceManager nsm;
492                 XslKey key;
493                 
494                 public XsltKey (FunctionArguments args, IStaticXsltContext ctx) : base (args)
495                 {
496                         if (args == null || args.Tail == null)
497                                 throw new XPathException ("key takes 2 args");
498                         arg0 = args.Arg;
499                         arg1 = args.Tail.Arg;
500                         nsm = ctx.GetNsm ();
501                 }
502                 public Expression KeyName { get { return arg0; } }
503                 public Expression Field { get { return arg1; } }
504                 public XmlNamespaceManager NamespaceManager { get { return nsm; } }
505                 public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
506                 
507                 public override object Evaluate (BaseIterator iter)
508                 {
509                         QName name = XslNameUtil.FromString (arg0.EvaluateString (iter), nsm);
510                         XsltCompiledContext ctx = iter.NamespaceManager as XsltCompiledContext;
511                         if (key == null)
512                                 key = ctx.Processor.CompiledStyle.Style.FindKey (name);
513
514                         ArrayList result = new ArrayList ();
515                         object o = arg1.Evaluate (iter);
516                         XPathNodeIterator it = o as XPathNodeIterator;
517                         
518                         if (it != null) {
519                                 while (it.MoveNext())
520                                         FindKeyMatch (ctx, it.Current.Value, result, iter.Current);
521                         } else {
522                                 FindKeyMatch (ctx, XPathFunctions.ToString (o), result, iter.Current);
523                         }
524                         
525                         return new ListIterator (result, (iter.NamespaceManager as XsltCompiledContext), true);
526                 }
527                 
528                 void FindKeyMatch (XsltCompiledContext xsltContext, string value, ArrayList result, XPathNavigator context)
529                 {
530                         XPathNavigator searchDoc = context.Clone ();
531                         searchDoc.MoveToRoot ();
532                         if (key != null) {
533                                 XPathNodeIterator desc = searchDoc.SelectDescendants (XPathNodeType.All, true);
534
535                                 while (desc.MoveNext ()) {
536                                         if (key.Matches (desc.Current, xsltContext, value))
537                                                 AddResult (result, desc.Current);
538                                         
539                                         if (desc.Current.MoveToFirstAttribute ()) {
540                                                 do {
541                                                         if (key.Matches (desc.Current, xsltContext, value))
542                                                                 AddResult (result, desc.Current);       
543                                                 } while (desc.Current.MoveToNextAttribute ());
544                                                 
545                                                 desc.Current.MoveToParent ();
546                                         }
547                                 }
548                         }
549                 }
550
551                 void AddResult (ArrayList result, XPathNavigator nav)
552                 {
553                         for (int i = 0; i < result.Count; i++) {
554                                 XmlNodeOrder docOrder = nav.ComparePosition (((XPathNavigator)result [i]));
555                                 if (docOrder == XmlNodeOrder.Same)
556                                         return;
557                                 
558                                 if (docOrder == XmlNodeOrder.Before) {
559                                         result.Insert(i, nav.Clone ());
560                                         return;
561                                 }
562                         }
563                         result.Add (nav.Clone ());
564                 }
565         }
566         
567         class XsltSystemProperty : XPathFunction 
568         {
569                 Expression arg0;
570                 XmlNamespaceManager nsm;
571                 
572                 public XsltSystemProperty (FunctionArguments args, IStaticXsltContext ctx) : base (args)
573                 {
574                         if (args == null || args.Tail != null)
575                                 throw new XPathException ("system-property takes 1 arg");
576                         
577                         arg0 = args.Arg;
578                         nsm = ctx.GetNsm ();
579                 }
580                 
581                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
582                 public override object Evaluate (BaseIterator iter)
583                 {
584                         QName name = XslNameUtil.FromString (arg0.EvaluateString (iter), nsm);
585                         
586                         if (name.Namespace == Compiler.XsltNamespace) {
587                                 switch (name.Name) {
588                                         case "version": return "1.0";
589                                         case "vendor": return "Mono";
590                                         case "vendor-url": return "http://www.go-mono.com/";
591                                 }
592                         }
593                         
594                         return "";
595                 }
596         } 
597
598         class XsltUnparsedEntityUri : XPathFunction 
599         {
600                 Expression arg0;
601                 
602                 public XsltUnparsedEntityUri (FunctionArguments args) : base (args)
603                 {
604                         if (args == null || args.Tail != null)
605                                 throw new XPathException ("unparsed-entity-uri takes 1 arg");
606                         
607                         arg0 = args.Arg;
608                 }
609                 
610                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
611                 public override object Evaluate (BaseIterator iter)
612                 {
613                         IHasXmlNode xn = iter.Current as IHasXmlNode;
614                         if (xn == null)
615                                 return String.Empty;
616                         XmlNode n = xn.GetNode ();
617                         XmlDocumentType doctype = n.OwnerDocument.DocumentType;
618                         if (doctype == null)
619                                 return String.Empty;
620                         XmlEntity ent = doctype.Entities.GetNamedItem (arg0.EvaluateString (iter)) as XmlEntity;
621                         if (ent == null)
622                                 return String.Empty;
623                         else
624                                 return ent.BaseURI;
625                 }
626         }
627
628         class MSXslNodeSet : XPathFunction
629         {
630                 Expression arg0;
631
632                 public MSXslNodeSet (FunctionArguments args) : base (args)
633                 {
634                         if (args == null || args.Tail != null)
635                                 throw new XPathException ("element-available takes 1 arg");
636                         
637                         arg0 = args.Arg;
638                 }
639 \r
640                 public override XPathResultType ReturnType {\r
641                         get {\r
642                                 return XPathResultType.NodeSet;\r
643                         }\r
644                 }\r
645
646                 public override object Evaluate (BaseIterator iter)\r
647                 {\r
648                         XsltCompiledContext ctx = iter.NamespaceManager as XsltCompiledContext;\r
649                         XPathNavigator loc = iter.Current != null ? iter.Current.Clone () : null;\r
650                         XPathNavigator nav = arg0.EvaluateAs (iter, XPathResultType.Navigator) as XPathNavigator;\r
651                         if (nav == null) {\r
652                                 if (loc != null)\r
653                                         return new XsltException ("Cannot convert the XPath argument to a result tree fragment.", null, loc);\r
654                                 else\r
655                                         return new XsltException ("Cannot convert the XPath argument to a result tree fragment.", null);\r
656                         }\r
657                         ArrayList al = new ArrayList ();\r
658                         al.Add (nav);\r
659                         return new ListIterator (al, ctx, false);\r
660                 }
661         }
662 }