Re: [PATCH] Bug in XPATH() if expression returns a scalar value

From: Florian Pflug <fgp(at)phlo(dot)org>
To: Ross J(dot) Reedstrom <reedstrm(at)rice(dot)edu>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] Bug in XPATH() if expression returns a scalar value
Date: 2011-05-31 19:28:50
Message-ID: 2CB0EF81-619A-464C-AA93-3A603598704D@phlo.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On May31, 2011, at 19:15 , Ross J. Reedstrom wrote:
> What you describe, making XPATH return something for the scalar
> functions, is sorely needed. Constraining the return values to be valid
> XML fragments is the sort of wart that makes XML processing in
> postgresql seem odd to those familiar with other tools, though.

I've now changes things so that the results of scalar-value
returning XPath expressions are correctly entity-encoded (i.e.,
a literal "<" gets translated to "&lt;").

After realizing that this is necessary even for node-set
returning XPath expressions (see my other mail from today),
because they may very well select text nodes, I came to the
conclusion that doing this unconditionally (well, except for
element nodes obviously) is the least surprising behaviour.

The following subsumes the behavior with this and the patch
from my other e-mail applied.

SELECT
(XPATH('namespace-uri(/*)', x))[1] AS namespace,
(XPATH('/*/@value', x))[1] AS value,
(XPATH('/*/text()', x))[1] AS text
FROM (VALUES (XMLELEMENT(name "root",
XMLATTRIBUTES('<n' AS xmlns, '<v' AS value),
'<t'
))) v(x);

namespace | value | text
-----------+-------+-------
&lt;n | &lt;v | &lt;t

Without the patch from the other mail, the "namespace" result
stays the same, but "value" and "text" are "<v" and "<t"
respectively.

Updated patch is attached

best regards,
Florian Pflug

PS: Btw, while trying this I think I found another problem. If you
do

SELECT (XPATH(
'/*',
XMLELEMENT(NAME "root",
XMLATTRIBUTES('<n' AS xmlns,
'<v' AS value))
))[1];

you get

xpath
----------------------------------
<root xmlns="<n" value="&lt;v"/>

i.e. the "<" in the namespace URI isn't quoted properly.
Trying to cast that value to text and back to xml fails.
Funnily enough, if you skip the XPATH() call, things work properly

SELECT XMLELEMENT(NAME "root",
XMLATTRIBUTES('<n' AS xmlns,
'<v' AS value))

gives

xmlelement
-------------------------------------
<root xmlns="&lt;n" value="&lt;v"/>

I'll start a new thread for this issue...

Attachment Content-Type Size
pg_xpath_returnvalue.v2.patch application/octet-stream 7.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2011-05-31 19:38:08 Re: Please test peer (socket ident) auth on *BSD
Previous Message Cédric Villemain 2011-05-31 18:52:54 Re: Getting a bug tracker for the Postgres project