Skip to content

Commit 8fb3281

Browse files
committed
[bugfix] fn:transform RAW destination for results
Code for xsl:result-document could not create a destination when the output format was RAW. This was an oversight. Allow this to happen.
1 parent 0702a6e commit 8fb3281

File tree

5 files changed

+155
-4
lines changed

5 files changed

+155
-4
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/Convert.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ static Item ofItem(final XdmItem xdmItem) throws XPathException {
7979
final BuiltInAtomicType atomicType = atomicValue.getPrimitiveType();
8080
if (atomicType == BuiltInAtomicType.INTEGER) {
8181
return new IntegerValue(atomicValue.getStringValue());
82+
} else if (atomicType == BuiltInAtomicType.DOUBLE) {
83+
return new DoubleValue(atomicValue.getStringValue());
8284
} else {
8385
// TODO (AP)
8486
throw new XPathException(ErrorCodes.XPTY0004,

exist-core/src/main/java/org/exist/xquery/functions/fn/FnTransform.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,16 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
335335
xslt30Transformer.setGlobalContextItem(null);
336336
}
337337

338-
return new TemplateInvocation(options, sourceNode, delivery, xslt30Transformer, resultDocuments).invoke();
338+
final TemplateInvocation invocation = new TemplateInvocation(
339+
options, sourceNode, delivery, xslt30Transformer, resultDocuments);
340+
return invocation.invoke();
339341

340342
} catch (final SaxonApiException e) {
341343
if (e.getErrorCode() != null) {
342344
final QName errorQn = QName.fromJavaQName(e.getErrorCode().getStructuredQName().toJaxpQName());
343345
throw new XPathException(this, new ErrorCodes.ErrorCode(errorQn, e.getMessage()), e.getMessage());
344346
} else {
347+
throwOriginalXPathException(e, ErrorCodes.FOXT0003);
345348
throw new XPathException(this, ErrorCodes.FOXT0003, e.getMessage());
346349
}
347350
} catch (final UncheckedXPathException e) {
@@ -432,7 +435,8 @@ private MapType invokeApplyTemplates() throws XPathException, SaxonApiException
432435
}
433436
} else if (sourceNode.isPresent()) {
434437
if (options.deliveryFormat == DeliveryFormat.RAW) {
435-
final Sequence existValue = Convert.ToExist.of(xslt30Transformer.applyTemplates(sourceNode.get()));
438+
xslt30Transformer.applyTemplates(sourceNode.get(), destination);
439+
final Sequence existValue = Convert.ToExist.of(delivery.getXdmValue());
436440
return makeResultMap(options, existValue, resultDocuments);
437441
}
438442
xslt30Transformer.applyTemplates(sourceNode.get(), destination);
@@ -465,6 +469,8 @@ private class Delivery {
465469
MemTreeBuilder builder;
466470
StringWriter stringWriter;
467471

472+
RawDestination rawDestination;
473+
468474
Delivery(final DeliveryFormat format, final SerializationProperties serializationProperties) {
469475
this.format = format;
470476
this.serializationProperties = serializationProperties;
@@ -493,18 +499,35 @@ final Destination createDestination(final Xslt30Transformer xslt30Transformer, f
493499
stringWriter = new StringWriter();
494500
serializer.setOutputWriter(stringWriter);
495501
return serializer;
502+
case RAW:
503+
this.rawDestination = new RawDestination();
504+
return rawDestination;
496505
default:
497506
return null;
498507
}
499508
}
500509

501510
final String getSerializedString() {
511+
512+
if (stringWriter == null) {
513+
return null;
514+
}
502515
return stringWriter.getBuffer().toString();
503516
}
504517

505518
final DocumentImpl getDocument() {
519+
if (builder == null) {
520+
return null;
521+
}
506522
return builder.getDocument();
507523
}
524+
525+
final XdmValue getXdmValue() {
526+
if (rawDestination == null) {
527+
return null;
528+
}
529+
return rawDestination.getXdmValue();
530+
}
508531
}
509532

510533
private MapType makeResultMap(final Options options, final Delivery delivery, final Map<URI, Delivery> resultDocuments) throws XPathException {
@@ -565,7 +588,13 @@ private Sequence convertToDeliveryFormat(final Options options, final Delivery d
565588
case SERIALIZED:
566589
return new StringValue(delivery.getSerializedString());
567590
case RAW:
568-
return rawOutput(delivery.getDocument());
591+
//TODO (AP) rawOutput and mediation via document when the Xdm value is complex, is a hack
592+
final XdmValue xdmValue = delivery.getXdmValue();
593+
if (xdmValue != null) {
594+
return Convert.ToExist.of(xdmValue);
595+
} else {
596+
return rawOutput(delivery.getDocument());
597+
}
569598
case DOCUMENT:
570599
default:
571600
return delivery.getDocument();

exist-core/src/test/java/xquery/xquery3/XQuery3Tests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@RunWith(XSuite.class)
2828
@XSuite.XSuiteFiles({
2929
//"src/test/xquery/xquery3",
30-
"src/test/xquery/xquery3/fnTransform79.xqm"
30+
"src/test/xquery/xquery3/fnTransform87.xqm"
3131
})
3232
public class XQuery3Tests {
3333
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
(:
2+
: eXist-db Open Source Native XML Database
3+
: Copyright (C) 2001 The eXist-db Authors
4+
:
5+
6+
: http://www.exist-db.org
7+
:
8+
: This library is free software; you can redistribute it and/or
9+
: modify it under the terms of the GNU Lesser General Public
10+
: License as published by the Free Software Foundation; either
11+
: version 2.1 of the License, or (at your option) any later version.
12+
:
13+
: This library is distributed in the hope that it will be useful,
14+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
: Lesser General Public License for more details.
17+
:
18+
: You should have received a copy of the GNU Lesser General Public
19+
: License along with this library; if not, write to the Free Software
20+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
:)
22+
xquery version "3.1";
23+
24+
module namespace testTransform="http://exist-db.org/xquery/test/function_transform";
25+
26+
declare namespace test="http://exist-db.org/xquery/xqsuite";
27+
28+
declare variable $testTransform:transform-13a-xsl :=
29+
"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='3.0'>
30+
<xsl:template match='/'>
31+
<xsl:for-each select='//section'>
32+
<xsl:result-document href='section{position()}.html'> <!-- instructions content here -->
33+
<html>
34+
<head>
35+
<title>Section <xsl:value-of select='position()'/></title>
36+
</head>
37+
<body>
38+
<h1>Header for section <xsl:value-of select='position()'/></h1>
39+
<p>The content of <xsl:value-of select='.'/>.</p>
40+
<xsl:if test='position() ne last()'>
41+
<p><a href='section{position()+1}.html'>Next section</a></p>
42+
</xsl:if>
43+
<xsl:if test='position() eq last()'>
44+
<p><a href='section1.html'>First section</a></p>
45+
</xsl:if>
46+
</body>
47+
</html>
48+
</xsl:result-document>
49+
</xsl:for-each>
50+
</xsl:template>
51+
</xsl:stylesheet>";
52+
53+
declare variable $testTransform:transform-13a-xml := "<doc>
54+
<section>sect1</section>
55+
<section>sect2</section>
56+
<section>sect3</section>
57+
</doc>";
58+
59+
declare
60+
%test:assertEquals("96")
61+
function testTransform:transform-87() {
62+
let $xsl := $testTransform:transform-13a-xsl
63+
let $xml := $testTransform:transform-13a-xml
64+
let $result := fn:transform(map {"stylesheet-text": $xsl,
65+
"source-node": parse-xml($xml),
66+
"base-output-uri":"http://www.w3.org/fots/fn/transform/output-doc.xml"})
67+
return $result
68+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(:
2+
: eXist-db Open Source Native XML Database
3+
: Copyright (C) 2001 The eXist-db Authors
4+
:
5+
6+
: http://www.exist-db.org
7+
:
8+
: This library is free software; you can redistribute it and/or
9+
: modify it under the terms of the GNU Lesser General Public
10+
: License as published by the Free Software Foundation; either
11+
: version 2.1 of the License, or (at your option) any later version.
12+
:
13+
: This library is distributed in the hope that it will be useful,
14+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
: Lesser General Public License for more details.
17+
:
18+
: You should have received a copy of the GNU Lesser General Public
19+
: License along with this library; if not, write to the Free Software
20+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
:)
22+
xquery version "3.1";
23+
24+
module namespace testTransform="http://exist-db.org/xquery/test/function_transform";
25+
26+
declare namespace test="http://exist-db.org/xquery/xqsuite";
27+
28+
declare variable $testTransform:transform-87-xsl :=
29+
"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
30+
version='1.0'>
31+
<xsl:template match='/'>
32+
<xsl:variable name='in' select='.'/>
33+
<xsl:for-each select='1 to 3'>
34+
<xsl:result-document href='output{.}' omit-xml-declaration='yes'>
35+
<xsl:sequence select='number($in/a/b) + .'/>
36+
</xsl:result-document>
37+
</xsl:for-each>
38+
</xsl:template>
39+
</xsl:stylesheet>";
40+
41+
declare
42+
%test:assertEquals("96")
43+
function testTransform:transform-87() {
44+
let $xsl := $testTransform:transform-87-xsl
45+
let $trans-result := fn:transform(map{"stylesheet-text" : $xsl,
46+
"delivery-format" : "raw",
47+
"base-output-uri" : "http://example.com/",
48+
"source-node" : parse-xml('<a><b>89</b></a>'),
49+
"post-process" : function($uri, $val) { $val + 5 }
50+
})
51+
return $trans-result("http://example.com/output2")
52+
};

0 commit comments

Comments
 (0)