Question: I can create a XML-encoded string in Postgres like this:

SELECT xmlelement(name name, 'AT&T', null )

now I want to get the xml encoded value, that is to say AT&T.

But if I do:

SELECT unnest(xpath('/name/text()', xmlelement(name name, 'AT&T', null )))

then I get AT&T, not AT&T.

How can I get the XML-encoded value?

Furthermore, isn't it possible to supply an empty name to xmlelement, and just cast to varchar?

share|improve this question

70% accept rate
I get AT&T in 9.2. Where are you outputting that value? – Clodoaldo Jan 9 at 13:08
SELECT version(); --> "PostgreSQL 9.0.4, compiled by Visual C++ build 1500, 32-bit" in pgAdmin3 Version 1.12.3 (Apr. 15 2011 REL-1_12_3) – Quandary Jan 9 at 16:00
feedback

1 Answer

If you are writing to an HTML client then you will have to HTML escape that for it to show the raw HTML.

As I see you are mainly a C# developer then the static method HttpUtility.HtmlEncode() will do it.

share|improve this answer
Yes, but I'd like to test something, so I want to encode all entries. Decoding I can ;) – Quandary Jan 9 at 15:54
@Quandary I don't understand your comment. – Clodoaldo Jan 9 at 16:00
I'm working on the database. I want to test something. I need to HTML-encode the entries in one column of one table in the database, not the output on the html page. – Quandary Jan 9 at 16:03
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.