Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h5YnP-0000h2-86 for pgsql-general@arkaria.postgresql.org; Sun, 17 Mar 2019 16:37:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h5YnN-000114-OP for pgsql-general@arkaria.postgresql.org; Sun, 17 Mar 2019 16:37:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h5YnN-0000xN-EQ for pgsql-general@lists.postgresql.org; Sun, 17 Mar 2019 16:37:57 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h5YnK-0000Q4-F0 for pgsql-general@postgresql.org; Sun, 17 Mar 2019 16:37:57 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x2HGbh6E030124; Sun, 17 Mar 2019 12:37:43 -0400 From: Tom Lane To: "Andrus" cc: "Pavel Stehule" , "pgsql-general" Subject: Re: How to parse XML in Postgres newer versions also In-reply-to: References: Comments: In-reply-to "Andrus" message dated "Sun, 17 Mar 2019 16:11:44 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <30122.1552840663.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 17 Mar 2019 12:37:43 -0400 Message-ID: <30123.1552840663@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Andrus" writes: >> But I have not a idea, why old code doesn't work. It is little bit stra= nge so it worked without namespace before Amt tag. = > In 9.1.5 without namespaces > (xpath('Amt/text()', x,nsa))[1]::text::numeric AS tasusumma > works. > How to make it work in both versions? I'm hardly an XML expert, but I think the behavior change that's getting you came in with these commits: Author: Peter Eisentraut Branch: REL9_4_STABLE Release: REL9_4_1 [6bbf75192] 2015-01-17 22:11:2= 0 -0500 Branch: REL9_3_STABLE Release: REL9_3_6 [e32cb8d0e] 2015-01-17 22:13:2= 7 -0500 Branch: REL9_2_STABLE Release: REL9_2_10 [c8ef5b1ac] 2015-01-17 22:14:= 21 -0500 Branch: REL9_1_STABLE Release: REL9_1_15 [c975fa471] 2015-01-17 22:37:= 07 -0500 Branch: REL9_0_STABLE Release: REL9_0_19 [cebb3f032] 2015-01-17 22:37:= 32 -0500 = Fix namespace handling in xpath function = Previously, the xml value resulting from an xpath query would not = have namespace declarations if the namespace declarations were attached= to an ancestor element in the input xml value. That means the output= value was not correct XML. Fix that by running the result value through xmlCopyNode(), which produces the correct namespace declarations. = Author: Ali Akbar As things currently work, the lower xpath call is producing namespace-labeled XML: regression=3D# SELECT unnest(xpath('/ns:Document/ns:BkToCstmrStmt/ns:Stmt/= ns:Ntry', x,nsa)) as x = FROM t; x = --------------------------------------------------------------- + 1.34 + + 5.56 + (2 rows) so you have to attach namespaces to the elements of the upper xpath call if you want a match: regression=3D# SELECT = (xpath('ns:Ntry/ns:Amt/text()', x,nsa)) AS tasusumma FROM ( SELECT unnest(xpath('/ns:Document/ns:BkToCstmrStmt/ns:Stmt/ns:Ntry= ', x,nsa)) as x, nsa FROM t ) Ntry ; tasusumma = ----------- {1.34} {5.56} (2 rows) but of course that'd fail in 9.1.5 because the output of the lower xpath call bears no namespace labels :-(. (Or so I think anyway, I don't have any such installation handy to try.) The behavior you're seeing in 9.1.5 is just broken, so ideally what you'd do to resolve the cross-version discrepancy is update that installation to 9.1.15 or later. Or perhaps you could drop the usage of namespaces from this query? I think the old and new behaviors are the same if no namespaces are involved. regards, tom lane