XQDataSource dataSource = shell.getDataSource(); XQuery xquery = dataSource.newQuery(query); xquery.setVariable(dataSource.newVariable("uri", XQVariableType.XS_STRING, uri)); shell.runXQuery(xquery, true, false);
String query = "if(doc(\"" + uri + "\")) then xdmp:document-delete(\"" + uri + "\") " + "else \"Document not found.\""; ShellQuery squery = new ShellQuery(query, shell); shell.runXQuery(squery.asXQuery(), true, false);
	public void execute(Environment env, String commandline) {		if (commandline != null && commandline.length() > 0) {			if (env instanceof Shell) {				Shell shell = (Shell)env;				String[] tokens = commandline.split("\\s+");				CommandLineParser parser = new PosixParser();				CommandLine cmd = null;				try {					cmd = parser.parse(options, tokens);				} catch (ParseException e) {					env.printError(e);					return;				}								String xpath = cmd.getOptionValue("x");				if( xpath != null && xpath.length() > 0) {					String key = "n";					if (cmd.hasOption("f")) {						key = "y";					} else {						try {							key = shell.getConsole().readLine("remove all documents matching '"									                          + xpath + "'? (N|y) ");						} catch (IOException e) {							shell.printLine("Failed to read char from console");							shell.printError(e);						}					}					if (key.equalsIgnoreCase("y")) {							String query = "concat(xs:string(count(for $n in (" + xpath + ")" 									      +" return (xdmp:document-delete(base-uri($n)), <done/>))), "							               + "\" documents removed.\");";							try {								XQDataSource dataSource = shell.getDataSource();								XQuery xquery = dataSource.newQuery(query);								shell.runXQuery(xquery, true, false);							} catch (XQException e) {								shell.printError(e);							}					} else {						shell.printLine("");					}				} else {					for (Iterator i = cmd.getArgList().iterator(); i.hasNext();) {						String uri = (String) i.next();						String key = "n";						if (cmd.hasOption("f")) {							key = "y";						} else {							try {								key = shell.getConsole().readLine("remove '" + uri + "'? (N|y) ");							} catch (IOException e) {								shell.printLine("Failed to read char from console");								shell.printError(e);							}						}						if (key.equalsIgnoreCase("y")) {							String query = "define variable $uri as xs:string external ";							query += "if(doc($uri)) then xdmp:document-delete($uri) "									+ "else \"Document not found.\"";							try {								XQDataSource dataSource = shell.getDataSource();								XQuery xquery = dataSource.newQuery(query);								xquery.setVariable(dataSource.newVariable("uri",										XQVariableType.XS_STRING, uri));								shell.runXQuery(xquery, true, false);							} catch (XQException e) {								shell.printError(e);							}						} else {							shell.printLine("");						}					}				}			}		} else {			env.printLine("Please specify document(s) to remove. See help rm.");		}	}