Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c9cab10

Browse files
committed
Remove munging of xml and xpath params to xpath(). The XML must now be a well formed XML document.
1 parent 1079564 commit c9cab10

File tree

2 files changed

+17
-39
lines changed

2 files changed

+17
-39
lines changed

doc/src/sgml/func.sgml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.472 2009/02/07 14:16:45 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.473 2009/03/23 21:00:38 adunstan Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -8447,6 +8447,11 @@ SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
84478447
corresponding to the node set produced by the XPath expression.
84488448
</para>
84498449

8450+
<para>
8451+
The second argument must be a well formed XML document. In particular,
8452+
it must have a single root node element.
8453+
</para>
8454+
84508455
<para>
84518456
The third argument of the function is an array of namespace
84528457
mappings. This array should be a two-dimensional array with the

src/backend/utils/adt/xml.c

+11-38
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.83 2009/01/07 13:44:37 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.84 2009/03/23 21:00:39 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -3182,8 +3182,9 @@ xml_xmlnodetoxmltype(xmlNodePtr cur)
31823182
* to be the most useful one (array of XML functions plays a role of
31833183
* some kind of substitution for XQuery sequences).
31843184
*
3185-
* Workaround here: we parse XML data in different way to allow XPath for
3186-
* fragments (see "XPath for fragment" TODO comment inside).
3185+
* It is up to the user to ensure that the XML passed is in fact
3186+
* an XML document - XPath doesn't work easily on fragments without
3187+
* a context node being known.
31873188
*/
31883189
Datum
31893190
xpath(PG_FUNCTION_ARGS)
@@ -3258,41 +3259,13 @@ xpath(PG_FUNCTION_ARGS)
32583259

32593260
xml_init();
32603261

3261-
/*
3262-
* To handle both documents and fragments, regardless of the fact whether
3263-
* the XML datum has a single root (XML well-formedness), we wrap the XML
3264-
* datum in a dummy element (<x>...</x>) and extend the XPath expression
3265-
* accordingly. To do it, throw away the XML prolog, if any.
3266-
*/
3267-
if (len >= 5 &&
3268-
xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3269-
{
3270-
i = 5;
3271-
while (i < len &&
3272-
!(datastr[i - 1] == '?' && datastr[i] == '>'))
3273-
i++;
3274-
3275-
if (i == len)
3276-
xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3277-
"could not parse XML data");
3278-
3279-
++i;
3280-
3281-
datastr += i;
3282-
len -= i;
3283-
}
3284-
3285-
string = (xmlChar *) palloc((len + 8) * sizeof(xmlChar));
3286-
memcpy(string, "<x>", 3);
3287-
memcpy(string + 3, datastr, len);
3288-
memcpy(string + 3 + len, "</x>", 5);
3289-
len += 7;
3262+
string = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
3263+
memcpy(string, datastr, len);
3264+
string[len] = '\0';
32903265

3291-
xpath_expr = (xmlChar *) palloc((xpath_len + 3) * sizeof(xmlChar));
3292-
memcpy(xpath_expr, "/x", 2);
3293-
memcpy(xpath_expr + 2, VARDATA(xpath_expr_text), xpath_len);
3294-
xpath_expr[xpath_len + 2] = '\0';
3295-
xpath_len += 2;
3266+
xpath_expr = (xmlChar *) palloc((xpath_len +1) * sizeof(xmlChar));
3267+
memcpy(xpath_expr, VARDATA(xpath_expr_text), xpath_len);
3268+
xpath_expr[xpath_len] = '\0';
32963269

32973270
xmlInitParser();
32983271

@@ -3307,7 +3280,7 @@ xpath(PG_FUNCTION_ARGS)
33073280
doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
33083281
if (doc == NULL)
33093282
xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3310-
"could not parse XML data");
3283+
"could not parse XML document");
33113284
xpathctx = xmlXPathNewContext(doc);
33123285
if (xpathctx == NULL)
33133286
xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,

0 commit comments

Comments
 (0)