1

I'm having trouble with xpath() in PostgreSQL. For example...

WITH x(col) AS (
    SELECT '<?xml version="1.0" ?><response><status>ERROR &amp; DATA</status></response>'::xml)
SELECT xpath('//status/text()', col) AS status
FROM   x

I would like this to return ERROR & DATA but instead it returns ERROR &amp; DATA, leaving the XML entity encoded. How can I decode this in Postgres?

1

1 Answer 1

2
+300

Using XMLTABLE:

WITH x(col) AS (
SELECT '<?xml version="1.0" ?><response><status>ERROR &amp; DATA</status></response>'::xml
)
SELECT s.status
FROM x, XMLTABLE('//status' PASSING x.col COLUMNS status TEXT PATH '.') s;

--ERROR & DATA

db<>fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Very nice, I'm glad I asked! This particular syntax only works on version 10 and above. Anything for 9.6 or is that getting too old?
@MartinBurch Yes, it was introduced in PostgreSQL 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.