Пример #1
0
  /**
   * Show how to run two queries in tandem. The second query is applied to the results of the first.
   */
  public static void examplePipeline() throws XPathException {
    final Configuration config = new Configuration();

    // Compile the first query
    final StaticQueryContext sqc1 = config.newStaticQueryContext();
    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 = config.newStaticQueryContext();
    final XQueryExpression exp2 = sqc2.compileQuery("/a + 5");

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

    // Run the second query
    dynamicContext.clearParameters();
    dynamicContext.setContextItem(doc);
    final Object result = exp2.evaluateSingle(dynamicContext);
    System.out.println("3*3 + 5 = " + result);
    // The result is actually a java.lang.Double
  }
Пример #2
0
  /**
   * Show a query producing a single atomic value as its result and returning the value to the Java
   * application
   */
  public static void exampleToSingleton() throws XPathException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    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);
  }
Пример #3
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() throws XPathException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    final XQueryExpression exp =
        sqc.compileQuery("declare variable $in as xs:integer external;" + "$in * $in");

    final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
    dynamicContext.setParameterValue("in", Int64Value.makeIntegerValue(17));
    final Long result = (Long) exp.evaluateSingle(dynamicContext);
    System.out.println("17 * 17 = " + result);
  }
Пример #4
0
 /**
  * Show how to run a query that is read from a file and that serializes its output as HTML to
  * another file. The input to the query (the initial value of the context node) is supplied as the
  * content of another file.
  */
 public static void exampleToHTMLFile() throws XPathException, IOException {
   final Configuration config = new Configuration();
   final StaticQueryContext sqc = config.newStaticQueryContext();
   final XQueryExpression exp = sqc.compileQuery(new FileReader("query/books-to-html.xq"));
   final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
   dynamicContext.setContextItem(config.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);
 }
Пример #5
0
  /** Show a query producing a document as its result and serializing this directly to System.out */
  public static void exampleToStreamResult() throws XPathException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    final XQueryExpression exp = sqc.compileQuery("<a b='c'>{5+2}</a>");

    final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
    final Properties props = new Properties();
    props.setProperty(OutputKeys.METHOD, "xml");
    props.setProperty(OutputKeys.INDENT, "yes");
    exp.run(dynamicContext, new StreamResult(System.out), props);
  }
Пример #6
0
  /**
   * Show a query producing a sequence as its result and returning the sequence to the Java
   * application in the form of an iterator. The sequence is treated as if it were the content of a
   * document{...} constructor. Note that it must not contain free-standing attribute nodes.
   */
  public static void exampleToSerializedSequence() throws XPathException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    final XQueryExpression exp =
        sqc.compileQuery("<doc><chap><a>3</a></chap></doc>//a, <b>4</b>, 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);
  }
Пример #7
0
  /**
   * Show a query producing a sequence as its result and returning the sequence to the Java
   * application in the form of an iterator. The sequence is then output by wrapping the items in a
   * document, with wrapping elements indicating the type of each item, and serializing the
   * resulting document.
   */
  public static void exampleToWrappedSequence() throws XPathException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    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);
  }
Пример #8
0
  /**
   * Show a query producing a sequence as its result and returning the sequence to the Java
   * application in the form of an iterator. For each item in the result, its string value is
   * output.
   */
  public static void exampleToSequence() throws XPathException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    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;
      }
      System.out.println(item.getStringValue());
    }
  }
Пример #9
0
  /**
   * Show a query producing a DOM as its output. The DOM is then serialized using an identity
   * transform
   */
  public static void exampleToDOM() throws TransformerException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    final XQueryExpression exp =
        sqc.compileQuery("<a xmlns='http://a/uri' xmlns:a='another.uri'>text</a>");

    final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
    DOMResult result = new DOMResult();
    exp.run(dynamicContext, result, new Properties());

    // now serialize the DOM

    Transformer identity = TransformerFactory.newInstance().newTransformer();
    identity.transform(new DOMSource(result.getNode()), new StreamResult(System.out));
  }
Пример #10
0
  /**
   * Show a direct call from the Java application to a function defined in the Query. This is a very
   * efficient way of invoking a query, but it does minimal checking of the supplied arguments
   */
  public static void exampleDirectFunction() throws XPathException {
    final Configuration config = new Configuration();
    final StaticQueryContext sqc = config.newStaticQueryContext();
    final XQueryExpression exp1 =
        sqc.compileQuery(
            "declare namespace f='f.ns';"
                + "declare function f:t1($v1 as xs:integer, $v2 as xs:untypedAtomic*) { "
                + "   $v1 div $v2"
                + "};"
                + "10");

    final UserFunction fn1 = exp1.getStaticContext().getUserDefinedFunction("f.ns", "t1", 2);
    if (fn1 == null) {
      throw new IllegalStateException("Function f:t1() not found");
    }
    final Controller controller = exp1.newController();
    final Value[] arglist = new Value[2];
    arglist[0] = new Int64Value(10);
    for (int i = 3; i < 10; i++) {
      arglist[1] = new Int64Value(i);
      final ValueRepresentation result = fn1.call(arglist, controller);
      System.out.println(arglist[0] + " div " + arglist[1] + " = " + result);
    }
  }