diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 8307f1cf47b..37d85f71f3b 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -3934,10 +3934,7 @@ xpath_internal(text *xpath_expr_text, xmltype *data, ArrayType *namespaces,
if (xpathctx == NULL || xmlerrcxt->err_occurred)
xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
"could not allocate XPath context");
- xpathctx->node = xmlDocGetRootElement(doc);
- if (xpathctx->node == NULL || xmlerrcxt->err_occurred)
- xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
- "could not find root XML element");
+ xpathctx->node = (xmlNodePtr) doc;
/* register namespaces, if any */
if (ns_count > 0)
@@ -4276,10 +4273,7 @@ XmlTableSetDocument(TableFuncScanState *state, Datum value)
if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
"could not allocate XPath context");
- xpathcxt->node = xmlDocGetRootElement(doc);
- if (xpathcxt->node == NULL || xtCxt->xmlerrcxt->err_occurred)
- xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
- "could not find root XML element");
+ xpathcxt->node = (xmlNodePtr) doc;
}
PG_CATCH();
{
diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out
index 3eb638ca258..6e1f885112c 100644
--- a/src/test/regress/expected/xml.out
+++ b/src/test/regress/expected/xml.out
@@ -670,6 +670,12 @@ SELECT xpath('/nosuchtag', '');
{}
(1 row)
+SELECT xpath('root', '');
+ xpath
+-----------
+ {}
+(1 row)
+
-- Round-trip non-ASCII data through xpath().
DO $$
DECLARE
@@ -1212,7 +1218,7 @@ SELECT * FROM xmltable('/root' passing 'a1aa2aa1aa2a bbbbxxxcccc' COLUMNS element text PATH 'element/text()'); -- should fail
ERROR: more than one value returned by column XPath expression
-- CDATA test
-select * from xmltable('r' passing ' &"<>!foo]]>2' columns c text);
+select * from xmltable('d/r' passing ' &"<>!foo]]>2' columns c text);
c
-------------------------
&"<>!foo
diff --git a/src/test/regress/expected/xml_1.out b/src/test/regress/expected/xml_1.out
index 2053734f65f..0eba4243462 100644
--- a/src/test/regress/expected/xml_1.out
+++ b/src/test/regress/expected/xml_1.out
@@ -576,6 +576,12 @@ LINE 1: SELECT xpath('/nosuchtag', '');
^
DETAIL: This functionality requires the server to be built with libxml support.
HINT: You need to rebuild PostgreSQL using --with-libxml.
+SELECT xpath('root', '');
+ERROR: unsupported XML feature
+LINE 1: SELECT xpath('root', '');
+ ^
+DETAIL: This functionality requires the server to be built with libxml support.
+HINT: You need to rebuild PostgreSQL using --with-libxml.
-- Round-trip non-ASCII data through xpath().
DO $$
DECLARE
@@ -1067,10 +1073,10 @@ LINE 1: SELECT * FROM xmltable('/root' passing 'a1a &"<>!foo]]>2' columns c text);
+select * from xmltable('d/r' passing ' &"<>!foo]]>2' columns c text);
ERROR: unsupported XML feature
-LINE 1: select * from xmltable('r' passing '');
{}
(1 row)
+SELECT xpath('root', '');
+ xpath
+-----------
+ {}
+(1 row)
+
-- Round-trip non-ASCII data through xpath().
DO $$
DECLARE
@@ -1192,7 +1198,7 @@ SELECT * FROM xmltable('/root' passing 'a1aa2aa1aa2a bbbbxxxcccc' COLUMNS element text PATH 'element/text()'); -- should fail
ERROR: more than one value returned by column XPath expression
-- CDATA test
-select * from xmltable('r' passing ' &"<>!foo]]>2' columns c text);
+select * from xmltable('d/r' passing ' &"<>!foo]]>2' columns c text);
c
-------------------------
&"<>!foo
diff --git a/src/test/regress/sql/xml.sql b/src/test/regress/sql/xml.sql
index c223603a1f2..3b91b56d5a9 100644
--- a/src/test/regress/sql/xml.sql
+++ b/src/test/regress/sql/xml.sql
@@ -188,6 +188,7 @@ SELECT xpath('count(//*)=0', '');
SELECT xpath('count(//*)=3', '');
SELECT xpath('name(/*)', '');
SELECT xpath('/nosuchtag', '');
+SELECT xpath('root', '');
-- Round-trip non-ASCII data through xpath().
DO $$
@@ -423,7 +424,7 @@ SELECT * FROM xmltable('/root' passing 'a1aa2aa1aa2a bbbbxxxcccc' COLUMNS element text PATH 'element/text()'); -- should fail
-- CDATA test
-select * from xmltable('r' passing ' &"<>!foo]]>2' columns c text);
+select * from xmltable('d/r' passing ' &"<>!foo]]>2' columns c text);
-- XML builtin entities
SELECT * FROM xmltable('/x/a' PASSING ''"&<>' COLUMNS ent text);