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 */
31883189Datum
31893190xpath (PG_FUNCTION_ARGS )
@@ -3258,41 +3259,13 @@ xpath(PG_FUNCTION_ARGS)
32583259
32593260xml_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
32973270xmlInitParser ();
32983271
@@ -3307,7 +3280,7 @@ xpath(PG_FUNCTION_ARGS)
33073280doc = xmlCtxtReadMemory (ctxt , (char * )string ,len ,NULL ,NULL ,0 );
33083281if (doc == NULL )
33093282xml_ereport (ERROR ,ERRCODE_INVALID_XML_DOCUMENT ,
3310- "could not parse XMLdata " );
3283+ "could not parse XMLdocument " );
33113284xpathctx = xmlXPathNewContext (doc );
33123285if (xpathctx == NULL )
33133286xml_ereport (ERROR ,ERRCODE_OUT_OF_MEMORY ,