7
7
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
8
8
* Portions Copyright (c) 1994, Regents of the University of California
9
9
*
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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -3182,8 +3182,9 @@ xml_xmlnodetoxmltype(xmlNodePtr cur)
3182
3182
* to be the most useful one (array of XML functions plays a role of
3183
3183
* some kind of substitution for XQuery sequences).
3184
3184
*
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.
3187
3188
*/
3188
3189
Datum
3189
3190
xpath (PG_FUNCTION_ARGS )
@@ -3258,41 +3259,13 @@ xpath(PG_FUNCTION_ARGS)
3258
3259
3259
3260
xml_init ();
3260
3261
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' ;
3290
3265
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' ;
3296
3269
3297
3270
xmlInitParser ();
3298
3271
@@ -3307,7 +3280,7 @@ xpath(PG_FUNCTION_ARGS)
3307
3280
doc = xmlCtxtReadMemory (ctxt , (char * ) string , len , NULL , NULL , 0 );
3308
3281
if (doc == NULL )
3309
3282
xml_ereport (ERROR , ERRCODE_INVALID_XML_DOCUMENT ,
3310
- "could not parse XML data " );
3283
+ "could not parse XML document " );
3311
3284
xpathctx = xmlXPathNewContext (doc );
3312
3285
if (xpathctx == NULL )
3313
3286
xml_ereport (ERROR , ERRCODE_OUT_OF_MEMORY ,
0 commit comments