* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.53 2007/11/08 15:16:45 petere Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.54 2007/11/09 15:52:51 petere Exp $
  *
  *-------------------------------------------------------------------------
  */
    xmltype *result;
    StringInfoData buf;
 
-   if (pg_strncasecmp(target, "xml", 3) == 0)
+   if (pg_strcasecmp(target, "xml") == 0)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR), /* really */
                 errmsg("invalid XML processing instruction"),
-                errdetail("XML processing instruction target name cannot start with \"xml\".")));
+                errdetail("XML processing instruction target name cannot be \"%s\".", target)));
 
    /*
     * Following the SQL standard, the null check comes after the
 #define SKIP_XML_SPACE(p) \
    while (xmlIsBlank_ch(*(p))) (p)++
 
+/* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
+#define pg_xmlIsNameChar(c) \
+   (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
+           || xmlIsDigit_ch(c) \
+           || c == '.' || c == '-' || c == '_' || c == ':' \
+           || xmlIsCombiningQ(c) \
+           || xmlIsExtender_ch(c))
+
 static int
 parse_xml_decl(const xmlChar *str,size_t *lenp,
               xmlChar **version, xmlChar **encoding, int *standalone)
    const xmlChar *p;
    const xmlChar *save_p;
    size_t      len;
+   int         utf8len;
 
    xml_init();
 
    if (xmlStrncmp(p, (xmlChar *)"<?xml", 5) != 0)
        goto finished;
 
+   /* This means it's a PI like <?xml-stylesheet ...?>. */
+   if (pg_xmlIsNameChar(xmlGetUTF8Char(&p[5], &utf8len)))
+       goto finished;
+
    p += 5;
 
    /* version */
 
  <?foo?>
 (1 row)
 
-SELECT xmlpi(name xmlstuff);
+SELECT xmlpi(name xml);
 ERROR:  invalid XML processing instruction
-DETAIL:  XML processing instruction target name cannot start with "xml".
+DETAIL:  XML processing instruction target name cannot be "xml".
+SELECT xmlpi(name xmlstuff);
+    xmlpi     
+--------------
+ <?xmlstuff?>
+(1 row)
+
 SELECT xmlpi(name foo, 'bar');
     xmlpi    
 -------------
  
 (1 row)
 
-SELECT xmlpi(name xmlstuff, null);
+SELECT xmlpi(name xml, null);
 ERROR:  invalid XML processing instruction
-DETAIL:  XML processing instruction target name cannot start with "xml".
+DETAIL:  XML processing instruction target name cannot be "xml".
+SELECT xmlpi(name xmlstuff, null);
+ xmlpi 
+-------
+ 
+(1 row)
+
+SELECT xmlpi(name "xml-stylesheet", 'href="mystyle.css" type="text/css"');
+                         xmlpi                         
+-------------------------------------------------------
+ <?xml-stylesheet href="mystyle.css" type="text/css"?>
+(1 row)
+
 SELECT xmlpi(name foo, '   bar');
     xmlpi    
 -------------
 
 ERROR:  unsupported XML feature
 DETAIL:  This functionality requires the server to be built with libxml support.
 HINT:  You need to rebuild PostgreSQL using --with-libxml.
+SELECT xmlpi(name xml);
+ERROR:  unsupported XML feature
+DETAIL:  This functionality requires the server to be built with libxml support.
+HINT:  You need to rebuild PostgreSQL using --with-libxml.
 SELECT xmlpi(name xmlstuff);
 ERROR:  unsupported XML feature
 DETAIL:  This functionality requires the server to be built with libxml support.
 ERROR:  unsupported XML feature
 DETAIL:  This functionality requires the server to be built with libxml support.
 HINT:  You need to rebuild PostgreSQL using --with-libxml.
+SELECT xmlpi(name xml, null);
+ERROR:  unsupported XML feature
+DETAIL:  This functionality requires the server to be built with libxml support.
+HINT:  You need to rebuild PostgreSQL using --with-libxml.
 SELECT xmlpi(name xmlstuff, null);
 ERROR:  unsupported XML feature
 DETAIL:  This functionality requires the server to be built with libxml support.
 HINT:  You need to rebuild PostgreSQL using --with-libxml.
+SELECT xmlpi(name "xml-stylesheet", 'href="mystyle.css" type="text/css"');
+ERROR:  unsupported XML feature
+DETAIL:  This functionality requires the server to be built with libxml support.
+HINT:  You need to rebuild PostgreSQL using --with-libxml.
 SELECT xmlpi(name foo, '   bar');
 ERROR:  unsupported XML feature
 DETAIL:  This functionality requires the server to be built with libxml support.
 
 
 
 SELECT xmlpi(name foo);
+SELECT xmlpi(name xml);
 SELECT xmlpi(name xmlstuff);
 SELECT xmlpi(name foo, 'bar');
 SELECT xmlpi(name foo, 'in?>valid');
 SELECT xmlpi(name foo, null);
+SELECT xmlpi(name xml, null);
 SELECT xmlpi(name xmlstuff, null);
+SELECT xmlpi(name "xml-stylesheet", 'href="mystyle.css" type="text/css"');
 SELECT xmlpi(name foo, '   bar');