- appendStringInfo(&buf2, "
-
- if (global_standalone == 1)
- appendStringInfoString(&buf2, " standalone=\"yes\"");
- else if (global_standalone == 0)
- appendStringInfoString(&buf2, " standalone=\"no\"");
-
- appendStringInfoString(&buf2, "?>");
+ print_xml_decl(&buf2,
+ (!global_version_no_value && global_version) ? global_version : NULL,
+ 0,
+ global_standalone);
appendStringInfoString(&buf2, buf.data);
buf = buf2;
{
text *data = PG_GETARG_TEXT_P(0);
- PG_RETURN_XML_P(xmlparse(data, false, true));
+ PG_RETURN_XML_P(xmlparse(data, (xmloption == XMLOPTION_DOCUMENT), true));
}
xmlroot(xmltype *data, text *version, int standalone)
{
#ifdef USE_LIBXML
- xmltype *result;
- xmlDocPtr doc;
- xmlBufferPtr buffer;
- xmlSaveCtxtPtr save;
+ char *str;
+ size_t len;
+ xmlChar *orig_version;
+ int orig_standalone;
+ StringInfoData buf;
- doc = xml_parse((text *) data, true, true, NULL);
+ len = VARSIZE(data) - VARHDRSZ;
+ str = palloc(len + 1);
+ memcpy(str, VARDATA(data), len);
+ str[len] = '\0';
+
+ parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
if (version)
- doc->version = xmlStrdup(xml_text2xmlChar(version));
+ orig_version = xml_text2xmlChar(version);
else
- doc->version = NULL;
+ orig_version = NULL;
switch (standalone)
{
- case 1:
- doc->standalone = 1;
+ case XML_STANDALONE_YES:
+ orig_standalone = 1;
+ break;
+ case XML_STANDALONE_NO:
+ orig_standalone = 0;
break;
- case -1:
- doc->standalone = 0;
+ case XML_STANDALONE_NO_VALUE:
+ orig_standalone = -1;
break;
- default:
- doc->standalone = -1;
+ case XML_STANDALONE_OMITTED:
+ /* leave original value */
break;
}
- buffer = xmlBufferCreate();
- save = xmlSaveToBuffer(buffer, "UTF-8", 0);
- xmlSaveDoc(save, doc);
- xmlSaveClose(save);
-
- xmlFreeDoc(doc);
+ initStringInfo(&buf);
+ print_xml_decl(&buf, orig_version, 0, orig_standalone);
+ appendStringInfoString(&buf, str + len);
- result = cstring_to_xmltype((char *) pg_do_encoding_conversion((unsigned char *) xmlBufferContent(buffer),
- xmlBufferLength(buffer),
- PG_UTF8,
- GetDatabaseEncoding()));
- xmlBufferFree(buffer);
- return result;
+ return stringinfo_to_xmltype(&buf);
#else
NO_XML_SUPPORT();
return NULL;
}
+/*
+ * Write an XML declaration. On output, we adjust the XML declaration
+ * as follows. (These rules are the moral equivalent of the clause
+ * "Serialization of an XML value" in the SQL standard.)
+ *
+ * We try to avoid generating an XML declaration if possible. This is
+ * so that you don't get trivial things like xml '' resulting in
+ * '', which would surely be annoying. We
+ * must provide a declaration if the standalone property is specified
+ * or if we include an encoding declaration. If we have a
+ * declaration, we must specify a version (XML requires this).
+ * Otherwise we only make a declaration if the version is not "1.0",
+ * which is the default version specified in SQL:2003.
+ */
+static bool
+print_xml_decl(StringInfo buf, const xmlChar *version, pg_enc encoding, int standalone)
+{
+ if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
+ || (encoding && encoding != PG_UTF8)
+ || standalone != -1)
+ {
+ appendStringInfoString(buf, "
+
+ if (version)
+ appendStringInfo(buf, " version=\"%s\"", version);
+ else
+ appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
+
+ if (encoding && encoding != PG_UTF8)
+ /* XXX might be useful to convert this to IANA names
+ * (ISO-8859-1 instead of LATIN1 etc.); needs field
+ * experience */
+ appendStringInfo(buf, " encoding=\"%s\"", pg_encoding_to_char(encoding));
+
+ if (standalone == 1)
+ appendStringInfoString(buf, " standalone=\"yes\"");
+ else if (standalone == 0)
+ appendStringInfoString(buf, " standalone=\"no\"");
+ appendStringInfoString(buf, "?>");
+
+ return true;
+ }
+ else
+ return false;
+}
+
+
/*
* Convert a C string to XML internal representation
*
* Written by Peter Eisentraut
.
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.370 2007/01/25 04:35:11 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.371 2007/01/25 11:53:51 petere Exp $
*
*--------------------------------------------------------------------
*/
static const char *assign_backslash_quote(const char *newval, bool doit, GucSource source);
static const char *assign_timezone_abbreviations(const char *newval, bool doit, GucSource source);
static const char *assign_xmlbinary(const char *newval, bool doit, GucSource source);
+static const char *assign_xmloption(const char *newval, bool doit, GucSource source);
static bool assign_tcp_keepalives_idle(int newval, bool doit, GucSource source);
static bool assign_tcp_keepalives_interval(int newval, bool doit, GucSource source);
static char *data_directory;
static char *custom_variable_classes;
static char *xmlbinary_string;
+static char *xmloption_string;
static int max_function_args;
static int max_index_keys;
static int max_identifier_length;
"base64", assign_xmlbinary, NULL
},
+ {
+ {"xmloption", PGC_USERSET, CLIENT_CONN_STATEMENT,
+ gettext_noop("Sets whether XML data in implicit parsing and serialization "
+ "operations is to be considered as documents or content fragments."),
+ gettext_noop("Valid values are DOCUMENT and CONTENT.")
+ },
+ &xmloption_string,
+ "content", assign_xmloption, NULL
+ },
+
{
{"temp_tablespaces", PGC_USERSET, PGC_S_FILE,
gettext_noop("Sets the tablespaces suitable for creating new objects and sort files."),
return newval;
}
+static const char *
+assign_xmloption(const char *newval, bool doit, GucSource source)
+{
+ XmlOptionType xo;
+
+ if (pg_strcasecmp(newval, "document") == 0)
+ xo = XMLOPTION_DOCUMENT;
+ else if (pg_strcasecmp(newval, "content") == 0)
+ xo = XMLOPTION_CONTENT;
+ else
+ return NULL; /* reject */
+
+ if (doit)
+ xmloption = xo;
+
+ return newval;
+}
+
static bool
assign_tcp_keepalives_idle(int newval, bool doit, GucSource source)
{
#default_transaction_read_only = off
#statement_timeout = 0 # 0 is disabled
#vacuum_freeze_min_age = 100000000
+#xmlbinary = 'base64'
+#xmloption = 'content'
# - Locale and Formatting -
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.12 2007/01/20 09:27:20 petere Exp $
+ * $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.13 2007/01/25 11:53:51 petere Exp $
*
*-------------------------------------------------------------------------
*/
extern Datum texttoxml(PG_FUNCTION_ARGS);
extern Datum xmlvalidate(PG_FUNCTION_ARGS);
+typedef enum
+{
+ XML_STANDALONE_YES,
+ XML_STANDALONE_NO,
+ XML_STANDALONE_NO_VALUE,
+ XML_STANDALONE_OMITTED
+} XmlStandaloneType;
+
extern xmltype *xmlconcat(List *args);
extern xmltype *xmlelement(XmlExprState *xmlExpr, ExprContext *econtext);
extern xmltype *xmlparse(text *data, bool is_doc, bool preserve_whitespace);
extern XmlBinaryType xmlbinary;
+typedef enum
+{
+ XMLOPTION_DOCUMENT,
+ XMLOPTION_CONTENT
+} XmlOptionType;
+
+extern XmlOptionType xmloption;
+
#endif /* XML_H */
(1 row)
SELECT xmlconcat(1, 2);
-ERROR: argument of XMLCONCAT must be type xml, not type integer
+ERROR: argument of XMLCONCAT must be type "xml", not type integer
SELECT xmlconcat('bad', '
ERROR: invalid XML content
DETAIL: Entity: line 1: parser error : Couldn't find end of Start Tag syntax line 1
^
SELECT xmlconcat('', NULL, '');
+ xmlconcat
+--------------
+
+(1 row)
+
+SELECT xmlconcat('', NULL, '');
xmlconcat
-----------------------------------
xmlroot
---------
-
(1 row)
SELECT xmlroot(xml '', version '2.0');
- xmlroot
------------------------
-
-
-
+ xmlroot
+-----------------------------
+
+(1 row)
+
+SELECT xmlroot(xml '', version no value, standalone yes);
+ xmlroot
+----------------------------------------------
+
+(1 row)
+
+SELECT xmlroot(xml '', version no value, standalone yes);
+ xmlroot
+----------------------------------------------
+
(1 row)
SELECT xmlroot(xmlroot(xml '', version '1.0'), version '1.1', standalone no);
- xmlroot
----------------------------------------
-
+ xmlroot
+---------------------------------------------
+
+(1 row)
+
+SELECT xmlroot('', version no value, standalone no);
+ xmlroot
+---------------------------------------------
+
+(1 row)
+
+SELECT xmlroot('', version no value, standalone no value);
+ xmlroot
+---------
-
+(1 row)
+
+SELECT xmlroot('', version no value);
+ xmlroot
+----------------------------------------------
+
(1 row)
SELECT xmlroot (
version '1.0',
standalone yes
);
- xmlroot
-----------------------------------------------------
-
- foo
-
+ xmlroot
+------------------------------------------------------------------------------------------
+ foo
(1 row)
SELECT xmlserialize(content data as character varying) FROM xmltest;
(1 row)
+PREPARE foo (xml) AS SELECT xmlconcat('', $1);
+SET XML OPTION DOCUMENT;
+EXECUTE foo ('');
+ xmlconcat
+--------------
+
+(1 row)
+
+EXECUTE foo ('bad');
+ERROR: invalid XML document
+DETAIL: Entity: line 1: parser error : Start tag expected, '<' not found
+bad
+^
+SET XML OPTION CONTENT;
+EXECUTE foo ('');
+ xmlconcat
+--------------
+
+(1 row)
+
+EXECUTE foo ('good');
+ xmlconcat
+------------
+ good
+(1 row)
+
SELECT xmlconcat('hello', 'you');
ERROR: no XML support in this installation
SELECT xmlconcat(1, 2);
-ERROR: argument of XMLCONCAT must be type xml, not type integer
+ERROR: argument of XMLCONCAT must be type "xml", not type integer
SELECT xmlconcat('bad', '
ERROR: no XML support in this installation
SELECT xmlconcat('', NULL, '');
ERROR: no XML support in this installation
+SELECT xmlconcat('', NULL, '');
+ERROR: no XML support in this installation
SELECT xmlelement(name element,
xmlattributes (1 as one, 'deuce' as two),
'content');
ERROR: no XML support in this installation
SELECT xmlroot(xml '', version '2.0');
ERROR: no XML support in this installation
+SELECT xmlroot(xml '', version no value, standalone yes);
+ERROR: no XML support in this installation
+SELECT xmlroot(xml '', version no value, standalone yes);
+ERROR: no XML support in this installation
SELECT xmlroot(xmlroot(xml '', version '1.0'), version '1.1', standalone no);
ERROR: no XML support in this installation
+SELECT xmlroot('', version no value, standalone no);
+ERROR: no XML support in this installation
+SELECT xmlroot('', version no value, standalone no value);
+ERROR: no XML support in this installation
+SELECT xmlroot('', version no value);
+ERROR: no XML support in this installation
SELECT xmlroot (
xmlelement (
name gazonk,
ERROR: no XML support in this installation
SELECT xmlpi(name "123");
ERROR: no XML support in this installation
+PREPARE foo (xml) AS SELECT xmlconcat('', $1);
+ERROR: no XML support in this installation
+SET XML OPTION DOCUMENT;
+EXECUTE foo ('');
+ERROR: prepared statement "foo" does not exist
+EXECUTE foo ('bad');
+ERROR: prepared statement "foo" does not exist
+SET XML OPTION CONTENT;
+EXECUTE foo ('');
+ERROR: prepared statement "foo" does not exist
+EXECUTE foo ('good');
+ERROR: prepared statement "foo" does not exist
SELECT xmlconcat(1, 2);
SELECT xmlconcat('bad', '
SELECT xmlconcat('', NULL, '');
+SELECT xmlconcat('', NULL, '');
SELECT xmlelement(name element,
SELECT xmlroot(xml '', version no value, standalone no value);
SELECT xmlroot(xml '', version '2.0');
+SELECT xmlroot(xml '', version no value, standalone yes);
+SELECT xmlroot(xml '', version no value, standalone yes);
SELECT xmlroot(xmlroot(xml '', version '1.0'), version '1.1', standalone no);
+SELECT xmlroot('', version no value, standalone no);
+SELECT xmlroot('', version no value, standalone no value);
+SELECT xmlroot('', version no value);
+
SELECT xmlroot (
xmlelement (
SELECT xmlpi(name ":::_xml_abc135.%-&_");
SELECT xmlpi(name "123");
+
+
+PREPARE foo (xml) AS SELECT xmlconcat('', $1);
+
+SET XML OPTION DOCUMENT;
+EXECUTE foo ('');
+EXECUTE foo ('bad');
+
+SET XML OPTION CONTENT;
+EXECUTE foo ('');
+EXECUTE foo ('good');