]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix handling of namespace nodes in xpath() (xml)
authorMichael Paquier <michael@paquier.xyz>
Fri, 12 Jun 2026 01:25:51 +0000 (10:25 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 12 Jun 2026 01:25:51 +0000 (10:25 +0900)
xpath() attempted to call xmlCopyNode() and xmlNodeDump() on a
XML_NAMESPACE_DECL, finishing with a confusing error:
=# SELECT xpath('//namespace::foo', '<root xmlns:foo="http://127.0.0.1"/>');
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 <michael@paquier.xyz>
Discussion: https://postgr.es/m/aioT7ui_ZJ9RMlfM@paquier.xyz
Backpatch-through: 14

src/backend/utils/adt/xml.c
src/test/regress/expected/xml.out
src/test/regress/expected/xml_1.out
src/test/regress/sql/xml.sql

index 8e97bb3b3dec13ba7d5d3110567029fa1327a9d1..e4a3e9ea1ef09888bb36c50ec874298553d8837c 100644 (file)
@@ -4156,7 +4156,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;
index 21677b609a6ca242282c4d91d73829e54d5f520d..8de8dd409c062a56624e25a0ed6058895f8a2cb1 100644 (file)
@@ -944,6 +944,12 @@ SELECT xpath('root', '<root/>');
  {<root/>}
 (1 row)
 
+SELECT xpath('//namespace::foo', '<root xmlns:foo="http://127.0.0.1"/>');
+       xpath        
+--------------------
+ {http://127.0.0.1}
+(1 row)
+
 -- Round-trip non-ASCII data through xpath().
 DO $$
 DECLARE
index 852444cb0527b8c609694c29c472ffced724b71e..31603a98db7edb193d5cd9d0af469871725daef8 100644 (file)
@@ -687,6 +687,11 @@ ERROR:  unsupported XML feature
 LINE 1: SELECT xpath('root', '<root/>');
                              ^
 DETAIL:  This functionality requires the server to be built with libxml support.
+SELECT xpath('//namespace::foo', '<root xmlns:foo="http://127.0.0.1"/>');
+ERROR:  unsupported XML feature
+LINE 1: SELECT xpath('//namespace::foo', '<root xmlns:foo="http://12...
+                                         ^
+DETAIL:  This functionality requires the server to be built with libxml support.
 -- Round-trip non-ASCII data through xpath().
 DO $$
 DECLARE
index 0ea4f508837cf660972e7351fa95371b47088e6f..8eda57f4beacc907d90c3e97591511c04ddd45cb 100644 (file)
@@ -244,6 +244,7 @@ SELECT xpath('count(//*)=3', '<root><sub/><sub/></root>');
 SELECT xpath('name(/*)', '<root><sub/><sub/></root>');
 SELECT xpath('/nosuchtag', '<root/>');
 SELECT xpath('root', '<root/>');
+SELECT xpath('//namespace::foo', '<root xmlns:foo="http://127.0.0.1"/>');
 
 -- Round-trip non-ASCII data through xpath().
 DO $$