Skip to content

Commit bbc03d4

Browse files
authored
Merge pull request #4930 from evolvedbinary/hotfix/eb-2061-nodeproxy-saxon
Fix passing database nodes from XQuery to fn:transform as parameters
2 parents a323d8a + 48261a8 commit bbc03d4

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import net.sf.saxon.type.BuiltInAtomicType;
2727
import org.exist.dom.QName;
2828
import org.exist.dom.memtree.DocumentImpl;
29+
import org.exist.dom.persistent.NodeProxy;
2930
import org.exist.xquery.ErrorCodes;
3031
import org.exist.xquery.XPathException;
3132
import org.exist.xquery.functions.array.ArrayType;
@@ -119,6 +120,9 @@ static net.sf.saxon.s9api.QName of(final QNameValue qName) {
119120
}
120121

121122
XdmValue of(final Item item) throws XPathException {
123+
if (item instanceof NodeProxy nodeProxy) {
124+
return ofNode(nodeProxy.getNode());
125+
}
122126
final int itemType = item.getType();
123127
if (Type.subTypeOf(itemType, Type.ATOMIC)) {
124128
return ofAtomic((AtomicValue) item);
@@ -150,7 +154,7 @@ private XdmValue ofNode(final Node node) throws XPathException {
150154

151155
final DocumentBuilder sourceBuilder = newDocumentBuilder();
152156
try {
153-
if (node instanceof DocumentImpl) {
157+
if (node instanceof Document) {
154158
return sourceBuilder.build(new DOMSource(node));
155159
} else {
156160
//The source must be part of a document
@@ -178,6 +182,9 @@ XdmValue[] of(final ArrayType values) throws XPathException {
178182
}
179183

180184
XdmValue of(final Sequence value) throws XPathException {
185+
if (value instanceof NodeProxy nodeProxy) {
186+
return ofNode(nodeProxy.getNode());
187+
}
181188
return XdmValue.makeSequence(listOf(value));
182189
}
183190

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"src/test/xquery/xquery3",
3030
"src/test/xquery/xquery3/transform",
3131
// To add an individual test or only run a specific set of tests -
32-
// "src/test/xquery/xquery3/serialize.xql",
32+
//"src/test/xquery/xquery3/serialize.xql",
3333
})
3434
public class XQuery3Tests {
3535
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
import module namespace xmldb="http://exist-db.org/xquery/xmldb";
26+
declare namespace test="http://exist-db.org/xquery/xqsuite";
27+
28+
declare variable $testTransform:stylesheet1 := <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
29+
<xsl:param name='v'/>
30+
<xsl:template match='/'>
31+
<v><xsl:value-of select='$v'/></v>
32+
</xsl:template>
33+
</xsl:stylesheet>;
34+
35+
declare variable $testTransform:stylesheet2 := <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
36+
<xsl:param name='v'/>
37+
<xsl:template name='named-template' match='/'>
38+
<v><xsl:value-of select='$v'/></v>
39+
</xsl:template>
40+
</xsl:stylesheet>;
41+
42+
declare variable $testTransform:document := <document>
43+
<catalog>
44+
<book id="bk101">
45+
<author>Gambardella, Matthew</author>
46+
<title>XML Developer's Guide</title>
47+
<genre>Computer</genre>
48+
<price>44.95</price>
49+
<publish_date>2000-10-01</publish_date>
50+
<description>An in-depth look at creating applications
51+
with XML.</description>
52+
</book>
53+
<book id="bk102">
54+
<author>Ralls, Kim</author>
55+
<title>Midnight Rain</title>
56+
<genre>Fantasy</genre>
57+
<price>5.95</price>
58+
<publish_date>2000-12-16</publish_date>
59+
<description>A former architect battles corporate zombies,
60+
an evil sorceress, and her own childhood to become queen
61+
of the world.</description>
62+
</book>
63+
</catalog>
64+
</document>;
65+
66+
declare
67+
%test:setUp
68+
function testTransform:setup() {
69+
let $coll := xmldb:create-collection("/db", "regression-test")
70+
let $storeStylesheet1 := xmldb:store($coll, "stylesheet1.xsl", $testTransform:stylesheet1, "application/xslt+xml")
71+
let $storeStylesheet2 := xmldb:store($coll, "stylesheet2.xsl", $testTransform:stylesheet2, "application/xslt+xml")
72+
let $storeDocument := xmldb:store($coll, "document.xml", $testTransform:document, "application/document")
73+
return ()
74+
};
75+
76+
declare
77+
%test:tearDown
78+
function testTransform:cleanup() {
79+
xmldb:remove("/db/regression-test")
80+
};
81+
82+
declare
83+
%test:assertEquals("<v>Gambardella, MatthewXML Developer's GuideComputer44.952000-10-01An in-depth look at creating applications
84+
with XML.Ralls, KimMidnight RainFantasy5.952000-12-16A former architect battles corporate zombies,
85+
an evil sorceress, and her own childhood to become queen
86+
of the world.</v>")
87+
function testTransform:regression-test-1() {
88+
let $in := parse-xml("<dummy/>")
89+
let $result := ( fn:transform(map{
90+
"source-node":$in,
91+
"stylesheet-node":doc("/db/regression-test/stylesheet1.xsl"),
92+
"stylesheet-params": map { QName("","v"): doc("/db/regression-test/document.xml") } } ) )?output
93+
return $result
94+
};
95+
96+
declare
97+
%test:assertEquals("<v>Gambardella, MatthewXML Developer's GuideComputer44.952000-10-01An in-depth look at creating applications
98+
with XML.Ralls, KimMidnight RainFantasy5.952000-12-16A former architect battles corporate zombies,
99+
an evil sorceress, and her own childhood to become queen
100+
of the world.</v>")
101+
function testTransform:regression-test-2() {
102+
let $in := parse-xml("<dummy/>")
103+
let $result := ( fn:transform(map{
104+
"source-node":$in,
105+
"stylesheet-node":doc("/db/regression-test/stylesheet2.xsl"),
106+
"initial-template": QName('', 'named-template'),
107+
"global-context-item" : fn:doc("/db/regression-test/document.xml"),
108+
"stylesheet-params": map {
109+
QName('', 'v'): fn:doc("/db/regression-test/document.xml")
110+
}}))?output
111+
return $result
112+
};
113+
114+

0 commit comments

Comments
 (0)