Package net.sf.saxon.query

Examples of net.sf.saxon.query.XQueryExpression


     */

    public static void exampleToSingleton() throws XPathException {
        final Configuration config = new Configuration();
        final StaticQueryContext sqc = new StaticQueryContext(config);
        final XQueryExpression exp = sqc.compileQuery("avg(for $i in 1 to 10 return $i*$i)");

        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        final BigDecimal result = (BigDecimal)exp.evaluateSingle(dynamicContext);
        System.out.println(result);

    }
View Full Code Here


     */

    public static void exampleToSequence() throws XPathException {
        final Configuration config = new Configuration();
        final StaticQueryContext sqc = new StaticQueryContext(config);
        final XQueryExpression exp = sqc.compileQuery("for $i in 1 to 10 return ($i * $i)");

        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        final SequenceIterator iter = exp.iterator(dynamicContext);
        while (true) {
            Item item = iter.next();
            if (item == null) {
                break;
            }
View Full Code Here

     */

    public static void exampleToSerializedSequence() throws XPathException {
        final Configuration config = new Configuration();
        final StaticQueryContext sqc = new StaticQueryContext(config);
        final XQueryExpression exp = sqc.compileQuery(
                "<doc><chap><a>3</a></chap></doc>//a, <b>4</b>, attribute c {5}, 19");
        Properties props = new Properties();
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        final SequenceIterator iter = exp.iterator(dynamicContext);
        QueryResult.serializeSequence(iter, config, System.out, props);
    }
View Full Code Here

     */

    public static void exampleToWrappedSequence() throws XPathException {
        final Configuration config = new Configuration();
        final StaticQueryContext sqc = new StaticQueryContext(config);
        final XQueryExpression exp = sqc.compileQuery(
                "<doc><chap><a>3</a></chap></doc>//a, <b>4</b>, attribute c {5}, 19");
        Properties props = new Properties();
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        props.setProperty(OutputKeys.INDENT, "yes");

        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        final SequenceIterator iter = exp.iterator(dynamicContext);
        final DocumentInfo doc = QueryResult.wrap(iter, config);
        QueryResult.serialize(doc, new StreamResult(System.out), props, config);
    }
View Full Code Here

     */

    public static void exampleToHTMLFile() throws XPathException, IOException {
        final Configuration config = new Configuration();
        final StaticQueryContext sqc = new StaticQueryContext(config);
        final XQueryExpression exp = sqc.compileQuery(new FileReader("query/books-to-html.xq"));
        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        dynamicContext.setContextNode(sqc.buildDocument(new StreamSource("data/books.xml")));
        final Properties props = new Properties();
        props.setProperty(OutputKeys.METHOD, "html");
        props.setProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD HTML 4.01 Transitional//EN");
        exp.run(dynamicContext, new StreamResult(new File("booklist.html")), props);
    }
View Full Code Here

     */

    public static void exampleWithParam() throws XPathException {
        final Configuration config = new Configuration();
        final StaticQueryContext sqc = new StaticQueryContext(config);
        final XQueryExpression exp = sqc.compileQuery("declare variable $in as xs:integer external;" +
                "$in * $in");

        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        dynamicContext.setParameter("in", new Long(17));
        final Long result = (Long)exp.evaluateSingle(dynamicContext);
        System.out.println("17 * 17 = " + result);

    }
View Full Code Here

    public static void examplePipeline() throws XPathException {
        final Configuration config = new Configuration();

        // Compile the first query
        final StaticQueryContext sqc1 = new StaticQueryContext(config);
        final XQueryExpression exp1 = sqc1.compileQuery(
                "declare variable $in as xs:integer external;" +
                "document{ <a>{$in * $in}</a> }"
        );

        // Compile the second query (each query should have its own static context)
        final StaticQueryContext sqc2 = new StaticQueryContext(config);
        final XQueryExpression exp2 = sqc2.compileQuery(
                "/a + 5"
        );

        // Run the first query
        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        dynamicContext.setParameter("in", new Long(3));
        final NodeInfo doc = (NodeInfo)exp1.evaluateSingle(dynamicContext);

        // Run the second query
        dynamicContext.clearParameters();
        dynamicContext.setContextNode(doc);
        final Object result = exp2.evaluateSingle(dynamicContext);
        System.out.println("3*3 + 5 = " + result);
        // The result is actually a java.lang.Double
    }
View Full Code Here

     * checking of the supplied arguments
     */

    public static void exampleDirectFunction() throws XPathException {
        final StaticQueryContext sqc = new StaticQueryContext(new Configuration());
        final XQueryExpression exp1 = sqc.compileQuery("declare namespace f='f.ns';" +
                "declare function f:t1($v1 as xs:integer, $v2 as xdt:untypedAtomic*) { " +
                "   $v1 div $v2" +
                "};" +
                "10");

        final UserFunction fn1 = sqc.getUserDefinedFunction("f.ns", "t1", 2);
        final Controller controller = exp1.getController();
        final Value[] arglist = new Value[2];
        arglist[0] = new IntegerValue(10);
        for (int i=3; i<10; i++) {
            arglist[1] = new IntegerValue(i);
            final Value result = fn1.call(arglist, controller);
View Full Code Here

        Configuration config = null;
        Properties props = new Properties();
        props.setProperty(OutputKeys.METHOD, "xml");
        props.setProperty(OutputKeys.INDENT, "yes");

        XQueryExpression exp = implementation.getCompiledExpressionsCache().get(xqExpression);
        if (exp == null) {
            config = new Configuration();
            StaticQueryContext sqc = new StaticQueryContext(config);
            exp = sqc.compileQuery(xqExpression);
            implementation.getCompiledExpressionsCache().put(xqExpression, exp);
        } else {
            config = exp.getStaticContext().getConfiguration();
        }

        Object[] params = prepareParameters(payload, config, props);

        DynamicQueryContext dynamicContext = new DynamicQueryContext(config);

        // Setting the parameters for function invocation
        String methodName = theMethod.getName();
        for (int i = 0; i < params.length; i++) {
            dynamicContext.setParameter(methodName + "_" + i, params[i]);
        }

        // Setting references
        for (Map.Entry<String, Object> entry : referenceProxies.entrySet()) {
            dynamicContext.setParameter(entry.getKey(), entry.getValue());
        }

        // Setting properties
        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            dynamicContext.setParameter(entry.getKey(), transformProperty(entry.getValue(), config));
        }

        SequenceIterator iterator = null;
        Configuration oldConfigValue = SaxonDataBindingHelper.CURR_EXECUTING_CONFIG;
        SaxonDataBindingHelper.CURR_EXECUTING_CONFIG = config;
        try {
            iterator = exp.iterator(dynamicContext);
        } finally {
            SaxonDataBindingHelper.CURR_EXECUTING_CONFIG = oldConfigValue;
        }
        Item item = iterator.next();
        if (item == null) {
View Full Code Here

        xqExpression += "\r\n<dummy></dummy>";

        Configuration config = new Configuration();
        StaticQueryContext sqc = new StaticQueryContext(config);
        XQueryExpression exp = null;
        try {
            exp = sqc.compileQuery(xqExpression);
        } catch (XPathException e) {
            throw new ContributionResolveException(e);
        }
View Full Code Here

TOP

Related Classes of net.sf.saxon.query.XQueryExpression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.