Project

Profile

Help

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

he / latest9.4 / hen / csource / samples / SampleExtensions.cs @ c3c84ba2

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 Examples.cs program.
14
    /// </summary>
15

    
16
    public class SampleExtensions
17
    {
18

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

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

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

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

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

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

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

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

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

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

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

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

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

    
106
    }
107
}
(8-8/14)