Пример #1
0
    /**
     * Show a query producing a single atomic value as its result and returning the value
     * to the Java application
     */

    public static void ExampleToSingleton()
    {
        Processor        processor = new Processor();
        XQueryCompiler   compiler  = processor.NewXQueryCompiler();
        XQueryExecutable exp       = compiler.Compile("avg(for $i in 1 to 10 return $i * $i)");
        XQueryEvaluator  eval      = exp.Load();
        XdmAtomicValue   result    = (XdmAtomicValue)eval.EvaluateSingle();

        Console.WriteLine("Result type: " + result.Value.GetType());
        Console.WriteLine("Result value: " + (decimal)result.Value);
    }
Пример #2
0
    /**
     * Show a query that takes a parameter (external variable) as input.
     * The query produces a single atomic value as its result and returns the value
     * to the Java application. For the types of value that may be returned, and
     * their mapping to XPath data types, see {@link XPathEvaluator#Evaluate}
     */

    public static void ExampleWithParam()
    {
        Processor      processor = new Processor();
        XQueryCompiler compiler  = processor.NewXQueryCompiler();

        compiler.DeclareNamespace("p", "http://saxon.sf.net/ns/p");
        XQueryExecutable exp = compiler.Compile(
            "declare variable $p:in as xs:integer external; $p:in * $p:in");
        XQueryEvaluator eval = exp.Load();

        eval.SetExternalVariable(new QName("http://saxon.sf.net/ns/p", "p:in"), new XdmAtomicValue(12));
        XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();

        Console.WriteLine("Result type: " + result.Value.GetType());
        Console.WriteLine("Result value: " + (long)result.Value);
    }
Пример #3
0
    /**
     * Show a query consisting of two modules, using a QueryResolver to resolve
     * the "import module" declaration
     */

    public static void ExampleMultiModule()
    {
        String mod1 = "import module namespace m2 = 'http://www.example.com/module2';" +
                      "m2:square(3)";

        String mod2 = "module namespace m2 = 'http://www.example.com/module2';" +
                      "declare function m2:square($p) { $p * $p };";

        Processor      processor = new Processor();
        XQueryCompiler compiler  = processor.NewXQueryCompiler();

        InlineModuleResolver resolver = new InlineModuleResolver();

        resolver.AddModule(new Uri("http://www.example.com/module2"), mod2);
        compiler.QueryResolver = resolver;
        XQueryExecutable exp  = compiler.Compile(mod1);
        XQueryEvaluator  eval = exp.Load();

        XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();

        Console.WriteLine("Result type: " + result.Value.GetType());
        Console.WriteLine("Result value: " + (long)result.Value);
    }
Пример #4
0
        public int GetMetaID(String meta)
        {
            Processor      processor = new Processor();
            XQueryCompiler compiler  = processor.NewXQueryCompiler();

            //compiler.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
            compiler.BaseUri = BaseUri;
            XQueryExecutable exp  = compiler.Compile(XQueries.GetMetaID);
            XQueryEvaluator  eval = exp.Load();

            eval.SetExternalVariable(new QName("meta"), new XdmAtomicValue(meta));
            //XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
            XdmItem     value = eval.EvaluateSingle();
            IEnumerator e     = value.GetEnumerator();

            while (e.MoveNext())
            {
                //XdmItem item = (XdmItem)e.Current;
                //Console.WriteLine(item.ToString());
                return(Convert.ToInt32(e.Current.ToString()));
            }
            return(-1);
        }
Пример #5
0
    /**
     * Method main. First argument is the Saxon samples directory.
     */
    public static void Main(String[] argv)
    {
        String samplesDir;

        if (argv.Length > 0)
        {
            samplesDir = argv[0];
        }
        else
        {
            String home = Environment.GetEnvironmentVariable("SAXON_HOME");
            if (home == null)
            {
                Console.WriteLine("No input directory supplied, and SAXON_HOME is not set");
                return;
            }
            else
            {
                if (home.EndsWith("/") || home.EndsWith("\\"))
                {
                    samplesDir = home + "samples/";
                }
                else
                {
                    samplesDir = home + "/samples/";
                }
            }
        }

        UriBuilder ub = new UriBuilder();

        ub.Scheme = "file";
        ub.Host   = "";
        ub.Path   = samplesDir;
        Uri baseUri = ub.Uri;

        Console.WriteLine("Base URI: " + baseUri.ToString());

        // Create a schema-aware Processor

        Processor saxon = new Processor(true);

        // Load a schema

        SchemaManager manager = saxon.SchemaManager;

        manager.ErrorList = new ArrayList();
        Uri schemaUri = new Uri(baseUri, "data/books.xsd");

        try {
            manager.Compile(schemaUri);
        } catch (Exception e) {
            Console.WriteLine("Schema compilation failed with " + manager.ErrorList.Count + " errors");
            foreach (StaticError error in manager.ErrorList)
            {
                Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
            }
            return;
        }


        // Use this to validate an instance document

        SchemaValidator validator   = manager.NewSchemaValidator();
        Uri             instanceUri = new Uri(baseUri, "data/books.xml");

        validator.SetSource(instanceUri);
        validator.ErrorList = new ArrayList();
        XdmDestination psvi = new XdmDestination();

        validator.SetDestination(psvi);

        try {
            validator.Run();
        } catch (Exception e) {
            Console.WriteLine("Instance validation failed with " + validator.ErrorList.Count + " errors");
            foreach (StaticError error in validator.ErrorList)
            {
                Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
            }
        }


        // Run a query on the result to check that it has type annotations

        XQueryCompiler  xq = saxon.NewXQueryCompiler();
        XQueryEvaluator xv = xq.Compile("data((//PRICE)[1]) instance of xs:decimal").Load();

        xv.ContextItem = psvi.XdmNode;
        Console.WriteLine("Price is decimal? " + xv.EvaluateSingle().ToString());
    }