如何在 XQuery 中抑制 XML 输出的缩进

发布于 2024-12-08 08:25:31 字数 2450 浏览 0 评论 0原文

有没有办法从 XQuery 中删除 XML 输出的缩进?

例如。假设我有一个 XQuery;

<foo><bar/></foo>

生成 XML 结果文档;

<foo>
  <bar/>
</foo>

如何删除缩进,使输出文档看起来像这样;

<foo>
<bar/>
</foo>

理想情况下,我想要一些可以通过 XQuery 本身控制的东西,例如。在查询开始的声明中。我尝试过将这样的东西放入 XQuery 中;

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

以及上述的其他几种变体,具体取决于 Google 提供的文档,但 XML 输出永远不会改变。

我正在使用 Saxon 并通过 Java XQJ 扩展调用它;

import net.sf.saxon.xqj.SaxonXQDataSource;

这是我必须用 Java 而不是 Xquery 做的事情吗?

更新

这是我用来调用 Saxon 的代码。抱歉,内容相当多,但我不确定哪些是相关的;

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();

}

上面是这样调用的;

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);

}

据我所知,我只是将 executeQuery() 返回的内容作为纯文本输出。

该程序作为一种 XQuery 服务器运行。它在特定端口上侦听来自 HTTP 客户端对特定 XQuery 文件的请求。然后它加载该文件并将其传递给 Saxon 运行,然后将结果从 Saxon 输出回 HTTP 客户端。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无声情话 2024-12-15 08:25:31

您应该传递一个 Properties 对象,而不是传递 null

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

,如 getItemAsString,其中包含设置为的 indent“no”XSLT 2.0 中所述和 XQuery 1.0 序列化参考

实际上,这不是一个XQuery执行问题,而是一个问题:XQuery的结果实际上是一个没有任何格式化的节点集,如何转换为包含格式化的字符串或StringBuffer

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.

夜唯美灬不弃 2024-12-15 08:25:31

这里出了什么问题并不明显。但您还没有解释如何生成输出。您究竟如何在 XQJ 中运行查询,以及将其输出发送到哪里? (根据您提供的信息,序列化可能甚至不是由查询处理器完成的 - 例如,您可能将输出写入 DOM,然后序列化 DOM。)

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.)

慕巷 2024-12-15 08:25:31

除了 Gunther 声明之外,您还可以在 XQuery Prolog 中定义此选项:

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

如果您没有绑定到 saxon,BaseX 提供了设置indents选项的可能性(类似于saxon的indent-spaces)并且是免费的。

您只需要使用以下两行:

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

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";
不奢求什么 2024-12-15 08:25:31

你可以尝试声明选项 saxon:output "method=text";或者您可以尝试声明选项 saxon:output "method=xml";

如果它们不起作用,您可以在输出之前去除特殊字符并修剪它们。干杯!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文