admin管理员组文章数量:1312751
declare namespace xf = "//test"
declare namespace output ="http://www.w3./2010/xslt-xquery-serialization";
declare option output: method "text";
declare function xf:test($test as element(*)) as item () * {
for $row in $test/*:row
return ('"<row", "',data($row), '"')
};
declare variable $test as element(*) external;
xf:test($test)
Even after adding declare option output: method "test"; the output is not in text format . I can set output method "text" using Saxon API, but its not working directly in xquery.
Input xml
<all>
<row>one</row>
<row>two</row>
<row>three</row>
<all>
Actual Output
"<row>", "one"
"<row>", "two"
"<row>", "three"
Expected Output
"<row>","one"
"<row>","two"
"<row>","three"
Here is the sample code . When i use getSerializationParameters and set property , it works in the API.
import com.saxonica.xqj.SaxonXQDataSource;
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream(
new File("resources/META-INF/xquery//test.xq"));
SaxonXQDataSource ds = new SaxonXQDataSource();
Configuration a = ds.getConfiguration();
((SaxonXQDataSource) ds).getConfiguration().getDefaultStaticQueryContext()
.setLanguageVersion(30);
a.getDefaultStaticQueryContext().setLanguageVersion(31);
XQConnection conn = ds.getConnection();
XQPreparedExpression exp = conn.prepareExpression(inputStream);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
InputStream payloadInputStream = new FileInputStream(
new File("resources/META-INF/msgs/MsgReq6.xml"));
String payloadMsg = null;
try (Scanner scanner = new Scanner(payloadInputStream, StandardCharsets.UTF_8.name())) {
payloadMsg = scanner.useDelimiter("\\A").next();
}
exp.bindNode(new QName("test"), convertStringToDocument(payloadMsg).getDocumentElement(),
null);
XQResultSequence result = exp.executeQuery();
while (result.next()) {
System.out.println(result.getItemAsString(getSerializationParameters()));
}
}
public static Properties getSerializationParameters() {
Properties properties = new Properties();
properties.setProperty("method", "text");
return properties;
}
public static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
return doc;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
declare namespace xf = "http://tempuri.//test"
declare namespace output ="http://www.w3./2010/xslt-xquery-serialization";
declare option output: method "text";
declare function xf:test($test as element(*)) as item () * {
for $row in $test/*:row
return ('"<row", "',data($row), '"')
};
declare variable $test as element(*) external;
xf:test($test)
Even after adding declare option output: method "test"; the output is not in text format . I can set output method "text" using Saxon API, but its not working directly in xquery.
Input xml
<all>
<row>one</row>
<row>two</row>
<row>three</row>
<all>
Actual Output
"<row>", "one"
"<row>", "two"
"<row>", "three"
Expected Output
"<row>","one"
"<row>","two"
"<row>","three"
Here is the sample code . When i use getSerializationParameters and set property , it works in the API.
import com.saxonica.xqj.SaxonXQDataSource;
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream(
new File("resources/META-INF/xquery//test.xq"));
SaxonXQDataSource ds = new SaxonXQDataSource();
Configuration a = ds.getConfiguration();
((SaxonXQDataSource) ds).getConfiguration().getDefaultStaticQueryContext()
.setLanguageVersion(30);
a.getDefaultStaticQueryContext().setLanguageVersion(31);
XQConnection conn = ds.getConnection();
XQPreparedExpression exp = conn.prepareExpression(inputStream);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
InputStream payloadInputStream = new FileInputStream(
new File("resources/META-INF/msgs/MsgReq6.xml"));
String payloadMsg = null;
try (Scanner scanner = new Scanner(payloadInputStream, StandardCharsets.UTF_8.name())) {
payloadMsg = scanner.useDelimiter("\\A").next();
}
exp.bindNode(new QName("test"), convertStringToDocument(payloadMsg).getDocumentElement(),
null);
XQResultSequence result = exp.executeQuery();
while (result.next()) {
System.out.println(result.getItemAsString(getSerializationParameters()));
}
}
public static Properties getSerializationParameters() {
Properties properties = new Properties();
properties.setProperty("method", "text");
return properties;
}
public static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
return doc;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Share Improve this question edited Feb 7 at 16:38 user29408236 asked Feb 1 at 16:18 user29408236user29408236 11 silver badge2 bronze badges 6- 1 Please provide all details to allow us to easily reproduce the problem, such as Saxon version, the code (e.g. command line or Java) you use to run the XQuery. – Martin Honnen Commented Feb 1 at 16:20
- I have tried it with Saxon-HE-9.7.0-14 jar . I have also tried with Saxon 10.1 version. I am using Java code in which I create a SaxonDataSource and invoke a file having xquery code written above. – user29408236 Commented Feb 4 at 20:55
- Post minimal but complete sample code to allow others to reproduce the problem. It doesn't help if you post code containing typos to ask us to look at, then once we identify that error you tell us it is not in your code. 9.7 is obviously quite old, but I don't think it matters for XQuery serialization. Not sure exactly what a SaxonDataSource is and where you got the idea to use it, I would start with the official samples that accompany each release, it has Java code on how to run XQuery. – Martin Honnen Commented Feb 4 at 21:20
- You might be using XQJ to run the XQuery, I must admit I have never used it with Saxon, always went with s9api where you can have a Serializer as the Destination of a query. I am not sure whether XQJ is concerned with serialization and if so, it uses the way defined after its creation, i.e. I think XQJ is older than the XQuery/XSLT serialization spec w3./2010/xslt-xquery-serialization. So not sure whether it is a deficit of the API you use or of your code, might want to try s9api. – Martin Honnen Commented Feb 4 at 21:35
- I am using xqj to run xquery. I have included sample code above , when I use getItemAsString(getSerializationParameters()) and set output as text in the method, I can see expected output. – user29408236 Commented Feb 7 at 16:40
1 Answer
Reset to default 0I believe that the XQJ API does not support serialization using parameters defined within the query.
Unfortunately one of the rules of the XQJ license conditions is that if you implement the API, you aren't allowed to extend it, so Saxon does what the XQJ spec says and nothing more. (On one interpretation Saxon is even in breach of those license conditions simply by allowing XQuery 3.1 to be used via this API).
I think XQJ is a dead end and would encourage you to use Saxon's s9api interface instead.
You might be able to escape from the XQJ view of the world into the underlying Saxon classes: cast the XQExecutable
to a SaxonXQExecutable
, from that get the Saxon XQueryExpression
, then the (Saxon) Executable
, from that getPrimarySerializationProperties().getProperties()
and then you can feed these properties back into XQJ. But that's so tortuous that by the time you've done this, you've lost any benefits you might have gained by using a "standard" API rather than Saxon's native API.
本文标签: saxonNot able to set outputmethod quottextquot in XqueryStack Overflow
版权声明:本文标题:saxon - Not able to set output:method "text" in Xquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741870452a2402161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论