1 /*-------------------------------------------------------------------------
4 * XML data type support.
7 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/utils/adt/xml.c
12 *-------------------------------------------------------------------------
16 * Generally, XML type support is only available when libxml use was
17 * configured during the build. But even if that is not done, the
18 * type and all the functions are available, but most of them will
19 * fail. For one thing, this avoids having to manage variant catalog
20 * installations. But it also has nice effects such as that you can
21 * dump a database containing XML type data even if the server is not
22 * linked with libxml. Thus, make sure xml_out() works even if nothing
27 * Notes on memory management:
29 * Sometimes libxml allocates global structures in the hope that it can reuse
30 * them later on. This makes it impractical to change the xmlMemSetup
31 * functions on-the-fly; that is likely to lead to trying to pfree() chunks
32 * allocated with malloc() or vice versa. Since libxml might be used by
33 * loadable modules, eg libperl, our only safe choices are to change the
34 * functions at postmaster/backend launch or not at all. Since we'd rather
35 * not activate libxml in sessions that might never use it, the latter choice
36 * is the preferred one. However, for debugging purposes it can be awfully
37 * handy to constrain libxml's allocations to be done in a specific palloc
38 * context, where they're easy to track. Therefore there is code here that
39 * can be enabled in debug builds to redirect libxml's allocations into a
40 * special context LibxmlContext. It's not recommended to turn this on in
41 * a production build because of the possibility of bad interactions with
44 /* #define USE_LIBXMLCONTEXT */
49 #include <libxml/chvalid.h>
50 #include <libxml/parser.h>
51 #include <libxml/parserInternals.h>
52 #include <libxml/tree.h>
53 #include <libxml/uri.h>
54 #include <libxml/xmlerror.h>
55 #include <libxml/xmlversion.h>
56 #include <libxml/xmlwriter.h>
57 #include <libxml/xpath.h>
58 #include <libxml/xpathInternals.h>
61 * We used to check for xmlStructuredErrorContext via a configure test; but
62 * that doesn't work on Windows, so instead use this grottier method of
63 * testing the library version number.
65 #if LIBXML_VERSION >= 20704
66 #define HAVE_XMLSTRUCTUREDERRORCONTEXT 1
68 #endif /* USE_LIBXML */
70 #include "access/htup_details.h"
71 #include "catalog/namespace.h"
72 #include "catalog/pg_type.h"
73 #include "commands/dbcommands.h"
74 #include "executor/executor.h"
75 #include "executor/spi.h"
77 #include "lib/stringinfo.h"
78 #include "libpq/pqformat.h"
79 #include "mb/pg_wchar.h"
80 #include "miscadmin.h"
81 #include "nodes/execnodes.h"
82 #include "nodes/nodeFuncs.h"
83 #include "utils/array.h"
84 #include "utils/builtins.h"
85 #include "utils/date.h"
86 #include "utils/datetime.h"
87 #include "utils/lsyscache.h"
88 #include "utils/memutils.h"
89 #include "utils/rel.h"
90 #include "utils/syscache.h"
91 #include "utils/xml.h"
100 /* random number to identify PgXmlErrorContext */
101 #define ERRCXT_MAGIC 68275028
103 struct PgXmlErrorContext
106 /* strictness argument passed to pg_xml_init */
107 PgXmlStrictness strictness;
108 /* current error status and accumulated message, if any */
110 StringInfoData err_buf;
111 /* previous libxml error handling state (saved by pg_xml_init) */
112 xmlStructuredErrorFunc saved_errfunc;
114 /* previous libxml entity handler (saved by pg_xml_init) */
115 xmlExternalEntityLoader saved_entityfunc;
118 static xmlParserInputPtr xmlPgEntityLoader(const char *URL, const char *ID,
119 xmlParserCtxtPtr ctxt);
120 static void xml_errorHandler(void *data, xmlErrorPtr error);
121 static void xml_ereport_by_code(int level, int sqlcode,
122 const char *msg, int errcode);
123 static void chopStringInfoNewlines(StringInfo str);
124 static void appendStringInfoLineSeparator(StringInfo str);
126 #ifdef USE_LIBXMLCONTEXT
128 static MemoryContext LibxmlContext = NULL;
130 static void xml_memory_init(void);
131 static void *xml_palloc(size_t size);
132 static void *xml_repalloc(void *ptr, size_t size);
133 static void xml_pfree(void *ptr);
134 static char *xml_pstrdup(const char *string);
135 #endif /* USE_LIBXMLCONTEXT */
137 static xmlChar *xml_text2xmlChar(text *in);
138 static int parse_xml_decl(const xmlChar *str, size_t *lenp,
139 xmlChar **version, xmlChar **encoding, int *standalone);
140 static bool print_xml_decl(StringInfo buf, const xmlChar *version,
141 pg_enc encoding, int standalone);
142 static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
143 bool preserve_whitespace, int encoding);
144 static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
145 static int xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
146 ArrayBuildState **astate);
147 #endif /* USE_LIBXML */
149 static StringInfo query_to_xml_internal(const char *query, char *tablename,
150 const char *xmlschema, bool nulls, bool tableforest,
151 const char *targetns, bool top_level);
152 static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
153 bool nulls, bool tableforest, const char *targetns);
154 static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
155 List *relid_list, bool nulls,
156 bool tableforest, const char *targetns);
157 static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
158 bool nulls, bool tableforest,
159 const char *targetns);
160 static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
161 static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
162 static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
163 static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
164 char *tablename, bool nulls, bool tableforest,
165 const char *targetns, bool top_level);
167 #define NO_XML_SUPPORT() \
169 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
170 errmsg("unsupported XML feature"), \
171 errdetail("This functionality requires the server to be built with libxml support."), \
172 errhint("You need to rebuild PostgreSQL using --with-libxml.")))
175 /* from SQL/XML:2008 section 4.9 */
176 #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
177 #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
178 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
184 xmlChar_to_encoding(const xmlChar *encoding_name)
186 int encoding = pg_char_to_encoding((const char *) encoding_name);
190 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
191 errmsg("invalid encoding name \"%s\"",
192 (const char *) encoding_name)));
199 * xml_in uses a plain C string to VARDATA conversion, so for the time being
200 * we use the conversion function for the text datatype.
202 * This is only acceptable so long as xmltype and text use the same
206 xml_in(PG_FUNCTION_ARGS)
209 char *s = PG_GETARG_CSTRING(0);
213 vardata = (xmltype *) cstring_to_text(s);
216 * Parse the data to check if it is well-formed XML data. Assume that
217 * ERROR occurred if parsing failed.
219 doc = xml_parse(vardata, xmloption, true, GetDatabaseEncoding());
222 PG_RETURN_XML_P(vardata);
230 #define PG_XML_DEFAULT_VERSION "1.0"
234 * xml_out_internal uses a plain VARDATA to C string conversion, so for the
235 * time being we use the conversion function for the text datatype.
237 * This is only acceptable so long as xmltype and text use the same
241 xml_out_internal(xmltype *x, pg_enc target_encoding)
243 char *str = text_to_cstring((text *) x);
246 size_t len = strlen(str);
251 if ((res_code = parse_xml_decl((xmlChar *) str,
252 &len, &version, NULL, &standalone)) == 0)
256 initStringInfo(&buf);
258 if (!print_xml_decl(&buf, version, target_encoding, standalone))
261 * If we are not going to produce an XML declaration, eat a single
262 * newline in the original string to prevent empty first lines in
265 if (*(str + len) == '\n')
268 appendStringInfoString(&buf, str + len);
275 xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
276 "could not parse XML declaration in stored value",
284 xml_out(PG_FUNCTION_ARGS)
286 xmltype *x = PG_GETARG_XML_P(0);
289 * xml_out removes the encoding property in all cases. This is because we
290 * cannot control from here whether the datum will be converted to a
291 * different client encoding, so we'd do more harm than good by including
294 PG_RETURN_CSTRING(xml_out_internal(x, 0));
299 xml_recv(PG_FUNCTION_ARGS)
302 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
308 xmlChar *encodingStr = NULL;
312 * Read the data in raw format. We don't know yet what the encoding is, as
313 * that information is embedded in the xml declaration; so we have to
314 * parse that before converting to server encoding.
316 nbytes = buf->len - buf->cursor;
317 str = (char *) pq_getmsgbytes(buf, nbytes);
320 * We need a null-terminated string to pass to parse_xml_decl(). Rather
321 * than make a separate copy, make the temporary result one byte bigger
322 * than it needs to be.
324 result = palloc(nbytes + 1 + VARHDRSZ);
325 SET_VARSIZE(result, nbytes + VARHDRSZ);
326 memcpy(VARDATA(result), str, nbytes);
327 str = VARDATA(result);
330 parse_xml_decl((const xmlChar *) str, NULL, NULL, &encodingStr, NULL);
333 * If encoding wasn't explicitly specified in the XML header, treat it as
334 * UTF-8, as that's the default in XML. This is different from xml_in(),
335 * where the input has to go through the normal client to server encoding
338 encoding = encodingStr ? xmlChar_to_encoding(encodingStr) : PG_UTF8;
341 * Parse the data to check if it is well-formed XML data. Assume that
342 * xml_parse will throw ERROR if not.
344 doc = xml_parse(result, xmloption, true, encoding);
347 /* Now that we know what we're dealing with, convert to server encoding */
348 newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
351 GetDatabaseEncoding());
356 result = (xmltype *) cstring_to_text(newstr);
360 PG_RETURN_XML_P(result);
369 xml_send(PG_FUNCTION_ARGS)
371 xmltype *x = PG_GETARG_XML_P(0);
376 * xml_out_internal doesn't convert the encoding, it just prints the right
377 * declaration. pq_sendtext will do the conversion.
379 outval = xml_out_internal(x, pg_get_client_encoding());
381 pq_begintypsend(&buf);
382 pq_sendtext(&buf, outval, strlen(outval));
384 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
390 appendStringInfoText(StringInfo str, const text *t)
392 appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
398 stringinfo_to_xmltype(StringInfo buf)
400 return (xmltype *) cstring_to_text_with_len(buf->data, buf->len);
405 cstring_to_xmltype(const char *string)
407 return (xmltype *) cstring_to_text(string);
413 xmlBuffer_to_xmltype(xmlBufferPtr buf)
415 return (xmltype *) cstring_to_text_with_len((const char *) xmlBufferContent(buf),
416 xmlBufferLength(buf));
422 xmlcomment(PG_FUNCTION_ARGS)
425 text *arg = PG_GETARG_TEXT_P(0);
426 char *argdata = VARDATA(arg);
427 int len = VARSIZE(arg) - VARHDRSZ;
431 /* check for "--" in string or "-" at the end */
432 for (i = 1; i < len; i++)
434 if (argdata[i] == '-' && argdata[i - 1] == '-')
436 (errcode(ERRCODE_INVALID_XML_COMMENT),
437 errmsg("invalid XML comment")));
439 if (len > 0 && argdata[len - 1] == '-')
441 (errcode(ERRCODE_INVALID_XML_COMMENT),
442 errmsg("invalid XML comment")));
444 initStringInfo(&buf);
445 appendStringInfoString(&buf, "<!--");
446 appendStringInfoText(&buf, arg);
447 appendStringInfoString(&buf, "-->");
449 PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
459 * TODO: xmlconcat needs to merge the notations and unparsed entities
460 * of the argument values. Not very important in practice, though.
463 xmlconcat(List *args)
466 int global_standalone = 1;
467 xmlChar *global_version = NULL;
468 bool global_version_no_value = false;
472 initStringInfo(&buf);
475 xmltype *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
481 len = VARSIZE(x) - VARHDRSZ;
482 str = text_to_cstring((text *) x);
484 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
486 if (standalone == 0 && global_standalone == 1)
487 global_standalone = 0;
489 global_standalone = -1;
492 global_version_no_value = true;
493 else if (!global_version)
494 global_version = version;
495 else if (xmlStrcmp(version, global_version) != 0)
496 global_version_no_value = true;
498 appendStringInfoString(&buf, str + len);
502 if (!global_version_no_value || global_standalone >= 0)
506 initStringInfo(&buf2);
508 print_xml_decl(&buf2,
509 (!global_version_no_value) ? global_version : NULL,
513 appendStringInfoString(&buf2, buf.data);
517 return stringinfo_to_xmltype(&buf);
529 xmlconcat2(PG_FUNCTION_ARGS)
536 PG_RETURN_XML_P(PG_GETARG_XML_P(1));
538 else if (PG_ARGISNULL(1))
539 PG_RETURN_XML_P(PG_GETARG_XML_P(0));
541 PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
542 PG_GETARG_XML_P(1))));
547 texttoxml(PG_FUNCTION_ARGS)
549 text *data = PG_GETARG_TEXT_P(0);
551 PG_RETURN_XML_P(xmlparse(data, xmloption, true));
556 xmltotext(PG_FUNCTION_ARGS)
558 xmltype *data = PG_GETARG_XML_P(0);
560 /* It's actually binary compatible. */
561 PG_RETURN_TEXT_P((text *) data);
566 xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
568 if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
570 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
571 errmsg("not an XML document")));
573 /* It's actually binary compatible, save for the above check. */
574 return (text *) data;
579 xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
582 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
584 List *named_arg_strings;
589 PgXmlErrorContext *xmlerrcxt;
590 volatile xmlBufferPtr buf = NULL;
591 volatile xmlTextWriterPtr writer = NULL;
594 * We first evaluate all the arguments, then start up libxml and create
595 * the result. This avoids issues if one of the arguments involves a call
596 * to some other function or subsystem that wants to use libxml on its own
599 named_arg_strings = NIL;
601 foreach(arg, xmlExpr->named_args)
603 ExprState *e = (ExprState *) lfirst(arg);
608 value = ExecEvalExpr(e, econtext, &isnull, NULL);
612 str = map_sql_value_to_xml_value(value, exprType((Node *) e->expr), false);
613 named_arg_strings = lappend(named_arg_strings, str);
618 foreach(arg, xmlExpr->args)
620 ExprState *e = (ExprState *) lfirst(arg);
625 value = ExecEvalExpr(e, econtext, &isnull, NULL);
626 /* here we can just forget NULL elements immediately */
629 str = map_sql_value_to_xml_value(value,
630 exprType((Node *) e->expr), true);
631 arg_strings = lappend(arg_strings, str);
635 /* now safe to run libxml */
636 xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
640 buf = xmlBufferCreate();
641 if (buf == NULL || xmlerrcxt->err_occurred)
642 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
643 "could not allocate xmlBuffer");
644 writer = xmlNewTextWriterMemory(buf, 0);
645 if (writer == NULL || xmlerrcxt->err_occurred)
646 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
647 "could not allocate xmlTextWriter");
649 xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
651 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
653 char *str = (char *) lfirst(arg);
654 char *argname = strVal(lfirst(narg));
657 xmlTextWriterWriteAttribute(writer,
662 foreach(arg, arg_strings)
664 char *str = (char *) lfirst(arg);
666 xmlTextWriterWriteRaw(writer, (xmlChar *) str);
669 xmlTextWriterEndElement(writer);
671 /* we MUST do this now to flush data out to the buffer ... */
672 xmlFreeTextWriter(writer);
675 result = xmlBuffer_to_xmltype(buf);
680 xmlFreeTextWriter(writer);
684 pg_xml_done(xmlerrcxt, true);
692 pg_xml_done(xmlerrcxt, false);
703 xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
708 doc = xml_parse(data, xmloption_arg, preserve_whitespace,
709 GetDatabaseEncoding());
712 return (xmltype *) data;
721 xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
727 if (pg_strcasecmp(target, "xml") == 0)
729 (errcode(ERRCODE_SYNTAX_ERROR), /* really */
730 errmsg("invalid XML processing instruction"),
731 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
734 * Following the SQL standard, the null check comes after the syntax check
737 *result_is_null = arg_is_null;
741 initStringInfo(&buf);
743 appendStringInfo(&buf, "<?%s", target);
749 string = text_to_cstring(arg);
750 if (strstr(string, "?>") != NULL)
752 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
753 errmsg("invalid XML processing instruction"),
754 errdetail("XML processing instruction cannot contain \"?>\".")));
756 appendStringInfoChar(&buf, ' ');
757 appendStringInfoString(&buf, string + strspn(string, " "));
760 appendStringInfoString(&buf, "?>");
762 result = stringinfo_to_xmltype(&buf);
773 xmlroot(xmltype *data, text *version, int standalone)
778 xmlChar *orig_version;
782 len = VARSIZE(data) - VARHDRSZ;
783 str = text_to_cstring((text *) data);
785 parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
788 orig_version = xml_text2xmlChar(version);
794 case XML_STANDALONE_YES:
797 case XML_STANDALONE_NO:
800 case XML_STANDALONE_NO_VALUE:
801 orig_standalone = -1;
803 case XML_STANDALONE_OMITTED:
804 /* leave original value */
808 initStringInfo(&buf);
809 print_xml_decl(&buf, orig_version, 0, orig_standalone);
810 appendStringInfoString(&buf, str + len);
812 return stringinfo_to_xmltype(&buf);
821 * Validate document (given as string) against DTD (given as external link)
823 * This has been removed because it is a security hole: unprivileged users
824 * should not be able to use Postgres to fetch arbitrary external files,
825 * which unfortunately is exactly what libxml is willing to do with the DTD
829 xmlvalidate(PG_FUNCTION_ARGS)
832 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
833 errmsg("xmlvalidate is not implemented")));
839 xml_is_document(xmltype *arg)
843 volatile xmlDocPtr doc = NULL;
844 MemoryContext ccxt = CurrentMemoryContext;
846 /* We want to catch ereport(INVALID_XML_DOCUMENT) and return false */
849 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
850 GetDatabaseEncoding());
858 ecxt = MemoryContextSwitchTo(ccxt);
859 errdata = CopyErrorData();
860 if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
867 MemoryContextSwitchTo(ecxt);
877 #else /* not USE_LIBXML */
880 #endif /* not USE_LIBXML */
887 * pg_xml_init_library --- set up for use of libxml
889 * This should be called by each function that is about to use libxml
890 * facilities but doesn't require error handling. It initializes libxml
891 * and verifies compatibility with the loaded libxml version. These are
892 * once-per-session activities.
894 * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
898 pg_xml_init_library(void)
900 static bool first_time = true;
904 /* Stuff we need do only once per session */
907 * Currently, we have no pure UTF-8 support for internals -- check if
910 if (sizeof(char) != sizeof(xmlChar))
912 (errmsg("could not initialize XML library"),
913 errdetail("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u.",
914 (int) sizeof(char), (int) sizeof(xmlChar))));
916 #ifdef USE_LIBXMLCONTEXT
917 /* Set up libxml's memory allocation our way */
921 /* Check library compatibility */
929 * pg_xml_init --- set up for use of libxml and register an error handler
931 * This should be called by each function that is about to use libxml
932 * facilities and requires error handling. It initializes libxml with
933 * pg_xml_init_library() and establishes our libxml error handler.
935 * strictness determines which errors are reported and which are ignored.
937 * Calls to this function MUST be followed by a PG_TRY block that guarantees
938 * that pg_xml_done() is called during either normal or error exit.
940 * This is exported for use by contrib/xml2, as well as other code that might
941 * wish to share use of this module's libxml error handler.
944 pg_xml_init(PgXmlStrictness strictness)
946 PgXmlErrorContext *errcxt;
949 /* Do one-time setup if needed */
950 pg_xml_init_library();
952 /* Create error handling context structure */
953 errcxt = (PgXmlErrorContext *) palloc(sizeof(PgXmlErrorContext));
954 errcxt->magic = ERRCXT_MAGIC;
955 errcxt->strictness = strictness;
956 errcxt->err_occurred = false;
957 initStringInfo(&errcxt->err_buf);
960 * Save original error handler and install ours. libxml originally didn't
961 * distinguish between the contexts for generic and for structured error
962 * handlers. If we're using an old libxml version, we must thus save the
963 * generic error context, even though we're using a structured error
966 errcxt->saved_errfunc = xmlStructuredError;
968 #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
969 errcxt->saved_errcxt = xmlStructuredErrorContext;
971 errcxt->saved_errcxt = xmlGenericErrorContext;
974 xmlSetStructuredErrorFunc((void *) errcxt, xml_errorHandler);
977 * Verify that xmlSetStructuredErrorFunc set the context variable we
978 * expected it to. If not, the error context pointer we just saved is not
979 * the correct thing to restore, and since that leaves us without a way to
980 * restore the context in pg_xml_done, we must fail.
982 * The only known situation in which this test fails is if we compile with
983 * headers from a libxml2 that doesn't track the structured error context
984 * separately (< 2.7.4), but at runtime use a version that does, or vice
985 * versa. The libxml2 authors did not treat that change as constituting
986 * an ABI break, so the LIBXML_TEST_VERSION test in pg_xml_init_library
987 * fails to protect us from this.
990 #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
991 new_errcxt = xmlStructuredErrorContext;
993 new_errcxt = xmlGenericErrorContext;
996 if (new_errcxt != (void *) errcxt)
998 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
999 errmsg("could not set up XML error handler"),
1000 errhint("This probably indicates that the version of libxml2"
1001 " being used is not compatible with the libxml2"
1002 " header files that PostgreSQL was built with.")));
1005 * Also, install an entity loader to prevent unwanted fetches of external
1008 errcxt->saved_entityfunc = xmlGetExternalEntityLoader();
1009 xmlSetExternalEntityLoader(xmlPgEntityLoader);
1016 * pg_xml_done --- restore previous libxml error handling
1018 * Resets libxml's global error-handling state to what it was before
1019 * pg_xml_init() was called.
1021 * This routine verifies that all pending errors have been dealt with
1022 * (in assert-enabled builds, anyway).
1025 pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
1029 /* An assert seems like enough protection here */
1030 Assert(errcxt->magic == ERRCXT_MAGIC);
1033 * In a normal exit, there should be no un-handled libxml errors. But we
1034 * shouldn't try to enforce this during error recovery, since the longjmp
1035 * could have been thrown before xml_ereport had a chance to run.
1037 Assert(!errcxt->err_occurred || isError);
1040 * Check that libxml's global state is correct, warn if not. This is a
1041 * real test and not an Assert because it has a higher probability of
1044 #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
1045 cur_errcxt = xmlStructuredErrorContext;
1047 cur_errcxt = xmlGenericErrorContext;
1050 if (cur_errcxt != (void *) errcxt)
1051 elog(WARNING, "libxml error handling state is out of sync with xml.c");
1053 /* Restore the saved handlers */
1054 xmlSetStructuredErrorFunc(errcxt->saved_errcxt, errcxt->saved_errfunc);
1055 xmlSetExternalEntityLoader(errcxt->saved_entityfunc);
1058 * Mark the struct as invalid, just in case somebody somehow manages to
1059 * call xml_errorHandler or xml_ereport with it.
1063 /* Release memory */
1064 pfree(errcxt->err_buf.data);
1070 * pg_xml_error_occurred() --- test the error flag
1073 pg_xml_error_occurred(PgXmlErrorContext *errcxt)
1075 return errcxt->err_occurred;
1080 * SQL/XML allows storing "XML documents" or "XML content". "XML
1081 * documents" are specified by the XML specification and are parsed
1082 * easily by libxml. "XML content" is specified by SQL/XML as the
1083 * production "XMLDecl? content". But libxml can only parse the
1084 * "content" part, so we have to parse the XML declaration ourselves
1088 #define CHECK_XML_SPACE(p) \
1090 if (!xmlIsBlank_ch(*(p))) \
1091 return XML_ERR_SPACE_REQUIRED; \
1094 #define SKIP_XML_SPACE(p) \
1095 while (xmlIsBlank_ch(*(p))) (p)++
1097 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1098 /* Beware of multiple evaluations of argument! */
1099 #define PG_XMLISNAMECHAR(c) \
1100 (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
1101 || xmlIsDigit_ch(c) \
1102 || c == '.' || c == '-' || c == '_' || c == ':' \
1103 || xmlIsCombiningQ(c) \
1104 || xmlIsExtender_ch(c))
1106 /* pnstrdup, but deal with xmlChar not char; len is measured in xmlChars */
1108 xml_pnstrdup(const xmlChar *str, size_t len)
1112 result = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
1113 memcpy(result, str, len * sizeof(xmlChar));
1119 * str is the null-terminated input string. Remaining arguments are
1120 * output arguments; each can be NULL if value is not wanted.
1121 * version and encoding are returned as locally-palloc'd strings.
1122 * Result is 0 if OK, an error code if not.
1125 parse_xml_decl(const xmlChar *str, size_t *lenp,
1126 xmlChar **version, xmlChar **encoding, int *standalone)
1129 const xmlChar *save_p;
1135 * Only initialize libxml. We don't need error handling here, but we do
1136 * need to make sure libxml is initialized before calling any of its
1137 * functions. Note that this is safe (and a no-op) if caller has already
1138 * done pg_xml_init().
1140 pg_xml_init_library();
1142 /* Initialize output arguments to "not present" */
1152 if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
1155 /* if next char is name char, it's a PI like <?xml-stylesheet ...?> */
1156 utf8len = strlen((const char *) (p + 5));
1157 utf8char = xmlGetUTF8Char(p + 5, &utf8len);
1158 if (PG_XMLISNAMECHAR(utf8char))
1166 if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
1167 return XML_ERR_VERSION_MISSING;
1171 return XML_ERR_VERSION_MISSING;
1175 if (*p == '\'' || *p == '"')
1179 q = xmlStrchr(p + 1, *p);
1181 return XML_ERR_VERSION_MISSING;
1184 *version = xml_pnstrdup(p + 1, q - p - 1);
1188 return XML_ERR_VERSION_MISSING;
1193 if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
1195 CHECK_XML_SPACE(save_p);
1199 return XML_ERR_MISSING_ENCODING;
1203 if (*p == '\'' || *p == '"')
1207 q = xmlStrchr(p + 1, *p);
1209 return XML_ERR_MISSING_ENCODING;
1212 *encoding = xml_pnstrdup(p + 1, q - p - 1);
1216 return XML_ERR_MISSING_ENCODING;
1226 if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1228 CHECK_XML_SPACE(save_p);
1232 return XML_ERR_STANDALONE_VALUE;
1235 if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
1236 xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1242 else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
1243 xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1250 return XML_ERR_STANDALONE_VALUE;
1258 if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1259 return XML_ERR_XMLDECL_NOT_FINISHED;
1265 for (p = str; p < str + len; p++)
1267 return XML_ERR_INVALID_CHAR;
1277 * Write an XML declaration. On output, we adjust the XML declaration
1278 * as follows. (These rules are the moral equivalent of the clause
1279 * "Serialization of an XML value" in the SQL standard.)
1281 * We try to avoid generating an XML declaration if possible. This is
1282 * so that you don't get trivial things like xml '<foo/>' resulting in
1283 * '<?xml version="1.0"?><foo/>', which would surely be annoying. We
1284 * must provide a declaration if the standalone property is specified
1285 * or if we include an encoding declaration. If we have a
1286 * declaration, we must specify a version (XML requires this).
1287 * Otherwise we only make a declaration if the version is not "1.0",
1288 * which is the default version specified in SQL:2003.
1291 print_xml_decl(StringInfo buf, const xmlChar *version,
1292 pg_enc encoding, int standalone)
1294 if ((version && strcmp((const char *) version, PG_XML_DEFAULT_VERSION) != 0)
1295 || (encoding && encoding != PG_UTF8)
1296 || standalone != -1)
1298 appendStringInfoString(buf, "<?xml");
1301 appendStringInfo(buf, " version=\"%s\"", version);
1303 appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1305 if (encoding && encoding != PG_UTF8)
1308 * XXX might be useful to convert this to IANA names (ISO-8859-1
1309 * instead of LATIN1 etc.); needs field experience
1311 appendStringInfo(buf, " encoding=\"%s\"",
1312 pg_encoding_to_char(encoding));
1315 if (standalone == 1)
1316 appendStringInfoString(buf, " standalone=\"yes\"");
1317 else if (standalone == 0)
1318 appendStringInfoString(buf, " standalone=\"no\"");
1319 appendStringInfoString(buf, "?>");
1329 * Convert a C string to XML internal representation
1331 * Note: it is caller's responsibility to xmlFreeDoc() the result,
1332 * else a permanent memory leak will ensue!
1334 * TODO maybe libxml2's xmlreader is better? (do not construct DOM,
1335 * yet do not use SAX - see xmlreader.c)
1338 xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1343 xmlChar *utf8string;
1344 PgXmlErrorContext *xmlerrcxt;
1345 volatile xmlParserCtxtPtr ctxt = NULL;
1346 volatile xmlDocPtr doc = NULL;
1348 len = VARSIZE(data) - VARHDRSZ; /* will be useful later */
1349 string = xml_text2xmlChar(data);
1351 utf8string = pg_do_encoding_conversion(string,
1356 /* Start up libxml and its parser */
1357 xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_WELLFORMED);
1359 /* Use a TRY block to ensure we clean up correctly */
1364 ctxt = xmlNewParserCtxt();
1365 if (ctxt == NULL || xmlerrcxt->err_occurred)
1366 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
1367 "could not allocate parser context");
1369 if (xmloption_arg == XMLOPTION_DOCUMENT)
1372 * Note, that here we try to apply DTD defaults
1373 * (XML_PARSE_DTDATTR) according to SQL/XML:2008 GR 10.16.7.d:
1374 * 'Default values defined by internal DTD are applied'. As for
1375 * external DTDs, we try to support them too, (see SQL/XML:2008 GR
1378 doc = xmlCtxtReadDoc(ctxt, utf8string,
1381 XML_PARSE_NOENT | XML_PARSE_DTDATTR
1382 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1383 if (doc == NULL || xmlerrcxt->err_occurred)
1384 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
1385 "invalid XML document");
1394 res_code = parse_xml_decl(utf8string,
1395 &count, &version, NULL, &standalone);
1397 xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
1398 "invalid XML content: invalid XML declaration",
1401 doc = xmlNewDoc(version);
1402 Assert(doc->encoding == NULL);
1403 doc->encoding = xmlStrdup((const xmlChar *) "UTF-8");
1404 doc->standalone = standalone;
1406 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
1407 utf8string + count, NULL);
1408 if (res_code != 0 || xmlerrcxt->err_occurred)
1409 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_CONTENT,
1410 "invalid XML content");
1418 xmlFreeParserCtxt(ctxt);
1420 pg_xml_done(xmlerrcxt, true);
1426 xmlFreeParserCtxt(ctxt);
1428 pg_xml_done(xmlerrcxt, false);
1435 * xmlChar<->text conversions
1438 xml_text2xmlChar(text *in)
1440 return (xmlChar *) text_to_cstring(in);
1444 #ifdef USE_LIBXMLCONTEXT
1447 * Manage the special context used for all libxml allocations (but only
1448 * in special debug builds; see notes at top of file)
1451 xml_memory_init(void)
1453 /* Create memory context if not there already */
1454 if (LibxmlContext == NULL)
1455 LibxmlContext = AllocSetContextCreate(TopMemoryContext,
1457 ALLOCSET_DEFAULT_MINSIZE,
1458 ALLOCSET_DEFAULT_INITSIZE,
1459 ALLOCSET_DEFAULT_MAXSIZE);
1461 /* Re-establish the callbacks even if already set */
1462 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
1466 * Wrappers for memory management functions
1469 xml_palloc(size_t size)
1471 return MemoryContextAlloc(LibxmlContext, size);
1476 xml_repalloc(void *ptr, size_t size)
1478 return repalloc(ptr, size);
1483 xml_pfree(void *ptr)
1485 /* At least some parts of libxml assume xmlFree(NULL) is allowed */
1492 xml_pstrdup(const char *string)
1494 return MemoryContextStrdup(LibxmlContext, string);
1496 #endif /* USE_LIBXMLCONTEXT */
1500 * xmlPgEntityLoader --- entity loader callback function
1502 * Silently prevent any external entity URL from being loaded. We don't want
1503 * to throw an error, so instead make the entity appear to expand to an empty
1506 * We would prefer to allow loading entities that exist in the system's
1507 * global XML catalog; but the available libxml2 APIs make that a complex
1508 * and fragile task. For now, just shut down all external access.
1510 static xmlParserInputPtr
1511 xmlPgEntityLoader(const char *URL, const char *ID,
1512 xmlParserCtxtPtr ctxt)
1514 return xmlNewStringInputStream(ctxt, (const xmlChar *) "");
1519 * xml_ereport --- report an XML-related error
1521 * The "msg" is the SQL-level message; some can be adopted from the SQL/XML
1522 * standard. This function adds libxml's native error message, if any, as
1525 * This is exported for modules that want to share the core libxml error
1526 * handler. Note that pg_xml_init() *must* have been called previously.
1529 xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
1533 /* Defend against someone passing us a bogus context struct */
1534 if (errcxt->magic != ERRCXT_MAGIC)
1535 elog(ERROR, "xml_ereport called with invalid PgXmlErrorContext");
1537 /* Flag that the current libxml error has been reported */
1538 errcxt->err_occurred = false;
1540 /* Include detail only if we have some text from libxml */
1541 if (errcxt->err_buf.len > 0)
1542 detail = errcxt->err_buf.data;
1548 errmsg_internal("%s", msg),
1549 detail ? errdetail_internal("%s", detail) : 0));
1554 * Error handler for libxml errors and warnings
1557 xml_errorHandler(void *data, xmlErrorPtr error)
1559 PgXmlErrorContext *xmlerrcxt = (PgXmlErrorContext *) data;
1560 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) error->ctxt;
1561 xmlParserInputPtr input = (ctxt != NULL) ? ctxt->input : NULL;
1562 xmlNodePtr node = error->node;
1563 const xmlChar *name = (node != NULL &&
1564 node->type == XML_ELEMENT_NODE) ? node->name : NULL;
1565 int domain = error->domain;
1566 int level = error->level;
1567 StringInfo errorBuf;
1570 * Defend against someone passing us a bogus context struct.
1572 * We force a backend exit if this check fails because longjmp'ing out of
1573 * libxml would likely render it unsafe to use further.
1575 if (xmlerrcxt->magic != ERRCXT_MAGIC)
1576 elog(FATAL, "xml_errorHandler called with invalid PgXmlErrorContext");
1579 * Older libxml versions report some errors differently.
1580 * First, some errors were previously reported as coming from the parser
1581 * domain but are now reported as coming from the namespace domain.
1582 * Second, some warnings were upgraded to errors.
1583 * We attempt to compensate for that here.
1586 switch (error->code)
1588 case XML_WAR_NS_URI:
1589 level = XML_ERR_ERROR;
1590 domain = XML_FROM_NAMESPACE;
1593 case XML_ERR_NS_DECL_ERROR:
1594 case XML_WAR_NS_URI_RELATIVE:
1595 case XML_WAR_NS_COLUMN:
1596 case XML_NS_ERR_XML_NAMESPACE:
1597 case XML_NS_ERR_UNDEFINED_NAMESPACE:
1598 case XML_NS_ERR_QNAME:
1599 case XML_NS_ERR_ATTRIBUTE_REDEFINED:
1600 case XML_NS_ERR_EMPTY:
1601 domain = XML_FROM_NAMESPACE;
1605 /* Decide whether to act on the error or not */
1608 case XML_FROM_PARSER:
1610 case XML_FROM_MEMORY:
1614 * Suppress warnings about undeclared entities. We need to do
1615 * this to avoid problems due to not loading DTD definitions.
1617 if (error->code == XML_WAR_UNDECLARED_ENTITY)
1620 /* Otherwise, accept error regardless of the parsing purpose */
1624 /* Ignore error if only doing well-formedness check */
1625 if (xmlerrcxt->strictness == PG_XML_STRICTNESS_WELLFORMED)
1630 /* Prepare error message in errorBuf */
1631 errorBuf = makeStringInfo();
1633 if (error->line > 0)
1634 appendStringInfo(errorBuf, "line %d: ", error->line);
1636 appendStringInfo(errorBuf, "element %s: ", name);
1637 appendStringInfoString(errorBuf, error->message);
1640 * Append context information to errorBuf.
1642 * xmlParserPrintFileContext() uses libxml's "generic" error handler to
1643 * write the context. Since we don't want to duplicate libxml
1644 * functionality here, we set up a generic error handler temporarily.
1646 * We use appendStringInfo() directly as libxml's generic error handler.
1647 * This should work because it has essentially the same signature as
1648 * libxml expects, namely (void *ptr, const char *msg, ...).
1652 xmlGenericErrorFunc errFuncSaved = xmlGenericError;
1653 void *errCtxSaved = xmlGenericErrorContext;
1655 xmlSetGenericErrorFunc((void *) errorBuf,
1656 (xmlGenericErrorFunc) appendStringInfo);
1658 /* Add context information to errorBuf */
1659 appendStringInfoLineSeparator(errorBuf);
1661 xmlParserPrintFileContext(input);
1663 /* Restore generic error func */
1664 xmlSetGenericErrorFunc(errCtxSaved, errFuncSaved);
1667 /* Get rid of any trailing newlines in errorBuf */
1668 chopStringInfoNewlines(errorBuf);
1671 * Legacy error handling mode. err_occurred is never set, we just add the
1672 * message to err_buf. This mode exists because the xml2 contrib module
1673 * uses our error-handling infrastructure, but we don't want to change its
1674 * behaviour since it's deprecated anyway. This is also why we don't
1675 * distinguish between notices, warnings and errors here --- the old-style
1676 * generic error handler wouldn't have done that either.
1678 if (xmlerrcxt->strictness == PG_XML_STRICTNESS_LEGACY)
1680 appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
1681 appendStringInfoString(&xmlerrcxt->err_buf, errorBuf->data);
1683 pfree(errorBuf->data);
1689 * We don't want to ereport() here because that'd probably leave libxml in
1690 * an inconsistent state. Instead, we remember the error and ereport()
1691 * from xml_ereport().
1693 * Warnings and notices can be reported immediately since they won't cause
1694 * a longjmp() out of libxml.
1696 if (level >= XML_ERR_ERROR)
1698 appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
1699 appendStringInfoString(&xmlerrcxt->err_buf, errorBuf->data);
1701 xmlerrcxt->err_occurred = true;
1703 else if (level >= XML_ERR_WARNING)
1706 (errmsg_internal("%s", errorBuf->data)));
1711 (errmsg_internal("%s", errorBuf->data)));
1714 pfree(errorBuf->data);
1720 * Wrapper for "ereport" function for XML-related errors. The "msg"
1721 * is the SQL-level message; some can be adopted from the SQL/XML
1722 * standard. This function uses "code" to create a textual detail
1723 * message. At the moment, we only need to cover those codes that we
1724 * may raise in this file.
1727 xml_ereport_by_code(int level, int sqlcode,
1728 const char *msg, int code)
1734 case XML_ERR_INVALID_CHAR:
1735 det = gettext_noop("Invalid character value.");
1737 case XML_ERR_SPACE_REQUIRED:
1738 det = gettext_noop("Space required.");
1740 case XML_ERR_STANDALONE_VALUE:
1741 det = gettext_noop("standalone accepts only 'yes' or 'no'.");
1743 case XML_ERR_VERSION_MISSING:
1744 det = gettext_noop("Malformed declaration: missing version.");
1746 case XML_ERR_MISSING_ENCODING:
1747 det = gettext_noop("Missing encoding in text declaration.");
1749 case XML_ERR_XMLDECL_NOT_FINISHED:
1750 det = gettext_noop("Parsing XML declaration: '?>' expected.");
1753 det = gettext_noop("Unrecognized libxml error code: %d.");
1759 errmsg_internal("%s", msg),
1760 errdetail(det, code)));
1765 * Remove all trailing newlines from a StringInfo string
1768 chopStringInfoNewlines(StringInfo str)
1770 while (str->len > 0 && str->data[str->len - 1] == '\n')
1771 str->data[--str->len] = '\0';
1776 * Append a newline after removing any existing trailing newlines
1779 appendStringInfoLineSeparator(StringInfo str)
1781 chopStringInfoNewlines(str);
1783 appendStringInfoChar(str, '\n');
1788 * Convert one char in the current server encoding to a Unicode codepoint.
1791 sqlchar_to_unicode(char *s)
1794 pg_wchar ret[2]; /* need space for trailing zero */
1796 utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1798 GetDatabaseEncoding(),
1801 pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret,
1802 pg_encoding_mblen(PG_UTF8, utf8string));
1804 if (utf8string != s)
1812 is_valid_xml_namefirst(pg_wchar c)
1814 /* (Letter | '_' | ':') */
1815 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1816 || c == '_' || c == ':');
1821 is_valid_xml_namechar(pg_wchar c)
1823 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1824 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1826 || c == '.' || c == '-' || c == '_' || c == ':'
1827 || xmlIsCombiningQ(c)
1828 || xmlIsExtenderQ(c));
1830 #endif /* USE_LIBXML */
1834 * Map SQL identifier to XML name; see SQL/XML:2008 section 9.1.
1837 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1845 * SQL/XML doesn't make use of this case anywhere, so it's probably a
1848 Assert(fully_escaped || !escape_period);
1850 initStringInfo(&buf);
1852 for (p = ident; *p; p += pg_mblen(p))
1854 if (*p == ':' && (p == ident || fully_escaped))
1855 appendStringInfoString(&buf, "_x003A_");
1856 else if (*p == '_' && *(p + 1) == 'x')
1857 appendStringInfoString(&buf, "_x005F_");
1858 else if (fully_escaped && p == ident &&
1859 pg_strncasecmp(p, "xml", 3) == 0)
1862 appendStringInfoString(&buf, "_x0078_");
1864 appendStringInfoString(&buf, "_x0058_");
1866 else if (escape_period && *p == '.')
1867 appendStringInfoString(&buf, "_x002E_");
1870 pg_wchar u = sqlchar_to_unicode(p);
1873 ? !is_valid_xml_namefirst(u)
1874 : !is_valid_xml_namechar(u))
1875 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1877 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1882 #else /* not USE_LIBXML */
1885 #endif /* not USE_LIBXML */
1890 * Map a Unicode codepoint into the current server encoding.
1893 unicode_to_sqlchar(pg_wchar c)
1895 unsigned char utf8string[5]; /* need room for trailing zero */
1898 memset(utf8string, 0, sizeof(utf8string));
1899 unicode_to_utf8(c, utf8string);
1901 result = (char *) pg_do_encoding_conversion(utf8string,
1902 pg_encoding_mblen(PG_UTF8,
1903 (char *) utf8string),
1905 GetDatabaseEncoding());
1906 /* if pg_do_encoding_conversion didn't strdup, we must */
1907 if (result == (char *) utf8string)
1908 result = pstrdup(result);
1914 * Map XML name to SQL identifier; see SQL/XML:2008 section 9.3.
1917 map_xml_name_to_sql_identifier(char *name)
1922 initStringInfo(&buf);
1924 for (p = name; *p; p += pg_mblen(p))
1926 if (*p == '_' && *(p + 1) == 'x'
1927 && isxdigit((unsigned char) *(p + 2))
1928 && isxdigit((unsigned char) *(p + 3))
1929 && isxdigit((unsigned char) *(p + 4))
1930 && isxdigit((unsigned char) *(p + 5))
1935 sscanf(p + 2, "%X", &u);
1936 appendStringInfoString(&buf, unicode_to_sqlchar(u));
1940 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1947 * Map SQL value to XML value; see SQL/XML:2008 section 9.8.
1949 * When xml_escape_strings is true, then certain characters in string
1950 * values are replaced by entity references (< etc.), as specified
1951 * in SQL/XML:2008 section 9.8 GR 9) a) iii). This is normally what is
1952 * wanted. The false case is mainly useful when the resulting value
1953 * is used with xmlTextWriterWriteAttribute() to write out an
1954 * attribute, because that function does the escaping itself.
1957 map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
1959 if (type_is_array_domain(type))
1972 array = DatumGetArrayTypeP(value);
1973 elmtype = ARR_ELEMTYPE(array);
1974 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1976 deconstruct_array(array, elmtype,
1977 elmlen, elmbyval, elmalign,
1978 &elem_values, &elem_nulls,
1981 initStringInfo(&buf);
1983 for (i = 0; i < num_elems; i++)
1987 appendStringInfoString(&buf, "<element>");
1988 appendStringInfoString(&buf,
1989 map_sql_value_to_xml_value(elem_values[i],
1991 appendStringInfoString(&buf, "</element>");
2006 * Flatten domains; the special-case treatments below should apply to,
2007 * eg, domains over boolean not just boolean.
2009 type = getBaseType(type);
2012 * Special XSD formatting for some data types
2017 if (DatumGetBool(value))
2026 char buf[MAXDATELEN + 1];
2028 date = DatumGetDateADT(value);
2029 /* XSD doesn't support infinite values */
2030 if (DATE_NOT_FINITE(date))
2032 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2033 errmsg("date out of range"),
2034 errdetail("XML does not support infinite date values.")));
2035 j2date(date + POSTGRES_EPOCH_JDATE,
2036 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2037 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
2039 return pstrdup(buf);
2044 Timestamp timestamp;
2047 char buf[MAXDATELEN + 1];
2049 timestamp = DatumGetTimestamp(value);
2051 /* XSD doesn't support infinite values */
2052 if (TIMESTAMP_NOT_FINITE(timestamp))
2054 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2055 errmsg("timestamp out of range"),
2056 errdetail("XML does not support infinite timestamp values.")));
2057 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2058 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2061 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2062 errmsg("timestamp out of range")));
2064 return pstrdup(buf);
2067 case TIMESTAMPTZOID:
2069 TimestampTz timestamp;
2073 const char *tzn = NULL;
2074 char buf[MAXDATELEN + 1];
2076 timestamp = DatumGetTimestamp(value);
2078 /* XSD doesn't support infinite values */
2079 if (TIMESTAMP_NOT_FINITE(timestamp))
2081 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2082 errmsg("timestamp out of range"),
2083 errdetail("XML does not support infinite timestamp values.")));
2084 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2085 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2088 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2089 errmsg("timestamp out of range")));
2091 return pstrdup(buf);
2097 bytea *bstr = DatumGetByteaPP(value);
2098 PgXmlErrorContext *xmlerrcxt;
2099 volatile xmlBufferPtr buf = NULL;
2100 volatile xmlTextWriterPtr writer = NULL;
2103 xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2107 buf = xmlBufferCreate();
2108 if (buf == NULL || xmlerrcxt->err_occurred)
2109 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2110 "could not allocate xmlBuffer");
2111 writer = xmlNewTextWriterMemory(buf, 0);
2112 if (writer == NULL || xmlerrcxt->err_occurred)
2113 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2114 "could not allocate xmlTextWriter");
2116 if (xmlbinary == XMLBINARY_BASE64)
2117 xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2118 0, VARSIZE_ANY_EXHDR(bstr));
2120 xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2121 0, VARSIZE_ANY_EXHDR(bstr));
2123 /* we MUST do this now to flush data out to the buffer */
2124 xmlFreeTextWriter(writer);
2127 result = pstrdup((const char *) xmlBufferContent(buf));
2132 xmlFreeTextWriter(writer);
2136 pg_xml_done(xmlerrcxt, true);
2144 pg_xml_done(xmlerrcxt, false);
2148 #endif /* USE_LIBXML */
2153 * otherwise, just use the type's native text representation
2155 getTypeOutputInfo(type, &typeOut, &isvarlena);
2156 str = OidOutputFunctionCall(typeOut, value);
2158 /* ... exactly as-is for XML, and when escaping is not wanted */
2159 if (type == XMLOID || !xml_escape_strings)
2162 /* otherwise, translate special characters as needed */
2163 return escape_xml(str);
2169 * Escape characters in text that have special meanings in XML.
2171 * Returns a palloc'd string.
2173 * NB: this is intentionally not dependent on libxml.
2176 escape_xml(const char *str)
2181 initStringInfo(&buf);
2182 for (p = str; *p; p++)
2187 appendStringInfoString(&buf, "&");
2190 appendStringInfoString(&buf, "<");
2193 appendStringInfoString(&buf, ">");
2196 appendStringInfoString(&buf, "
");
2199 appendStringInfoCharMacro(&buf, *p);
2208 _SPI_strdup(const char *s)
2210 size_t len = strlen(s) + 1;
2211 char *ret = SPI_palloc(len);
2213 memcpy(ret, s, len);
2219 * SQL to XML mapping functions
2221 * What follows below was at one point intentionally organized so that
2222 * you can read along in the SQL/XML standard. The functions are
2223 * mostly split up the way the clauses lay out in the standards
2224 * document, and the identifiers are also aligned with the standard
2225 * text. Unfortunately, SQL/XML:2006 reordered the clauses
2226 * differently than SQL/XML:2003, so the order below doesn't make much
2229 * There are many things going on there:
2231 * There are two kinds of mappings: Mapping SQL data (table contents)
2232 * to XML documents, and mapping SQL structure (the "schema") to XML
2233 * Schema. And there are functions that do both at the same time.
2235 * Then you can map a database, a schema, or a table, each in both
2236 * ways. This breaks down recursively: Mapping a database invokes
2237 * mapping schemas, which invokes mapping tables, which invokes
2238 * mapping rows, which invokes mapping columns, although you can't
2239 * call the last two from the outside. Because of this, there are a
2240 * number of xyz_internal() functions which are to be called both from
2241 * the function manager wrapper and from some upper layer in a
2244 * See the documentation about what the common function arguments
2245 * nulls, tableforest, and targetns mean.
2247 * Some style guidelines for XML output: Use double quotes for quoting
2248 * XML attributes. Indent XML elements by two spaces, but remember
2249 * that a lot of code is called recursively at different levels, so
2250 * it's better not to indent rather than create output that indents
2251 * and outdents weirdly. Add newlines to make the output look nice.
2256 * Visibility of objects for XML mappings; see SQL/XML:2008 section
2261 * Given a query, which must return type oid as first column, produce
2262 * a list of Oids with the query results.
2265 query_to_oid_list(const char *query)
2270 SPI_execute(query, true, 0);
2272 for (i = 0; i < SPI_processed; i++)
2277 oid = SPI_getbinval(SPI_tuptable->vals[i],
2278 SPI_tuptable->tupdesc,
2282 list = lappend_oid(list, DatumGetObjectId(oid));
2290 schema_get_xml_visible_tables(Oid nspid)
2292 StringInfoData query;
2294 initStringInfo(&query);
2295 appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class WHERE relnamespace = %u AND relkind IN ('r', 'm', 'v') AND pg_catalog.has_table_privilege (oid, 'SELECT') ORDER BY relname;", nspid);
2297 return query_to_oid_list(query.data);
2302 * Including the system schemas is probably not useful for a database
2305 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
2307 #define XML_VISIBLE_SCHEMAS "SELECT oid FROM pg_catalog.pg_namespace WHERE pg_catalog.has_schema_privilege (oid, 'USAGE') AND NOT " XML_VISIBLE_SCHEMAS_EXCLUDE
2311 database_get_xml_visible_schemas(void)
2313 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2318 database_get_xml_visible_tables(void)
2320 /* At the moment there is no order required here. */
2321 return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class WHERE relkind IN ('r', 'm', 'v') AND pg_catalog.has_table_privilege (pg_class.oid, 'SELECT') AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2326 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2008
2331 table_to_xml_internal(Oid relid,
2332 const char *xmlschema, bool nulls, bool tableforest,
2333 const char *targetns, bool top_level)
2335 StringInfoData query;
2337 initStringInfo(&query);
2338 appendStringInfo(&query, "SELECT * FROM %s",
2339 DatumGetCString(DirectFunctionCall1(regclassout,
2340 ObjectIdGetDatum(relid))));
2341 return query_to_xml_internal(query.data, get_rel_name(relid),
2342 xmlschema, nulls, tableforest,
2343 targetns, top_level);
2348 table_to_xml(PG_FUNCTION_ARGS)
2350 Oid relid = PG_GETARG_OID(0);
2351 bool nulls = PG_GETARG_BOOL(1);
2352 bool tableforest = PG_GETARG_BOOL(2);
2353 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2355 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
2362 query_to_xml(PG_FUNCTION_ARGS)
2364 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2365 bool nulls = PG_GETARG_BOOL(1);
2366 bool tableforest = PG_GETARG_BOOL(2);
2367 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2369 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2370 NULL, nulls, tableforest,
2376 cursor_to_xml(PG_FUNCTION_ARGS)
2378 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
2379 int32 count = PG_GETARG_INT32(1);
2380 bool nulls = PG_GETARG_BOOL(2);
2381 bool tableforest = PG_GETARG_BOOL(3);
2382 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2384 StringInfoData result;
2388 initStringInfo(&result);
2391 portal = SPI_cursor_find(name);
2394 (errcode(ERRCODE_UNDEFINED_CURSOR),
2395 errmsg("cursor \"%s\" does not exist", name)));
2397 SPI_cursor_fetch(portal, true, count);
2398 for (i = 0; i < SPI_processed; i++)
2399 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2400 tableforest, targetns, true);
2404 PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
2409 * Write the start tag of the root element of a data mapping.
2411 * top_level means that this is the very top level of the eventual
2412 * output. For example, when the user calls table_to_xml, then a call
2413 * with a table name to this function is the top level. When the user
2414 * calls database_to_xml, then a call with a schema name to this
2415 * function is not the top level. If top_level is false, then the XML
2416 * namespace declarations are omitted, because they supposedly already
2417 * appeared earlier in the output. Repeating them is not wrong, but
2421 xmldata_root_element_start(StringInfo result, const char *eltname,
2422 const char *xmlschema, const char *targetns,
2425 /* This isn't really wrong but currently makes no sense. */
2426 Assert(top_level || !xmlschema);
2428 appendStringInfo(result, "<%s", eltname);
2431 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2432 if (strlen(targetns) > 0)
2433 appendStringInfo(result, " xmlns=\"%s\"", targetns);
2437 /* FIXME: better targets */
2438 if (strlen(targetns) > 0)
2439 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2441 appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2443 appendStringInfoString(result, ">\n");
2448 xmldata_root_element_end(StringInfo result, const char *eltname)
2450 appendStringInfo(result, "</%s>\n", eltname);
2455 query_to_xml_internal(const char *query, char *tablename,
2456 const char *xmlschema, bool nulls, bool tableforest,
2457 const char *targetns, bool top_level)
2464 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2468 result = makeStringInfo();
2471 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2473 (errcode(ERRCODE_DATA_EXCEPTION),
2474 errmsg("invalid query")));
2478 xmldata_root_element_start(result, xmltn, xmlschema,
2479 targetns, top_level);
2480 appendStringInfoString(result, "\n");
2484 appendStringInfo(result, "%s\n\n", xmlschema);
2486 for (i = 0; i < SPI_processed; i++)
2487 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2488 tableforest, targetns, top_level);
2491 xmldata_root_element_end(result, xmltn);
2500 table_to_xmlschema(PG_FUNCTION_ARGS)
2502 Oid relid = PG_GETARG_OID(0);
2503 bool nulls = PG_GETARG_BOOL(1);
2504 bool tableforest = PG_GETARG_BOOL(2);
2505 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2509 rel = heap_open(relid, AccessShareLock);
2510 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2511 tableforest, targetns);
2512 heap_close(rel, NoLock);
2514 PG_RETURN_XML_P(cstring_to_xmltype(result));
2519 query_to_xmlschema(PG_FUNCTION_ARGS)
2521 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2522 bool nulls = PG_GETARG_BOOL(1);
2523 bool tableforest = PG_GETARG_BOOL(2);
2524 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2531 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2532 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2534 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2535 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2537 result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2539 tableforest, targetns));
2540 SPI_cursor_close(portal);
2543 PG_RETURN_XML_P(cstring_to_xmltype(result));
2548 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2550 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
2551 bool nulls = PG_GETARG_BOOL(1);
2552 bool tableforest = PG_GETARG_BOOL(2);
2553 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2554 const char *xmlschema;
2558 portal = SPI_cursor_find(name);
2561 (errcode(ERRCODE_UNDEFINED_CURSOR),
2562 errmsg("cursor \"%s\" does not exist", name)));
2564 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2566 tableforest, targetns));
2569 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2574 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2576 Oid relid = PG_GETARG_OID(0);
2577 bool nulls = PG_GETARG_BOOL(1);
2578 bool tableforest = PG_GETARG_BOOL(2);
2579 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2581 const char *xmlschema;
2583 rel = heap_open(relid, AccessShareLock);
2584 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2585 tableforest, targetns);
2586 heap_close(rel, NoLock);
2588 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2589 xmlschema, nulls, tableforest,
2595 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2597 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2598 bool nulls = PG_GETARG_BOOL(1);
2599 bool tableforest = PG_GETARG_BOOL(2);
2600 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2602 const char *xmlschema;
2608 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2609 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2611 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2612 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2614 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2615 InvalidOid, nulls, tableforest, targetns));
2616 SPI_cursor_close(portal);
2619 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2620 xmlschema, nulls, tableforest,
2626 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2008
2627 * sections 9.13, 9.14.
2631 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2632 bool tableforest, const char *targetns, bool top_level)
2639 xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2641 result = makeStringInfo();
2643 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2644 appendStringInfoString(result, "\n");
2647 appendStringInfo(result, "%s\n\n", xmlschema);
2651 relid_list = schema_get_xml_visible_tables(nspid);
2655 foreach(cell, relid_list)
2657 Oid relid = lfirst_oid(cell);
2660 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2663 appendStringInfoString(result, subres->data);
2664 appendStringInfoChar(result, '\n');
2670 xmldata_root_element_end(result, xmlsn);
2677 schema_to_xml(PG_FUNCTION_ARGS)
2679 Name name = PG_GETARG_NAME(0);
2680 bool nulls = PG_GETARG_BOOL(1);
2681 bool tableforest = PG_GETARG_BOOL(2);
2682 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2687 schemaname = NameStr(*name);
2688 nspid = LookupExplicitNamespace(schemaname, false);
2690 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2691 nulls, tableforest, targetns, true)));
2696 * Write the start element of the root element of an XML Schema mapping.
2699 xsd_schema_element_start(StringInfo result, const char *targetns)
2701 appendStringInfoString(result,
2703 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2704 if (strlen(targetns) > 0)
2705 appendStringInfo(result,
2707 " targetNamespace=\"%s\"\n"
2708 " elementFormDefault=\"qualified\"",
2710 appendStringInfoString(result,
2716 xsd_schema_element_end(StringInfo result)
2718 appendStringInfoString(result, "</xsd:schema>");
2723 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2724 bool tableforest, const char *targetns)
2732 result = makeStringInfo();
2734 nspid = LookupExplicitNamespace(schemaname, false);
2736 xsd_schema_element_start(result, targetns);
2740 relid_list = schema_get_xml_visible_tables(nspid);
2743 foreach(cell, relid_list)
2747 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2748 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2749 heap_close(rel, NoLock);
2752 appendStringInfoString(result,
2753 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2755 appendStringInfoString(result,
2756 map_sql_schema_to_xmlschema_types(nspid, relid_list,
2757 nulls, tableforest, targetns));
2759 xsd_schema_element_end(result);
2768 schema_to_xmlschema(PG_FUNCTION_ARGS)
2770 Name name = PG_GETARG_NAME(0);
2771 bool nulls = PG_GETARG_BOOL(1);
2772 bool tableforest = PG_GETARG_BOOL(2);
2773 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2775 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2776 nulls, tableforest, targetns)));
2781 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2783 Name name = PG_GETARG_NAME(0);
2784 bool nulls = PG_GETARG_BOOL(1);
2785 bool tableforest = PG_GETARG_BOOL(2);
2786 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2789 StringInfo xmlschema;
2791 schemaname = NameStr(*name);
2792 nspid = LookupExplicitNamespace(schemaname, false);
2794 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2795 tableforest, targetns);
2797 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2798 xmlschema->data, nulls,
2799 tableforest, targetns, true)));
2804 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2008
2805 * sections 9.16, 9.17.
2809 database_to_xml_internal(const char *xmlschema, bool nulls,
2810 bool tableforest, const char *targetns)
2817 xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2819 result = makeStringInfo();
2821 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2822 appendStringInfoString(result, "\n");
2825 appendStringInfo(result, "%s\n\n", xmlschema);
2829 nspid_list = database_get_xml_visible_schemas();
2833 foreach(cell, nspid_list)
2835 Oid nspid = lfirst_oid(cell);
2838 subres = schema_to_xml_internal(nspid, NULL, nulls,
2839 tableforest, targetns, false);
2841 appendStringInfoString(result, subres->data);
2842 appendStringInfoChar(result, '\n');
2848 xmldata_root_element_end(result, xmlcn);
2855 database_to_xml(PG_FUNCTION_ARGS)
2857 bool nulls = PG_GETARG_BOOL(0);
2858 bool tableforest = PG_GETARG_BOOL(1);
2859 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2861 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2862 tableforest, targetns)));
2867 database_to_xmlschema_internal(bool nulls, bool tableforest,
2868 const char *targetns)
2876 result = makeStringInfo();
2878 xsd_schema_element_start(result, targetns);
2882 relid_list = database_get_xml_visible_tables();
2883 nspid_list = database_get_xml_visible_schemas();
2886 foreach(cell, relid_list)
2890 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2891 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2892 heap_close(rel, NoLock);
2895 appendStringInfoString(result,
2896 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2898 appendStringInfoString(result,
2899 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2901 xsd_schema_element_end(result);
2910 database_to_xmlschema(PG_FUNCTION_ARGS)
2912 bool nulls = PG_GETARG_BOOL(0);
2913 bool tableforest = PG_GETARG_BOOL(1);
2914 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2916 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2917 tableforest, targetns)));
2922 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2924 bool nulls = PG_GETARG_BOOL(0);
2925 bool tableforest = PG_GETARG_BOOL(1);
2926 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2927 StringInfo xmlschema;
2929 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2931 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2932 nulls, tableforest, targetns)));
2937 * Map a multi-part SQL name to an XML name; see SQL/XML:2008 section
2941 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2943 StringInfoData result;
2945 initStringInfo(&result);
2948 appendStringInfoString(&result,
2949 map_sql_identifier_to_xml_name(a, true, true));
2951 appendStringInfo(&result, ".%s",
2952 map_sql_identifier_to_xml_name(b, true, true));
2954 appendStringInfo(&result, ".%s",
2955 map_sql_identifier_to_xml_name(c, true, true));
2957 appendStringInfo(&result, ".%s",
2958 map_sql_identifier_to_xml_name(d, true, true));
2965 * Map an SQL table to an XML Schema document; see SQL/XML:2008
2968 * Map an SQL table to XML Schema data types; see SQL/XML:2008 section
2972 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2973 bool tableforest, const char *targetns)
2977 char *tabletypename;
2979 StringInfoData result;
2981 initStringInfo(&result);
2983 if (OidIsValid(relid))
2986 Form_pg_class reltuple;
2988 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
2989 if (!HeapTupleIsValid(tuple))
2990 elog(ERROR, "cache lookup failed for relation %u", relid);
2991 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2993 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2996 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2997 get_database_name(MyDatabaseId),
2998 get_namespace_name(reltuple->relnamespace),
2999 NameStr(reltuple->relname));
3001 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
3002 get_database_name(MyDatabaseId),
3003 get_namespace_name(reltuple->relnamespace),
3004 NameStr(reltuple->relname));
3006 ReleaseSysCache(tuple);
3015 tabletypename = "TableType";
3016 rowtypename = "RowType";
3019 xsd_schema_element_start(&result, targetns);
3021 appendStringInfoString(&result,
3022 map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
3024 appendStringInfo(&result,
3025 "<xsd:complexType name=\"%s\">\n"
3026 " <xsd:sequence>\n",
3029 for (i = 0; i < tupdesc->natts; i++)
3031 if (tupdesc->attrs[i]->attisdropped)
3033 appendStringInfo(&result,
3034 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3035 map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
3037 map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
3038 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3041 appendStringInfoString(&result,
3042 " </xsd:sequence>\n"
3043 "</xsd:complexType>\n\n");
3047 appendStringInfo(&result,
3048 "<xsd:complexType name=\"%s\">\n"
3050 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3051 " </xsd:sequence>\n"
3052 "</xsd:complexType>\n\n",
3053 tabletypename, rowtypename);
3055 appendStringInfo(&result,
3056 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3057 xmltn, tabletypename);
3060 appendStringInfo(&result,
3061 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3062 xmltn, rowtypename);
3064 xsd_schema_element_end(&result);
3071 * Map an SQL schema to XML Schema data types; see SQL/XML:2008
3075 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
3076 bool tableforest, const char *targetns)
3081 char *schematypename;
3082 StringInfoData result;
3085 dbname = get_database_name(MyDatabaseId);
3086 nspname = get_namespace_name(nspid);
3088 initStringInfo(&result);
3090 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3092 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3097 appendStringInfo(&result,
3098 "<xsd:complexType name=\"%s\">\n", schematypename);
3100 appendStringInfoString(&result,
3103 appendStringInfoString(&result,
3104 " <xsd:sequence>\n");
3106 foreach(cell, relid_list)
3108 Oid relid = lfirst_oid(cell);
3109 char *relname = get_rel_name(relid);
3110 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3111 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3117 appendStringInfo(&result,
3118 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3119 xmltn, tabletypename);
3121 appendStringInfo(&result,
3122 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3123 xmltn, tabletypename);
3127 appendStringInfoString(&result,
3130 appendStringInfoString(&result,
3131 " </xsd:sequence>\n");
3132 appendStringInfoString(&result,
3133 "</xsd:complexType>\n\n");
3135 appendStringInfo(&result,
3136 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3137 xmlsn, schematypename);
3144 * Map an SQL catalog to XML Schema data types; see SQL/XML:2008
3148 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
3149 bool tableforest, const char *targetns)
3153 char *catalogtypename;
3154 StringInfoData result;
3157 dbname = get_database_name(MyDatabaseId);
3159 initStringInfo(&result);
3161 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3163 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3168 appendStringInfo(&result,
3169 "<xsd:complexType name=\"%s\">\n", catalogtypename);
3170 appendStringInfoString(&result,
3173 foreach(cell, nspid_list)
3175 Oid nspid = lfirst_oid(cell);
3176 char *nspname = get_namespace_name(nspid);
3177 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3178 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3183 appendStringInfo(&result,
3184 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3185 xmlsn, schematypename);
3188 appendStringInfoString(&result,
3190 appendStringInfoString(&result,
3191 "</xsd:complexType>\n\n");
3193 appendStringInfo(&result,
3194 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3195 xmlcn, catalogtypename);
3202 * Map an SQL data type to an XML name; see SQL/XML:2008 section 9.4.
3205 map_sql_type_to_xml_name(Oid typeoid, int typmod)
3207 StringInfoData result;
3209 initStringInfo(&result);
3215 appendStringInfoString(&result, "CHAR");
3217 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3221 appendStringInfoString(&result, "VARCHAR");
3223 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3227 appendStringInfoString(&result, "NUMERIC");
3229 appendStringInfo(&result, "NUMERIC_%d_%d",
3230 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3231 (typmod - VARHDRSZ) & 0xffff);
3234 appendStringInfoString(&result, "INTEGER");
3237 appendStringInfoString(&result, "SMALLINT");
3240 appendStringInfoString(&result, "BIGINT");
3243 appendStringInfoString(&result, "REAL");
3246 appendStringInfoString(&result, "DOUBLE");
3249 appendStringInfoString(&result, "BOOLEAN");
3253 appendStringInfoString(&result, "TIME");
3255 appendStringInfo(&result, "TIME_%d", typmod);
3259 appendStringInfoString(&result, "TIME_WTZ");
3261 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3265 appendStringInfoString(&result, "TIMESTAMP");
3267 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3269 case TIMESTAMPTZOID:
3271 appendStringInfoString(&result, "TIMESTAMP_WTZ");
3273 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3276 appendStringInfoString(&result, "DATE");
3279 appendStringInfoString(&result, "XML");
3284 Form_pg_type typtuple;
3286 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3287 if (!HeapTupleIsValid(tuple))
3288 elog(ERROR, "cache lookup failed for type %u", typeoid);
3289 typtuple = (Form_pg_type) GETSTRUCT(tuple);
3291 appendStringInfoString(&result,
3292 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3293 get_database_name(MyDatabaseId),
3294 get_namespace_name(typtuple->typnamespace),
3295 NameStr(typtuple->typname)));
3297 ReleaseSysCache(tuple);
3306 * Map a collection of SQL data types to XML Schema data types; see
3307 * SQL/XML:2008 section 9.7.
3310 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
3312 List *uniquetypes = NIL;
3314 StringInfoData result;
3317 /* extract all column types used in the set of TupleDescs */
3318 foreach(cell0, tupdesc_list)
3320 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3322 for (i = 0; i < tupdesc->natts; i++)
3324 if (tupdesc->attrs[i]->attisdropped)
3326 uniquetypes = list_append_unique_oid(uniquetypes,
3327 tupdesc->attrs[i]->atttypid);
3331 /* add base types of domains */
3332 foreach(cell0, uniquetypes)
3334 Oid typid = lfirst_oid(cell0);
3335 Oid basetypid = getBaseType(typid);
3337 if (basetypid != typid)
3338 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
3341 /* Convert to textual form */
3342 initStringInfo(&result);
3344 foreach(cell0, uniquetypes)
3346 appendStringInfo(&result, "%s\n",
3347 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
3356 * Map an SQL data type to a named XML Schema data type; see
3357 * SQL/XML:2008 sections 9.5 and 9.6.
3359 * (The distinction between 9.5 and 9.6 is basically that 9.6 adds
3360 * a name attribute, which this function does. The name-less version
3361 * 9.5 doesn't appear to be required anywhere.)
3364 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
3366 StringInfoData result;
3367 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3369 initStringInfo(&result);
3371 if (typeoid == XMLOID)
3373 appendStringInfoString(&result,
3374 "<xsd:complexType mixed=\"true\">\n"
3376 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3377 " </xsd:sequence>\n"
3378 "</xsd:complexType>\n");
3382 appendStringInfo(&result,
3383 "<xsd:simpleType name=\"%s\">\n", typename);
3390 appendStringInfo(&result,
3391 " <xsd:restriction base=\"xsd:string\">\n");
3393 appendStringInfo(&result,
3394 " <xsd:maxLength value=\"%d\"/>\n",
3396 appendStringInfoString(&result, " </xsd:restriction>\n");
3400 appendStringInfo(&result,
3401 " <xsd:restriction base=\"xsd:%s\">\n"
3402 " </xsd:restriction>\n",
3403 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3408 appendStringInfo(&result,
3409 " <xsd:restriction base=\"xsd:decimal\">\n"
3410 " <xsd:totalDigits value=\"%d\"/>\n"
3411 " <xsd:fractionDigits value=\"%d\"/>\n"
3412 " </xsd:restriction>\n",
3413 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3414 (typmod - VARHDRSZ) & 0xffff);
3418 appendStringInfo(&result,
3419 " <xsd:restriction base=\"xsd:short\">\n"
3420 " <xsd:maxInclusive value=\"%d\"/>\n"
3421 " <xsd:minInclusive value=\"%d\"/>\n"
3422 " </xsd:restriction>\n",
3423 SHRT_MAX, SHRT_MIN);
3427 appendStringInfo(&result,
3428 " <xsd:restriction base=\"xsd:int\">\n"
3429 " <xsd:maxInclusive value=\"%d\"/>\n"
3430 " <xsd:minInclusive value=\"%d\"/>\n"
3431 " </xsd:restriction>\n",
3436 appendStringInfo(&result,
3437 " <xsd:restriction base=\"xsd:long\">\n"
3438 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3439 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3440 " </xsd:restriction>\n",
3441 (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3442 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3446 appendStringInfoString(&result,
3447 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3451 appendStringInfoString(&result,
3452 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3456 appendStringInfoString(&result,
3457 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3463 const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3466 appendStringInfo(&result,
3467 " <xsd:restriction base=\"xsd:time\">\n"
3468 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3469 " </xsd:restriction>\n", tz);
3470 else if (typmod == 0)
3471 appendStringInfo(&result,
3472 " <xsd:restriction base=\"xsd:time\">\n"
3473 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3474 " </xsd:restriction>\n", tz);
3476 appendStringInfo(&result,
3477 " <xsd:restriction base=\"xsd:time\">\n"
3478 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3479 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3484 case TIMESTAMPTZOID:
3486 const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3489 appendStringInfo(&result,
3490 " <xsd:restriction base=\"xsd:dateTime\">\n"
3491 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3492 " </xsd:restriction>\n", tz);
3493 else if (typmod == 0)
3494 appendStringInfo(&result,
3495 " <xsd:restriction base=\"xsd:dateTime\">\n"
3496 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3497 " </xsd:restriction>\n", tz);
3499 appendStringInfo(&result,
3500 " <xsd:restriction base=\"xsd:dateTime\">\n"
3501 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3502 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3507 appendStringInfoString(&result,
3508 " <xsd:restriction base=\"xsd:date\">\n"
3509 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3510 " </xsd:restriction>\n");
3514 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3517 int32 base_typmod = -1;
3519 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3521 appendStringInfo(&result,
3522 " <xsd:restriction base=\"%s\"/>\n",
3523 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3527 appendStringInfoString(&result, "</xsd:simpleType>\n");
3535 * Map an SQL row to an XML element, taking the row from the active
3536 * SPI cursor. See also SQL/XML:2008 section 9.10.
3539 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3540 bool nulls, bool tableforest,
3541 const char *targetns, bool top_level)
3547 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3557 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3559 appendStringInfoString(result, "<row>\n");
3561 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3567 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3569 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3570 SPI_tuptable->tupdesc,
3576 appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3579 appendStringInfo(result, " <%s>%s</%s>\n",
3581 map_sql_value_to_xml_value(colval,
3582 SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
3588 xmldata_root_element_end(result, xmltn);
3589 appendStringInfoChar(result, '\n');
3592 appendStringInfoString(result, "</row>\n\n");
3597 * XPath related functions
3603 * Convert XML node to text (dump subtree in case of element,
3604 * return value otherwise)
3607 xml_xmlnodetoxmltype(xmlNodePtr cur)
3611 if (cur->type == XML_ELEMENT_NODE)
3615 buf = xmlBufferCreate();
3618 xmlNodeDump(buf, NULL, cur, 0, 1);
3619 result = xmlBuffer_to_xmltype(buf);
3633 str = xmlXPathCastNodeToString(cur);
3636 /* Here we rely on XML having the same representation as TEXT */
3637 char *escaped = escape_xml((char *) str);
3639 result = (xmltype *) cstring_to_text(escaped);
3655 * Convert an XML XPath object (the result of evaluating an XPath expression)
3656 * to an array of xml values, which is returned at *astate. The function
3657 * result value is the number of elements in the array.
3659 * If "astate" is NULL then we don't generate the array value, but we still
3660 * return the number of elements it would have had.
3662 * Nodesets are converted to an array containing the nodes' textual
3663 * representations. Primitive values (float, double, string) are converted
3664 * to a single-element array containing the value's string representation.
3667 xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
3668 ArrayBuildState **astate)
3678 switch (xpathobj->type)
3681 if (xpathobj->nodesetval != NULL)
3683 result = xpathobj->nodesetval->nodeNr;
3688 for (i = 0; i < result; i++)
3690 datum = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3691 *astate = accumArrayResult(*astate, datum,
3693 CurrentMemoryContext);
3702 datum = BoolGetDatum(xpathobj->boolval);
3703 datumtype = BOOLOID;
3709 datum = Float8GetDatum(xpathobj->floatval);
3710 datumtype = FLOAT8OID;
3716 datum = CStringGetDatum((char *) xpathobj->stringval);
3717 datumtype = CSTRINGOID;
3721 elog(ERROR, "xpath expression result type %d is unsupported",
3723 return 0; /* keep compiler quiet */
3726 /* Common code for scalar-value cases */
3727 result_str = map_sql_value_to_xml_value(datum, datumtype, true);
3728 datum = PointerGetDatum(cstring_to_xmltype(result_str));
3729 *astate = accumArrayResult(*astate, datum,
3731 CurrentMemoryContext);
3737 * Common code for xpath() and xmlexists()
3739 * Evaluate XPath expression and return number of nodes in res_items
3740 * and array of XML values in astate. Either of those pointers can be
3741 * NULL if the corresponding result isn't wanted.
3743 * It is up to the user to ensure that the XML passed is in fact
3744 * an XML document - XPath doesn't work easily on fragments without
3745 * a context node being known.
3748 xpath_internal(text *xpath_expr_text, xmltype *data, ArrayType *namespaces,
3749 int *res_nitems, ArrayBuildState **astate)
3751 PgXmlErrorContext *xmlerrcxt;
3752 volatile xmlParserCtxtPtr ctxt = NULL;
3753 volatile xmlDocPtr doc = NULL;
3754 volatile xmlXPathContextPtr xpathctx = NULL;
3755 volatile xmlXPathCompExprPtr xpathcomp = NULL;
3756 volatile xmlXPathObjectPtr xpathobj = NULL;
3761 xmlChar *xpath_expr;
3764 Datum *ns_names_uris;
3765 bool *ns_names_uris_nulls;
3769 * Namespace mappings are passed as text[]. If an empty array is passed
3770 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
3771 * Else, a 2-dimensional array with length of the second axis being equal
3772 * to 2 should be passed, i.e., every subarray contains 2 elements, the
3773 * first element defining the name, the second one the URI. Example:
3774 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
3775 * 'http://example2.com']].
3777 ndim = namespaces ? ARR_NDIM(namespaces) : 0;
3782 dims = ARR_DIMS(namespaces);
3784 if (ndim != 2 || dims[1] != 2)
3786 (errcode(ERRCODE_DATA_EXCEPTION),
3787 errmsg("invalid array for XML namespace mapping"),
3788 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3790 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3792 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3793 &ns_names_uris, &ns_names_uris_nulls,
3796 Assert((ns_count % 2) == 0); /* checked above */
3797 ns_count /= 2; /* count pairs only */
3801 ns_names_uris = NULL;
3802 ns_names_uris_nulls = NULL;
3806 datastr = VARDATA(data);
3807 len = VARSIZE(data) - VARHDRSZ;
3808 xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3811 (errcode(ERRCODE_DATA_EXCEPTION),
3812 errmsg("empty XPath expression")));
3814 string = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
3815 memcpy(string, datastr, len);
3818 xpath_expr = (xmlChar *) palloc((xpath_len + 1) * sizeof(xmlChar));
3819 memcpy(xpath_expr, VARDATA(xpath_expr_text), xpath_len);
3820 xpath_expr[xpath_len] = '\0';
3822 xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
3829 * redundant XML parsing (two parsings for the same value during one
3830 * command execution are possible)
3832 ctxt = xmlNewParserCtxt();
3833 if (ctxt == NULL || xmlerrcxt->err_occurred)
3834 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
3835 "could not allocate parser context");
3836 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3837 if (doc == NULL || xmlerrcxt->err_occurred)
3838 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3839 "could not parse XML document");
3840 xpathctx = xmlXPathNewContext(doc);
3841 if (xpathctx == NULL || xmlerrcxt->err_occurred)
3842 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
3843 "could not allocate XPath context");
3844 xpathctx->node = xmlDocGetRootElement(doc);
3845 if (xpathctx->node == NULL || xmlerrcxt->err_occurred)
3846 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
3847 "could not find root XML element");
3849 /* register namespaces, if any */
3852 for (i = 0; i < ns_count; i++)
3857 if (ns_names_uris_nulls[i * 2] ||
3858 ns_names_uris_nulls[i * 2 + 1])
3860 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3861 errmsg("neither namespace name nor URI may be null")));
3862 ns_name = TextDatumGetCString(ns_names_uris[i * 2]);
3863 ns_uri = TextDatumGetCString(ns_names_uris[i * 2 + 1]);
3864 if (xmlXPathRegisterNs(xpathctx,
3865 (xmlChar *) ns_name,
3866 (xmlChar *) ns_uri) != 0)
3867 ereport(ERROR, /* is this an internal error??? */
3868 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3873 xpathcomp = xmlXPathCompile(xpath_expr);
3874 if (xpathcomp == NULL || xmlerrcxt->err_occurred)
3875 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
3876 "invalid XPath expression");
3879 * Version 2.6.27 introduces a function named
3880 * xmlXPathCompiledEvalToBoolean, which would be enough for xmlexists,
3881 * but we can derive the existence by whether any nodes are returned,
3882 * thereby preventing a library version upgrade and keeping the code
3885 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3886 if (xpathobj == NULL || xmlerrcxt->err_occurred)
3887 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
3888 "could not create XPath object");
3891 * Extract the results as requested.
3893 if (res_nitems != NULL)
3894 *res_nitems = xml_xpathobjtoxmlarray(xpathobj, astate);
3896 (void) xml_xpathobjtoxmlarray(xpathobj, astate);
3901 xmlXPathFreeObject(xpathobj);
3903 xmlXPathFreeCompExpr(xpathcomp);
3905 xmlXPathFreeContext(xpathctx);
3909 xmlFreeParserCtxt(ctxt);
3911 pg_xml_done(xmlerrcxt, true);
3917 xmlXPathFreeObject(xpathobj);
3918 xmlXPathFreeCompExpr(xpathcomp);
3919 xmlXPathFreeContext(xpathctx);
3921 xmlFreeParserCtxt(ctxt);
3923 pg_xml_done(xmlerrcxt, false);
3925 #endif /* USE_LIBXML */
3928 * Evaluate XPath expression and return array of XML values.
3930 * As we have no support of XQuery sequences yet, this function seems
3931 * to be the most useful one (array of XML functions plays a role of
3932 * some kind of substitution for XQuery sequences).
3935 xpath(PG_FUNCTION_ARGS)
3938 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3939 xmltype *data = PG_GETARG_XML_P(1);
3940 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3942 ArrayBuildState *astate;
3944 xpath_internal(xpath_expr_text, data, namespaces,
3945 &res_nitems, &astate);
3947 if (res_nitems == 0)
3948 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3950 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
3958 * Determines if the node specified by the supplied XPath exists
3959 * in a given XML document, returning a boolean.
3962 xmlexists(PG_FUNCTION_ARGS)
3965 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3966 xmltype *data = PG_GETARG_XML_P(1);
3969 xpath_internal(xpath_expr_text, data, NULL,
3972 PG_RETURN_BOOL(res_nitems > 0);
3980 * Determines if the node specified by the supplied XPath exists
3981 * in a given XML document, returning a boolean. Differs from
3982 * xmlexists as it supports namespaces and is not defined in SQL/XML.
3985 xpath_exists(PG_FUNCTION_ARGS)
3988 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3989 xmltype *data = PG_GETARG_XML_P(1);
3990 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3993 xpath_internal(xpath_expr_text, data, namespaces,
3996 PG_RETURN_BOOL(res_nitems > 0);
4004 * Functions for checking well-formed-ness
4009 wellformed_xml(text *data, XmlOptionType xmloption_arg)
4012 volatile xmlDocPtr doc = NULL;
4014 /* We want to catch any exceptions and return false */
4017 doc = xml_parse(data, xmloption_arg, true, GetDatabaseEncoding());
4035 xml_is_well_formed(PG_FUNCTION_ARGS)
4038 text *data = PG_GETARG_TEXT_P(0);
4040 PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4044 #endif /* not USE_LIBXML */
4048 xml_is_well_formed_document(PG_FUNCTION_ARGS)
4051 text *data = PG_GETARG_TEXT_P(0);
4053 PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4057 #endif /* not USE_LIBXML */
4061 xml_is_well_formed_content(PG_FUNCTION_ARGS)
4064 text *data = PG_GETARG_TEXT_P(0);
4066 PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4070 #endif /* not USE_LIBXML */