From: Michael Paquier Date: Fri, 12 Jun 2026 01:25:53 +0000 (+0900) Subject: Fix handling of namespace nodes in xpath() (xml) X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1740766012e6432be639e0da98c5f4a917bfeafd;p=thirdparty%2Fpostgresql.git Fix handling of namespace nodes in xpath() (xml) xpath() attempted to call xmlCopyNode() and xmlNodeDump() on a XML_NAMESPACE_DECL, finishing with a confusing error: =# SELECT xpath('//namespace::foo', ''); ERROR: 53200: could not copy node CONTEXT: SQL function "xpath" statement 1 xpath() is changed so as it goes through xmlXPathCastNodeToString() instead, that is able to handle namespace nodes. xml2 uses the same solution. This issue has been discovered while digging into 9d33a5a804db. Author: Michael Paquier Discussion: https://postgr.es/m/aioT7ui_ZJ9RMlfM@paquier.xyz Backpatch-through: 14 --- diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index aad320baaf9..7c5abceffdd 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -4137,7 +4137,9 @@ xml_xmlnodetoxmltype(xmlNodePtr cur, PgXmlErrorContext *xmlerrcxt) { xmltype *result = NULL; - if (cur->type != XML_ATTRIBUTE_NODE && cur->type != XML_TEXT_NODE) + if (cur->type != XML_ATTRIBUTE_NODE && + cur->type != XML_TEXT_NODE && + cur->type != XML_NAMESPACE_DECL) { void (*volatile nodefree) (xmlNodePtr) = NULL; volatile xmlBufferPtr buf = NULL; diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out index caec8f830ed..66b798a5a59 100644 --- a/src/test/regress/expected/xml.out +++ b/src/test/regress/expected/xml.out @@ -944,6 +944,12 @@ SELECT xpath('root', ''); {} (1 row) +SELECT xpath('//namespace::foo', ''); + xpath +-------------------- + {http://127.0.0.1} +(1 row) + -- Round-trip non-ASCII data through xpath(). DO $$ DECLARE diff --git a/src/test/regress/expected/xml_1.out b/src/test/regress/expected/xml_1.out index e89828cb489..e76edc9a7a2 100644 --- a/src/test/regress/expected/xml_1.out +++ b/src/test/regress/expected/xml_1.out @@ -687,6 +687,11 @@ ERROR: unsupported XML feature LINE 1: SELECT xpath('root', ''); ^ DETAIL: This functionality requires the server to be built with libxml support. +SELECT xpath('//namespace::foo', ''); +ERROR: unsupported XML feature +LINE 1: SELECT xpath('//namespace::foo', ''); -- Round-trip non-ASCII data through xpath(). DO $$