Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress indentation for XML output in XQuery

Is there a way from within an XQuery to remove indentation of the XML output?

Eg. say I had an XQuery of;

<foo><bar/></foo>

producing an XML result document of;

<foo>
  <bar/>
</foo>

How can I remove the indents so the output document looked like this;

<foo>
<bar/>
</foo>

Ideally I want something I can control from with the XQuery itself eg. in the declarations at the start of the query. I've tried putting things like this in the XQuery;

declare namespace saxon="http://saxon.sf.net/";
declare option saxon:output "indent=no";

And several other variations of the above depending on what documentation Google throws up, but the XML output never changes.

I'm using Saxon and calling it via the Java XQJ extensions;

import net.sf.saxon.xqj.SaxonXQDataSource;

Is it something I would have to do in Java not Xquery?

Update

This is the code I'm using to call Saxon. I'm sorry there's rather a lot of it but I'm not sure what will be relevant;

private String runXQuery(String query, HttpServletRequest request, String payload)
 throws XQException {

  XQDataSource ds = new SaxonXQDataSource();
  XQConnection conn = ds.getConnection();

  XQPreparedExpression exp = conn.prepareExpression(query);

  bindObject(exp, "HTTP_METHOD", request.getMethod());
  bindObject(exp, "HTTP_URI", request.getRequestURI());
  bindObject(exp, "HTTP_QUERY", request.getQueryString());
  bindObject(exp, "HTTP_COOKIES", request.getHeader("Cookie"));
  bindObject(exp, "HTTP_PAYLOAD", payload);

  XQResultSequence result = exp.executeQuery();          // Run the XQuery.

  StringBuffer buffer = new StringBuffer();

  while (result.next()) {
    buffer.append(result.getItemAsString(null));
    buffer.append(System.getProperty("line.separator"));
  }

  return buffer.toString();

}

The above is called like this;

public void handle(String target, Request baseRequest, HttpServletRequest request,
 HttpServletResponse response) throws IOException, ServletException {

  response.setContentType("text/html;charset=utf-8");
  baseRequest.setHandled(true);

  File file = null;
  String out = "";

  File inbound = new File(root, target);        // File or folder
  file = checkFile(inbound);                    // File.
  String xquery = loadFile(file);
  String payload = getPayload(request.getReader());
  out = runXQuery(xquery, request, payload);
  response.setStatus(HttpServletResponse.SC_OK);
  response.getWriter().println(out);

}

As far as I know, I'm just outputting whatever comes back from executeQuery() as plain text.

The program works as a sort of XQuery server. It listens on a specific port for a request from an HTTP client for a specific XQuery file. It then loads that file and passes it to Saxon to run, then outputs the result from Saxon back to the HTTP client.

like image 896
Nigel Alderton Avatar asked Oct 06 '11 08:10

Nigel Alderton


1 Answers

Instead of passing null in

buffer.append(result.getItemAsString(null));

you should pass a Properties object, as suggested by the documentation for getItemAsString, which contains an indent key set to "no" as documented in the XSLT 2.0 and XQuery 1.0 Serialization reference.

Actually, this is not an XQuery execution issue, but a question how the result of the XQuery which is actually a node set without any formatting at all is converted to the string or StringBuffer which then contains formatting.

like image 163
Frank Avatar answered Oct 15 '22 21:10

Frank