diff --git a/contrib/xml2/expected/xml2.out b/contrib/xml2/expected/xml2.out
index 1906fcf33e2..9078f15f6b3 100644
--- a/contrib/xml2/expected/xml2.out
+++ b/contrib/xml2/expected/xml2.out
@@ -231,6 +231,14 @@ SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
- test
- 37
(1 row)
+-- namespace node
+SELECT xpath_nodeset('',
+ '//namespace::foo');
+ xpath_nodeset
+----------------------
+ http://icl.com/saxon
+(1 row)
+
-- xpath_list()
SELECT xpath_list(article_xml::text, '/article/author|/article/pages')
FROM articles;
diff --git a/contrib/xml2/expected/xml2_1.out b/contrib/xml2/expected/xml2_1.out
index 9a2144d58f5..62e8bd6802a 100644
--- a/contrib/xml2/expected/xml2_1.out
+++ b/contrib/xml2/expected/xml2_1.out
@@ -175,6 +175,14 @@ SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
- test
- 37
(1 row)
+-- namespace node
+SELECT xpath_nodeset('',
+ '//namespace::foo');
+ xpath_nodeset
+----------------------
+ http://icl.com/saxon
+(1 row)
+
-- xpath_list()
SELECT xpath_list(article_xml::text, '/article/author|/article/pages')
FROM articles;
diff --git a/contrib/xml2/sql/xml2.sql b/contrib/xml2/sql/xml2.sql
index 510d18a3679..145c487cbde 100644
--- a/contrib/xml2/sql/xml2.sql
+++ b/contrib/xml2/sql/xml2.sql
@@ -132,6 +132,9 @@ SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
'result', 'item')
FROM articles;
+-- namespace node
+SELECT xpath_nodeset('',
+ '//namespace::foo');
-- xpath_list()
SELECT xpath_list(article_xml::text, '/article/author|/article/pages')
diff --git a/contrib/xml2/xpath.c b/contrib/xml2/xpath.c
index 2820874cb5e..9227db36c41 100644
--- a/contrib/xml2/xpath.c
+++ b/contrib/xml2/xpath.c
@@ -149,16 +149,23 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
}
else
{
+ xmlNodePtr node = nodeset->nodeTab[i];
+
if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
{
xmlBufferWriteChar(buf, "<");
xmlBufferWriteCHAR(buf, septagname);
xmlBufferWriteChar(buf, ">");
}
- xmlNodeDump(buf,
- nodeset->nodeTab[i]->doc,
- nodeset->nodeTab[i],
- 1, 0);
+
+ /*
+ * XML_NAMESPACE_DECL nodes are xmlNs structs, that cannot
+ * be processed by xmlNodeDump().
+ */
+ if (node->type == XML_NAMESPACE_DECL)
+ xmlBufferWriteCHAR(buf, xmlXPathCastNodeToString(node));
+ else
+ xmlNodeDump(buf, node->doc, node, 1, 0);
if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
{