开发者

How to suppress indentation for XML output in XQuery

开发者 https://www.devze.com 2023-04-10 21:17 出处:网络
Is there a way from within an XQuery to remove indentation of the XML output? Eg. say I had an XQuery of;

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.


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.


It's not obvious what's wrong here. But you haven't explained how you are generating the output. Exactly how are you running the query in XQJ, and where are you sending its output? (From the information you've given, it might be that the serialisation isn't even being done by the query processor - for example you might be writing output to a DOM and then serialising the DOM.)


Additional to what Gunther statet, you've also got the possibility to define this option in XQuery Prolog:

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

If you're not bound to saxon, BaseX offers the possibility to set the indents-option (similiar to saxon's indent-spaces) and is free.

You would just need to use the following two lines:

declare option output:indent "yes";
declare option output:indents "0";


could you try declare option saxon:output "method=text"; or could you try declare option saxon:output "method=xml";

If they dont work, you could strip the special characters and trim them before outputting. Cheers!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号