@@ -375,4 +375,78 @@ public void deleteExpr() throws EXistException, RecognitionException, XPathExcep
375
375
assertEquals (Expression .Category .UPDATING , expr .getFirst ().getCategory ());
376
376
}
377
377
}
378
+
379
+ @ Test
380
+ public void replaceNodeExpr () throws EXistException , RecognitionException , XPathException , TokenStreamException , PermissionDeniedException
381
+ {
382
+ String query =
383
+ "replace node fn:doc(\" bib.xml\" )/books/book[1]/publisher\n " +
384
+ "with fn:doc(\" bib.xml\" )/books/book[2]/publisher" ;
385
+
386
+ BrokerPool pool = BrokerPool .getInstance ();
387
+ try (final DBBroker broker = pool .getBroker ()) {
388
+ // parse the query into the internal syntax tree
389
+ XQueryContext context = new XQueryContext (broker .getBrokerPool ());
390
+ XQueryLexer lexer = new XQueryLexer (context , new StringReader (query ));
391
+ XQueryParser xparser = new XQueryParser (lexer );
392
+ xparser .expr ();
393
+ if (xparser .foundErrors ()) {
394
+ fail (xparser .getErrorMessage ());
395
+ return ;
396
+ }
397
+
398
+ XQueryAST ast = (XQueryAST ) xparser .getAST ();
399
+
400
+ XQueryTreeParser treeParser = new XQueryTreeParser (context );
401
+ PathExpr expr = new PathExpr (context );
402
+ treeParser .expr (ast , expr );
403
+
404
+ if (treeParser .foundErrors ()) {
405
+ fail (treeParser .getErrorMessage ());
406
+ return ;
407
+ }
408
+
409
+ assertTrue (expr .getFirst () instanceof ReplaceExpr );
410
+ assertEquals (ReplaceExpr .ReplacementType .NODE ,((ReplaceExpr ) expr .getFirst ()).getReplacementType ());
411
+ assertEquals ("doc(\" bib.xml\" )/child::{}books/child::{}book[1]/child::{}publisher" ,((ReplaceExpr ) expr .getFirst ()).getTargetExpr ().toString ());
412
+ assertEquals ("doc(\" bib.xml\" )/child::{}books/child::{}book[2]/child::{}publisher" ,((ReplaceExpr ) expr .getFirst ()).getWithExpr ().toString ());
413
+ }
414
+ }
415
+
416
+ @ Test
417
+ public void replaceValueExpr () throws EXistException , RecognitionException , XPathException , TokenStreamException , PermissionDeniedException
418
+ {
419
+ String query =
420
+ "replace value of node fn:doc(\" bib.xml\" )/books/book[1]/price\n " +
421
+ "with fn:doc(\" bib.xml\" )/books/book[1]/price * 1.1" ;
422
+
423
+ BrokerPool pool = BrokerPool .getInstance ();
424
+ try (final DBBroker broker = pool .getBroker ()) {
425
+ // parse the query into the internal syntax tree
426
+ XQueryContext context = new XQueryContext (broker .getBrokerPool ());
427
+ XQueryLexer lexer = new XQueryLexer (context , new StringReader (query ));
428
+ XQueryParser xparser = new XQueryParser (lexer );
429
+ xparser .expr ();
430
+ if (xparser .foundErrors ()) {
431
+ fail (xparser .getErrorMessage ());
432
+ return ;
433
+ }
434
+
435
+ XQueryAST ast = (XQueryAST ) xparser .getAST ();
436
+
437
+ XQueryTreeParser treeParser = new XQueryTreeParser (context );
438
+ PathExpr expr = new PathExpr (context );
439
+ treeParser .expr (ast , expr );
440
+
441
+ if (treeParser .foundErrors ()) {
442
+ fail (treeParser .getErrorMessage ());
443
+ return ;
444
+ }
445
+
446
+ assertTrue (expr .getFirst () instanceof ReplaceExpr );
447
+ assertEquals (ReplaceExpr .ReplacementType .VALUE ,((ReplaceExpr ) expr .getFirst ()).getReplacementType ());
448
+ assertEquals ("doc(\" bib.xml\" )/child::{}books/child::{}book[1]/child::{}price" ,((ReplaceExpr ) expr .getFirst ()).getTargetExpr ().toString ());
449
+ assertEquals ("doc(\" bib.xml\" )/child::{}books/child::{}book[1]/child::{}price * 1.1" ,((ReplaceExpr ) expr .getFirst ()).getWithExpr ().toString ());
450
+ }
451
+ }
378
452
}
0 commit comments