Is there a way from within an XQuery to remove indentation of the XML output?
是否有一种方法可以从XQuery中删除XML输出的缩进?
Eg. say I had an XQuery of;
如。假设我有一个XQuery;
<foo><bar/></foo>
producing an XML result document of;
生成XML结果文档;
<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;
理想情况下,我想要一些我可以控制的东西,比如XQuery本身。在查询开头的声明中。我尝试过在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.
根据谷歌提供的文档,以及上面的其他一些变体,但是XML输出不会改变。
I'm using Saxon and calling it via the Java XQJ extensions;
我使用Saxon并通过Java XQJ扩展调用它;
import net.sf.saxon.xqj.SaxonXQDataSource;
Is it something I would have to do in Java not Xquery?
这是我在Java中必须做的事,而不是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;
这是我用来调用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();
}
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.
就我所知,我只是将从executeQuery()返回的内容输出为纯文本。
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.
该程序作为一种XQuery服务器工作。它侦听来自HTTP客户端的特定端口对特定XQuery文件的请求。然后它加载该文件并将其传递给Saxon以运行,然后将结果从Saxon返回到HTTP客户端。
4 个解决方案
#1
1
Instead of passing null
in
而不是传入null
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.
您应该传递一个属性对象,正如getItemAsString文档中所建议的那样,该对象包含一个缩进键,在XSLT 2.0和XQuery 1.0序列化引用中记录为“no”。
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.
实际上,这并不是一个XQuery执行问题,而是一个问题:实际上是一个节点集的XQuery是如何将结果转换为字符串或StringBuffer的,并包含格式。
#2
0
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.)
这里的问题并不明显。但是您还没有解释如何生成输出。确切地说,您是如何在XQJ中运行查询的,以及在何处发送其输出?(根据您提供的信息,序列化甚至可能不是由查询处理器完成的——例如,您可能正在将输出写入DOM,然后将DOM序列化。)
#3
0
Additional to what Gunther statet, you've also got the possibility to define this option in XQuery Prolog:
除了Gunther statet之外,您还可以在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.
如果您不受saxon的约束,BaseX提供了设置indents选项(类似于saxon的学生空间)的可能性,而且是免费的。
You would just need to use the following two lines:
您只需要使用以下两行:
declare option output:indent "yes";
declare option output:indents "0";
#4
0
could you try declare option saxon:output "method=text"; or could you try declare option saxon:output "method=xml";
能否尝试声明选项saxon:output "method=text";或者您可以尝试声明选项saxon:输出“method=xml”;
If they dont work, you could strip the special characters and trim them before outputting. Cheers!
如果他们不工作,你可以剥掉特殊的人物,然后在输出之前修剪。干杯!
#1
1
Instead of passing null
in
而不是传入null
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.
您应该传递一个属性对象,正如getItemAsString文档中所建议的那样,该对象包含一个缩进键,在XSLT 2.0和XQuery 1.0序列化引用中记录为“no”。
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.
实际上,这并不是一个XQuery执行问题,而是一个问题:实际上是一个节点集的XQuery是如何将结果转换为字符串或StringBuffer的,并包含格式。
#2
0
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.)
这里的问题并不明显。但是您还没有解释如何生成输出。确切地说,您是如何在XQJ中运行查询的,以及在何处发送其输出?(根据您提供的信息,序列化甚至可能不是由查询处理器完成的——例如,您可能正在将输出写入DOM,然后将DOM序列化。)
#3
0
Additional to what Gunther statet, you've also got the possibility to define this option in XQuery Prolog:
除了Gunther statet之外,您还可以在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.
如果您不受saxon的约束,BaseX提供了设置indents选项(类似于saxon的学生空间)的可能性,而且是免费的。
You would just need to use the following two lines:
您只需要使用以下两行:
declare option output:indent "yes";
declare option output:indents "0";
#4
0
could you try declare option saxon:output "method=text"; or could you try declare option saxon:output "method=xml";
能否尝试声明选项saxon:output "method=text";或者您可以尝试声明选项saxon:输出“method=xml”;
If they dont work, you could strip the special characters and trim them before outputting. Cheers!
如果他们不工作,你可以剥掉特殊的人物,然后在输出之前修剪。干杯!