Extract text from XML, pay attention to XML Entities

Поиск
Список
Период
Сортировка
Искать

Extract text from XML, pay attention to XML Entities

От:
Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
Дата:

Dear Mailing List,

 

Let’s say I want to extract from '<x>foo &lt; bar</x>' the inner text "foo < bar" with XML Entity escape sequences resolved.

 

First attempt:

================

SELECT (xpath('/x/text()', '<x>foo &lt; bar</x>'))[1]::text

 

… does not resolve the escape sequences: gives me "foo &lt; bar"

 

 

Next attempt:

================

SELECT xpath_string('<x>foo &lt; bar</x>', '/x/text()')

 

… works as intended, but

  • the xml2 extension is deprecated
  • won’t deal with namespaces as far as I understand

 

 

Third attempt:

================

SELECT txt

FROM XMLTABLE('/x' PASSING '<x>foo &lt; bar</x>' COLUMNS txt TEXT PATH '.')

 

works as intended, but if I want both namespaces and isolate this piece of code into a stored procedure, I have to deal with the XMLTABLE XMLNAMESPACE clause. And I suspect there is no freaking way to pass a parameter containing an array of namespace definitions into the XMLTABLE XMLNAMESPACE clause? (This I would consider a failure of SQL reusability.)

 

 

Any idea how to solve this fairly simple problem in a simple and general fashion? Thank You.

 

 

My last resorts would be

 

* using xpath(…)::text and replacing XML Entities *manually*

 

* or using XMLTABLE with dynamic SQL

 

* or: SELECT * FROM XMLTABLE('.' PASSING (xpath('/x', '<x>foo &lt; bar</x>'))[1] COLUMNS txt TEXT PATH '.')
(Which shall mean: I can use xpath potentially passing namespace definitions as an array, and then use XMLTABLE for the single purpose of un-escaping the text.

 

 

Regards,

Franz-Josef Färber

 

 

P.S.: I have read:

Re: xpath does not seem to escape HTML correctly

https://www.postgresql.org/message-id/1409795403248-5817667.post%40n5.nabble.com

 

Re: Extract text from XML, pay attention to XML Entities

От:
Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
Дата:

*ping* anybody interested to reply? Thank You.

 

Von: Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
Gesendet: Mittwoch, 16. September 2026 17:35
An: pgsql-general@lists.postgresql.org
Betreff: Extract text from XML, pay attention to XML Entities

 

Dear Mailing List,

 

Let’s say I want to extract from '<x>foo &lt; bar</x>' the inner text "foo < bar" with XML Entity escape sequences resolved.

 

First attempt:

================

SELECT (xpath('/x/text()', '<x>foo &lt; bar</x>'))[1]::text

 

… does not resolve the escape sequences: gives me "foo &lt; bar"

 

 

Next attempt:

================

SELECT xpath_string('<x>foo &lt; bar</x>', '/x/text()')

 

… works as intended, but

  • the xml2 extension is deprecated
  • won’t deal with namespaces as far as I understand

 

 

Third attempt:

================

SELECT txt

FROM XMLTABLE('/x' PASSING '<x>foo &lt; bar</x>' COLUMNS txt TEXT PATH '.')

 

works as intended, but if I want both namespaces and isolate this piece of code into a stored procedure, I have to deal with the XMLTABLE XMLNAMESPACE clause. And I suspect there is no freaking way to pass a parameter containing an array of namespace definitions into the XMLTABLE XMLNAMESPACE clause? (This I would consider a failure of SQL reusability.)

 

 

Any idea how to solve this fairly simple problem in a simple and general fashion? Thank You.

 

 

My last resorts would be

 

* using xpath(…)::text and replacing XML Entities *manually*

 

* or using XMLTABLE with dynamic SQL

 

* or: SELECT * FROM XMLTABLE('.' PASSING (xpath('/x', '<x>foo &lt; bar</x>'))[1] COLUMNS txt TEXT PATH '.')
(Which shall mean: I can use xpath potentially passing namespace definitions as an array, and then use XMLTABLE for the single purpose of un-escaping the text.

 

 

Regards,

Franz-Josef Färber

 

 

P.S.: I have read:

Re: xpath does not seem to escape HTML correctly

https://www.postgresql.org/message-id/1409795403248-5817667.post%40n5.nabble.com

 

Extract text from XML, pay attention to XML Entities

От:
"David G. Johnston" <david.g.johnston@gmail.com>
Дата:
On Monday, September 28, 2026, Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de> wrote:

*ping* anybody interested to reply? Thank You.


I suspect you’ve framed this up fully and correctly and are left with choosing whichever of your last resorts you deem least offensive.  I’d probably go with a custom text transformer personally; though why you need to do this, the risks involved, tend to matter for such decisions.

David J.

AW: Extract text from XML, pay attention to XML Entities

От:
Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
Дата:

Hello David,

 

Thanks for your reply.

 

It’s just strange and unfortunate that

 

a) Postgres does not offer a simple solution

b) Postgres had indeed offered a simple solution, xml_string, in the xml2 extension, which is deprecated (and won't deal with namespaces).

 

 

Regards,

Franz-Josef Färber

 

Von: David G. Johnston <david.g.johnston@gmail.com>
Gesendet: Montag, 28. September 2026 09:27
An: Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
Cc: pgsql-general@lists.postgresql.org
Betreff: Extract text from XML, pay attention to XML Entities

 

On Monday, September 28, 2026, Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de> wrote:

*ping* anybody interested to reply? Thank You.

 

I suspect you’ve framed this up fully and correctly and are left with choosing whichever of your last resorts you deem least offensive.  I’d probably go with a custom text transformer personally; though why you need to do this, the risks involved, tend to matter for such decisions.

 

David J.

 

Re: AW: Extract text from XML, pay attention to XML Entities

От:
Adrian Klaver <adrian.klaver@aklaver.com>
Дата:
On 9/29/26 6:12 AM, Färber, Franz-Josef (StMUK) wrote:
> Hello David,
> 
> Thanks for your reply.
> 
> It’s just strange and unfortunate that
> 
> a) Postgres does not offer a simple solution

I think you are left with from here:

https://www.postgresql.org/docs/current/xml2.html

"If you find that some of the functionality of this module is not 
available in an adequate form with the newer API, please explain your 
issue to  so that the deficiency can 
be addressed."


> 
> b) Postgres had indeed offered a simple solution, xml_string, in the 
> xml2 extension, which is deprecated (and won't deal with namespaces).
> 
> Regards,
> 
> Franz-Josef Färber
-- 
Adrian Klaver
adrian.klaver@aklaver.com


FAQ