Project

Profile

Help

How to connect?
Download (3.1 KB) Statistics
| Branch: | Revision:

he / src / samples / cs / SampleExtensions.cs @ be17ef52

1
using System;
2
using System.Collections;
3
using System.Text;
4
using Saxon.Api;
5
using System.Xml;
6
using System.Collections.Generic;
7

    
8
namespace SampleExtensions
9
{
10

    
11
    /// <summary>
12
    /// This class contains some example methods that can be invoked from XSLT as
13
    /// extension functions or from XQuery as external functions. For examples of calls
14
	/// on these functions, from both XSLT and XQuery, see the ExamplesPE.cs and 
15
	/// ExamplesEE.cs programs.
16
    /// </summary>
17

    
18
    public class SampleExtensions
19
    {
20

    
21
        /// <summary>
22
        /// Add two numbers
23
        /// </summary>
24

    
25
        public static double add(double one, double two)
26
        {
27
            return one + two;
28
        }
29

    
30
        /// <summary>
31
        /// Get the average of an array of numbers
32
        /// </summary>
33

    
34
        public static double average(double[] numbers)
35
        {
36
            double total = 0.0e0;
37
            foreach (double d in numbers)
38
            {
39
                total += d;
40
            }
41
            return total / numbers.Length;
42
        }
43

    
44
        /// <summary>
45
        /// Get the current host language from the Saxon context
46
        /// </summary>
47

    
48
        public static string hostLanguage(net.sf.saxon.expr.XPathContext context)
49
        {
50
            var lang = context.getController().getExecutable().getHostLanguage();
51
            if (lang == net.sf.saxon.s9api.HostLanguage.XQUERY)
52
            {
53
                return "XQuery";
54
            }
55
            else if (lang == net.sf.saxon.s9api.HostLanguage.XSLT)
56
            {
57
                return "XSLT";
58
            }
59
            else if (lang == net.sf.saxon.s9api.HostLanguage.XPATH)
60
            {
61
                return "XPath";
62
            }
63
            else
64
            {
65
                return "unknown";
66
            }
67
        }
68

    
69
        /// <summary>
70
        /// Get the local name of the first child of the node supplied as an argument
71
        /// (Shows that an extension function can accept an XmlNode: this only works if
72
        /// the input document is a wrapper around an XmlDocument)
73
        /// </summary>
74

    
75
        public static string nameOfFirstChild(XmlNode current)
76
        {
77
            return current.FirstChild.LocalName;
78
        }
79

    
80
        /// <summary>
81
        /// Get the first child of the node supplied as an argument
82
        /// (Shows that an extension function can return an XmlNode)
83
        /// </summary>
84

    
85
        public static XmlNode FirstChild(XmlNode current)
86
        {
87
            return current.FirstChild;
88
        }
89

    
90
        /// <summary>
91
        /// Accept a node and an atomic value and return the sequence containing the string
92
        /// value of the node followed by the value.
93
        /// </summary>
94

    
95
        public static XdmValue combine(XdmNode node, XdmAtomicValue value)
96
        {
97
            IList<XdmItem> list = new List<XdmItem>();
98
            list.Add(new XdmAtomicValue(node.StringValue));
99
            list.Add(value);
100
            return new XdmValue(list);
101
        }
102

    
103
        //public static string shorten(string instr) {
104
        //    throw new NullReferenceException("thrown deliberately");
105
        //    return String.Empty;
106
        //}
107

    
108
    }
109
}
(4-4/5)