Project

Profile

Help

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

he / tags / 9.8.0.2 / samples / cs / SampleExtensions.cs @ 02f8308b

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

    
7
namespace SampleExtensions
8
{
9

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

    
17
    public class SampleExtensions
18
    {
19

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

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

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

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

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

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

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

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

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

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

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

    
94
        public static XdmValue combine(XdmNode node, XdmAtomicValue value)
95
        {
96
            ArrayList list = new ArrayList();
97
            list.Add(new XdmAtomicValue(node.StringValue));
98
            list.Add(value);
99
            return new XdmValue(list);
100
        }
101

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

    
107
    }
108
}
(4-4/5)