1 /*-------------------------------------------------------------------------
4 * XML data type support.
7 * Portions Copyright (c) 1996-2013, 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 appendStringInfo(&buf, "<!--");
446 appendStringInfoText(&buf, arg);
447 appendStringInfo(&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:
1613 * Suppress warnings about undeclared entities. We need to do
1614 * this to avoid problems due to not loading DTD definitions.
1616 if (error->code == XML_WAR_UNDECLARED_ENTITY)
1619 /* Otherwise, accept error regardless of the parsing purpose */
1623 /* Ignore error if only doing well-formedness check */
1624 if (xmlerrcxt->strictness == PG_XML_STRICTNESS_WELLFORMED)
1629 /* Prepare error message in errorBuf */
1630 errorBuf = makeStringInfo();
1632 if (error->line > 0)
1633 appendStringInfo(errorBuf, "line %d: ", error->line);
1635 appendStringInfo(errorBuf, "element %s: ", name);
1636 appendStringInfoString(errorBuf, error->message);
1639 * Append context information to errorBuf.
1641 * xmlParserPrintFileContext() uses libxml's "generic" error handler to
1642 * write the context. Since we don't want to duplicate libxml
1643 * functionality here, we set up a generic error handler temporarily.
1645 * We use appendStringInfo() directly as libxml's generic error handler.
1646 * This should work because it has essentially the same signature as
1647 * libxml expects, namely (void *ptr, const char *msg, ...).
1651 xmlGenericErrorFunc errFuncSaved = xmlGenericError;
1652 void *errCtxSaved = xmlGenericErrorContext;
1654 xmlSetGenericErrorFunc((void *) errorBuf,
1655 (xmlGenericErrorFunc) appendStringInfo);
1657 /* Add context information to errorBuf */
1658 appendStringInfoLineSeparator(errorBuf);
1660 xmlParserPrintFileContext(input);
1662 /* Restore generic error func */
1663 xmlSetGenericErrorFunc(errCtxSaved, errFuncSaved);
1666 /* Get rid of any trailing newlines in errorBuf */
1667 chopStringInfoNewlines(errorBuf);
1670 * Legacy error handling mode. err_occurred is never set, we just add the
1671 * message to err_buf. This mode exists because the xml2 contrib module
1672 * uses our error-handling infrastructure, but we don't want to change its
1673 * behaviour since it's deprecated anyway. This is also why we don't
1674 * distinguish between notices, warnings and errors here --- the old-style
1675 * generic error handler wouldn't have done that either.
1677 if (xmlerrcxt->strictness == PG_XML_STRICTNESS_LEGACY)
1679 appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
1680 appendStringInfoString(&xmlerrcxt->err_buf, errorBuf->data);
1682 pfree(errorBuf->data);
1688 * We don't want to ereport() here because that'd probably leave libxml in
1689 * an inconsistent state. Instead, we remember the error and ereport()
1690 * from xml_ereport().
1692 * Warnings and notices can be reported immediately since they won't cause
1693 * a longjmp() out of libxml.
1695 if (level >= XML_ERR_ERROR)
1697 appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
1698 appendStringInfoString(&xmlerrcxt->err_buf, errorBuf->data);
1700 xmlerrcxt->err_occurred = true;
1702 else if (level >= XML_ERR_WARNING)
1705 (errmsg_internal("%s", errorBuf->data)));
1710 (errmsg_internal("%s", errorBuf->data)));
1713 pfree(errorBuf->data);
1719 * Wrapper for "ereport" function for XML-related errors. The "msg"
1720 * is the SQL-level message; some can be adopted from the SQL/XML
1721 * standard. This function uses "code" to create a textual detail
1722 * message. At the moment, we only need to cover those codes that we
1723 * may raise in this file.
1726 xml_ereport_by_code(int level, int sqlcode,
1727 const char *msg, int code)
1733 case XML_ERR_INVALID_CHAR:
1734 det = gettext_noop("Invalid character value.");
1736 case XML_ERR_SPACE_REQUIRED:
1737 det = gettext_noop("Space required.");
1739 case XML_ERR_STANDALONE_VALUE:
1740 det = gettext_noop("standalone accepts only 'yes' or 'no'.");
1742 case XML_ERR_VERSION_MISSING:
1743 det = gettext_noop("Malformed declaration: missing version.");
1745 case XML_ERR_MISSING_ENCODING:
1746 det = gettext_noop("Missing encoding in text declaration.");
1748 case XML_ERR_XMLDECL_NOT_FINISHED:
1749 det = gettext_noop("Parsing XML declaration: '?>' expected.");
1752 det = gettext_noop("Unrecognized libxml error code: %d.");
1758 errmsg_internal("%s", msg),
1759 errdetail(det, code)));
1764 * Remove all trailing newlines from a StringInfo string
1767 chopStringInfoNewlines(StringInfo str)
1769 while (str->len > 0 && str->data[str->len - 1] == '\n')
1770 str->data[--str->len] = '\0';
1775 * Append a newline after removing any existing trailing newlines
1778 appendStringInfoLineSeparator(StringInfo str)
1780 chopStringInfoNewlines(str);
1782 appendStringInfoChar(str, '\n');
1787 * Convert one char in the current server encoding to a Unicode codepoint.
1790 sqlchar_to_unicode(char *s)
1793 pg_wchar ret[2]; /* need space for trailing zero */
1795 utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1797 GetDatabaseEncoding(),
1800 pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret,
1801 pg_encoding_mblen(PG_UTF8, utf8string));
1803 if (utf8string != s)
1811 is_valid_xml_namefirst(pg_wchar c)
1813 /* (Letter | '_' | ':') */
1814 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1815 || c == '_' || c == ':');
1820 is_valid_xml_namechar(pg_wchar c)
1822 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1823 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1825 || c == '.' || c == '-' || c == '_' || c == ':'
1826 || xmlIsCombiningQ(c)
1827 || xmlIsExtenderQ(c));
1829 #endif /* USE_LIBXML */
1833 * Map SQL identifier to XML name; see SQL/XML:2008 section 9.1.
1836 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1844 * SQL/XML doesn't make use of this case anywhere, so it's probably a
1847 Assert(fully_escaped || !escape_period);
1849 initStringInfo(&buf);
1851 for (p = ident; *p; p += pg_mblen(p))
1853 if (*p == ':' && (p == ident || fully_escaped))
1854 appendStringInfo(&buf, "_x003A_");
1855 else if (*p == '_' && *(p + 1) == 'x')
1856 appendStringInfo(&buf, "_x005F_");
1857 else if (fully_escaped && p == ident &&
1858 pg_strncasecmp(p, "xml", 3) == 0)
1861 appendStringInfo(&buf, "_x0078_");
1863 appendStringInfo(&buf, "_x0058_");
1865 else if (escape_period && *p == '.')
1866 appendStringInfo(&buf, "_x002E_");
1869 pg_wchar u = sqlchar_to_unicode(p);
1872 ? !is_valid_xml_namefirst(u)
1873 : !is_valid_xml_namechar(u))
1874 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1876 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1881 #else /* not USE_LIBXML */
1884 #endif /* not USE_LIBXML */
1889 * Map a Unicode codepoint into the current server encoding.
1892 unicode_to_sqlchar(pg_wchar c)
1894 unsigned char utf8string[5]; /* need room for trailing zero */
1897 memset(utf8string, 0, sizeof(utf8string));
1898 unicode_to_utf8(c, utf8string);
1900 result = (char *) pg_do_encoding_conversion(utf8string,
1901 pg_encoding_mblen(PG_UTF8,
1902 (char *) utf8string),
1904 GetDatabaseEncoding());
1905 /* if pg_do_encoding_conversion didn't strdup, we must */
1906 if (result == (char *) utf8string)
1907 result = pstrdup(result);
1913 * Map XML name to SQL identifier; see SQL/XML:2008 section 9.3.
1916 map_xml_name_to_sql_identifier(char *name)
1921 initStringInfo(&buf);
1923 for (p = name; *p; p += pg_mblen(p))
1925 if (*p == '_' && *(p + 1) == 'x'
1926 && isxdigit((unsigned char) *(p + 2))
1927 && isxdigit((unsigned char) *(p + 3))
1928 && isxdigit((unsigned char) *(p + 4))
1929 && isxdigit((unsigned char) *(p + 5))
1934 sscanf(p + 2, "%X", &u);
1935 appendStringInfoString(&buf, unicode_to_sqlchar(u));
1939 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1946 * Map SQL value to XML value; see SQL/XML:2008 section 9.8.
1948 * When xml_escape_strings is true, then certain characters in string
1949 * values are replaced by entity references (< etc.), as specified
1950 * in SQL/XML:2008 section 9.8 GR 9) a) iii). This is normally what is
1951 * wanted. The false case is mainly useful when the resulting value
1952 * is used with xmlTextWriterWriteAttribute() to write out an
1953 * attribute, because that function does the escaping itself.
1956 map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
1958 if (type_is_array_domain(type))
1971 array = DatumGetArrayTypeP(value);
1972 elmtype = ARR_ELEMTYPE(array);
1973 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1975 deconstruct_array(array, elmtype,
1976 elmlen, elmbyval, elmalign,
1977 &elem_values, &elem_nulls,
1980 initStringInfo(&buf);
1982 for (i = 0; i < num_elems; i++)
1986 appendStringInfoString(&buf, "<element>");
1987 appendStringInfoString(&buf,
1988 map_sql_value_to_xml_value(elem_values[i],
1990 appendStringInfoString(&buf, "</element>");
2005 * Special XSD formatting for some data types
2010 if (DatumGetBool(value))
2019 char buf[MAXDATELEN + 1];
2021 date = DatumGetDateADT(value);
2022 /* XSD doesn't support infinite values */
2023 if (DATE_NOT_FINITE(date))
2025 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2026 errmsg("date out of range"),
2027 errdetail("XML does not support infinite date values.")));
2028 j2date(date + POSTGRES_EPOCH_JDATE,
2029 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2030 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
2032 return pstrdup(buf);
2037 Timestamp timestamp;
2040 char buf[MAXDATELEN + 1];
2042 timestamp = DatumGetTimestamp(value);
2044 /* XSD doesn't support infinite values */
2045 if (TIMESTAMP_NOT_FINITE(timestamp))
2047 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2048 errmsg("timestamp out of range"),
2049 errdetail("XML does not support infinite timestamp values.")));
2050 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2051 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2054 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2055 errmsg("timestamp out of range")));
2057 return pstrdup(buf);
2060 case TIMESTAMPTZOID:
2062 TimestampTz timestamp;
2066 const char *tzn = NULL;
2067 char buf[MAXDATELEN + 1];
2069 timestamp = DatumGetTimestamp(value);
2071 /* XSD doesn't support infinite values */
2072 if (TIMESTAMP_NOT_FINITE(timestamp))
2074 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2075 errmsg("timestamp out of range"),
2076 errdetail("XML does not support infinite timestamp values.")));
2077 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2078 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2081 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2082 errmsg("timestamp out of range")));
2084 return pstrdup(buf);
2090 bytea *bstr = DatumGetByteaPP(value);
2091 PgXmlErrorContext *xmlerrcxt;
2092 volatile xmlBufferPtr buf = NULL;
2093 volatile xmlTextWriterPtr writer = NULL;
2096 xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2100 buf = xmlBufferCreate();
2101 if (buf == NULL || xmlerrcxt->err_occurred)
2102 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2103 "could not allocate xmlBuffer");
2104 writer = xmlNewTextWriterMemory(buf, 0);
2105 if (writer == NULL || xmlerrcxt->err_occurred)
2106 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2107 "could not allocate xmlTextWriter");
2109 if (xmlbinary == XMLBINARY_BASE64)
2110 xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2111 0, VARSIZE_ANY_EXHDR(bstr));
2113 xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2114 0, VARSIZE_ANY_EXHDR(bstr));
2116 /* we MUST do this now to flush data out to the buffer */
2117 xmlFreeTextWriter(writer);
2120 result = pstrdup((const char *) xmlBufferContent(buf));
2125 xmlFreeTextWriter(writer);
2129 pg_xml_done(xmlerrcxt, true);
2137 pg_xml_done(xmlerrcxt, false);
2141 #endif /* USE_LIBXML */
2146 * otherwise, just use the type's native text representation
2148 getTypeOutputInfo(type, &typeOut, &isvarlena);
2149 str = OidOutputFunctionCall(typeOut, value);
2151 /* ... exactly as-is for XML, and when escaping is not wanted */
2152 if (type == XMLOID || !xml_escape_strings)
2155 /* otherwise, translate special characters as needed */
2156 return escape_xml(str);
2162 * Escape characters in text that have special meanings in XML.
2164 * Returns a palloc'd string.
2166 * NB: this is intentionally not dependent on libxml.
2169 escape_xml(const char *str)
2174 initStringInfo(&buf);
2175 for (p = str; *p; p++)
2180 appendStringInfoString(&buf, "&");
2183 appendStringInfoString(&buf, "<");
2186 appendStringInfoString(&buf, ">");
2189 appendStringInfoString(&buf, "
");
2192 appendStringInfoCharMacro(&buf, *p);
2201 _SPI_strdup(const char *s)
2203 size_t len = strlen(s) + 1;
2204 char *ret = SPI_palloc(len);
2206 memcpy(ret, s, len);
2212 * SQL to XML mapping functions
2214 * What follows below was at one point intentionally organized so that
2215 * you can read along in the SQL/XML standard. The functions are
2216 * mostly split up the way the clauses lay out in the standards
2217 * document, and the identifiers are also aligned with the standard
2218 * text. Unfortunately, SQL/XML:2006 reordered the clauses
2219 * differently than SQL/XML:2003, so the order below doesn't make much
2222 * There are many things going on there:
2224 * There are two kinds of mappings: Mapping SQL data (table contents)
2225 * to XML documents, and mapping SQL structure (the "schema") to XML
2226 * Schema. And there are functions that do both at the same time.
2228 * Then you can map a database, a schema, or a table, each in both
2229 * ways. This breaks down recursively: Mapping a database invokes
2230 * mapping schemas, which invokes mapping tables, which invokes
2231 * mapping rows, which invokes mapping columns, although you can't
2232 * call the last two from the outside. Because of this, there are a
2233 * number of xyz_internal() functions which are to be called both from
2234 * the function manager wrapper and from some upper layer in a
2237 * See the documentation about what the common function arguments
2238 * nulls, tableforest, and targetns mean.
2240 * Some style guidelines for XML output: Use double quotes for quoting
2241 * XML attributes. Indent XML elements by two spaces, but remember
2242 * that a lot of code is called recursively at different levels, so
2243 * it's better not to indent rather than create output that indents
2244 * and outdents weirdly. Add newlines to make the output look nice.
2249 * Visibility of objects for XML mappings; see SQL/XML:2008 section
2254 * Given a query, which must return type oid as first column, produce
2255 * a list of Oids with the query results.
2258 query_to_oid_list(const char *query)
2263 SPI_execute(query, true, 0);
2265 for (i = 0; i < SPI_processed; i++)
2270 oid = SPI_getbinval(SPI_tuptable->vals[i],
2271 SPI_tuptable->tupdesc,
2275 list = lappend_oid(list, DatumGetObjectId(oid));
2283 schema_get_xml_visible_tables(Oid nspid)
2285 StringInfoData query;
2287 initStringInfo(&query);
2288 appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class WHERE relnamespace = %u AND relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (oid, 'SELECT') ORDER BY relname;", nspid);
2290 return query_to_oid_list(query.data);
2295 * Including the system schemas is probably not useful for a database
2298 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
2300 #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
2304 database_get_xml_visible_schemas(void)
2306 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2311 database_get_xml_visible_tables(void)
2313 /* At the moment there is no order required here. */
2314 return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class WHERE relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (pg_class.oid, 'SELECT') AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2319 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2008
2324 table_to_xml_internal(Oid relid,
2325 const char *xmlschema, bool nulls, bool tableforest,
2326 const char *targetns, bool top_level)
2328 StringInfoData query;
2330 initStringInfo(&query);
2331 appendStringInfo(&query, "SELECT * FROM %s",
2332 DatumGetCString(DirectFunctionCall1(regclassout,
2333 ObjectIdGetDatum(relid))));
2334 return query_to_xml_internal(query.data, get_rel_name(relid),
2335 xmlschema, nulls, tableforest,
2336 targetns, top_level);
2341 table_to_xml(PG_FUNCTION_ARGS)
2343 Oid relid = PG_GETARG_OID(0);
2344 bool nulls = PG_GETARG_BOOL(1);
2345 bool tableforest = PG_GETARG_BOOL(2);
2346 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2348 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
2355 query_to_xml(PG_FUNCTION_ARGS)
2357 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2358 bool nulls = PG_GETARG_BOOL(1);
2359 bool tableforest = PG_GETARG_BOOL(2);
2360 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2362 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2363 NULL, nulls, tableforest,
2369 cursor_to_xml(PG_FUNCTION_ARGS)
2371 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
2372 int32 count = PG_GETARG_INT32(1);
2373 bool nulls = PG_GETARG_BOOL(2);
2374 bool tableforest = PG_GETARG_BOOL(3);
2375 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2377 StringInfoData result;
2381 initStringInfo(&result);
2384 portal = SPI_cursor_find(name);
2387 (errcode(ERRCODE_UNDEFINED_CURSOR),
2388 errmsg("cursor \"%s\" does not exist", name)));
2390 SPI_cursor_fetch(portal, true, count);
2391 for (i = 0; i < SPI_processed; i++)
2392 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2393 tableforest, targetns, true);
2397 PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
2402 * Write the start tag of the root element of a data mapping.
2404 * top_level means that this is the very top level of the eventual
2405 * output. For example, when the user calls table_to_xml, then a call
2406 * with a table name to this function is the top level. When the user
2407 * calls database_to_xml, then a call with a schema name to this
2408 * function is not the top level. If top_level is false, then the XML
2409 * namespace declarations are omitted, because they supposedly already
2410 * appeared earlier in the output. Repeating them is not wrong, but
2414 xmldata_root_element_start(StringInfo result, const char *eltname,
2415 const char *xmlschema, const char *targetns,
2418 /* This isn't really wrong but currently makes no sense. */
2419 Assert(top_level || !xmlschema);
2421 appendStringInfo(result, "<%s", eltname);
2424 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2425 if (strlen(targetns) > 0)
2426 appendStringInfo(result, " xmlns=\"%s\"", targetns);
2430 /* FIXME: better targets */
2431 if (strlen(targetns) > 0)
2432 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2434 appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2436 appendStringInfo(result, ">\n");
2441 xmldata_root_element_end(StringInfo result, const char *eltname)
2443 appendStringInfo(result, "</%s>\n", eltname);
2448 query_to_xml_internal(const char *query, char *tablename,
2449 const char *xmlschema, bool nulls, bool tableforest,
2450 const char *targetns, bool top_level)
2457 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2461 result = makeStringInfo();
2464 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2466 (errcode(ERRCODE_DATA_EXCEPTION),
2467 errmsg("invalid query")));
2471 xmldata_root_element_start(result, xmltn, xmlschema,
2472 targetns, top_level);
2473 appendStringInfoString(result, "\n");
2477 appendStringInfo(result, "%s\n\n", xmlschema);
2479 for (i = 0; i < SPI_processed; i++)
2480 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2481 tableforest, targetns, top_level);
2484 xmldata_root_element_end(result, xmltn);
2493 table_to_xmlschema(PG_FUNCTION_ARGS)
2495 Oid relid = PG_GETARG_OID(0);
2496 bool nulls = PG_GETARG_BOOL(1);
2497 bool tableforest = PG_GETARG_BOOL(2);
2498 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2502 rel = heap_open(relid, AccessShareLock);
2503 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2504 tableforest, targetns);
2505 heap_close(rel, NoLock);
2507 PG_RETURN_XML_P(cstring_to_xmltype(result));
2512 query_to_xmlschema(PG_FUNCTION_ARGS)
2514 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2515 bool nulls = PG_GETARG_BOOL(1);
2516 bool tableforest = PG_GETARG_BOOL(2);
2517 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2524 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2525 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2527 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2528 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2530 result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2532 tableforest, targetns));
2533 SPI_cursor_close(portal);
2536 PG_RETURN_XML_P(cstring_to_xmltype(result));
2541 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2543 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
2544 bool nulls = PG_GETARG_BOOL(1);
2545 bool tableforest = PG_GETARG_BOOL(2);
2546 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2547 const char *xmlschema;
2551 portal = SPI_cursor_find(name);
2554 (errcode(ERRCODE_UNDEFINED_CURSOR),
2555 errmsg("cursor \"%s\" does not exist", name)));
2557 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2559 tableforest, targetns));
2562 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2567 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2569 Oid relid = PG_GETARG_OID(0);
2570 bool nulls = PG_GETARG_BOOL(1);
2571 bool tableforest = PG_GETARG_BOOL(2);
2572 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2574 const char *xmlschema;
2576 rel = heap_open(relid, AccessShareLock);
2577 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2578 tableforest, targetns);
2579 heap_close(rel, NoLock);
2581 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2582 xmlschema, nulls, tableforest,
2588 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2590 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2591 bool nulls = PG_GETARG_BOOL(1);
2592 bool tableforest = PG_GETARG_BOOL(2);
2593 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2595 const char *xmlschema;
2601 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2602 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2604 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2605 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2607 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2608 InvalidOid, nulls, tableforest, targetns));
2609 SPI_cursor_close(portal);
2612 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2613 xmlschema, nulls, tableforest,
2619 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2008
2620 * sections 9.13, 9.14.
2624 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2625 bool tableforest, const char *targetns, bool top_level)
2632 xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2634 result = makeStringInfo();
2636 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2637 appendStringInfoString(result, "\n");
2640 appendStringInfo(result, "%s\n\n", xmlschema);
2644 relid_list = schema_get_xml_visible_tables(nspid);
2648 foreach(cell, relid_list)
2650 Oid relid = lfirst_oid(cell);
2653 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2656 appendStringInfoString(result, subres->data);
2657 appendStringInfoChar(result, '\n');
2663 xmldata_root_element_end(result, xmlsn);
2670 schema_to_xml(PG_FUNCTION_ARGS)
2672 Name name = PG_GETARG_NAME(0);
2673 bool nulls = PG_GETARG_BOOL(1);
2674 bool tableforest = PG_GETARG_BOOL(2);
2675 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2680 schemaname = NameStr(*name);
2681 nspid = LookupExplicitNamespace(schemaname, false);
2683 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2684 nulls, tableforest, targetns, true)));
2689 * Write the start element of the root element of an XML Schema mapping.
2692 xsd_schema_element_start(StringInfo result, const char *targetns)
2694 appendStringInfoString(result,
2696 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2697 if (strlen(targetns) > 0)
2698 appendStringInfo(result,
2700 " targetNamespace=\"%s\"\n"
2701 " elementFormDefault=\"qualified\"",
2703 appendStringInfoString(result,
2709 xsd_schema_element_end(StringInfo result)
2711 appendStringInfoString(result, "</xsd:schema>");
2716 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2717 bool tableforest, const char *targetns)
2725 result = makeStringInfo();
2727 nspid = LookupExplicitNamespace(schemaname, false);
2729 xsd_schema_element_start(result, targetns);
2733 relid_list = schema_get_xml_visible_tables(nspid);
2736 foreach(cell, relid_list)
2740 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2741 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2742 heap_close(rel, NoLock);
2745 appendStringInfoString(result,
2746 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2748 appendStringInfoString(result,
2749 map_sql_schema_to_xmlschema_types(nspid, relid_list,
2750 nulls, tableforest, targetns));
2752 xsd_schema_element_end(result);
2761 schema_to_xmlschema(PG_FUNCTION_ARGS)
2763 Name name = PG_GETARG_NAME(0);
2764 bool nulls = PG_GETARG_BOOL(1);
2765 bool tableforest = PG_GETARG_BOOL(2);
2766 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2768 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2769 nulls, tableforest, targetns)));
2774 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2776 Name name = PG_GETARG_NAME(0);
2777 bool nulls = PG_GETARG_BOOL(1);
2778 bool tableforest = PG_GETARG_BOOL(2);
2779 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2782 StringInfo xmlschema;
2784 schemaname = NameStr(*name);
2785 nspid = LookupExplicitNamespace(schemaname, false);
2787 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2788 tableforest, targetns);
2790 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2791 xmlschema->data, nulls,
2792 tableforest, targetns, true)));
2797 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2008
2798 * sections 9.16, 9.17.
2802 database_to_xml_internal(const char *xmlschema, bool nulls,
2803 bool tableforest, const char *targetns)
2810 xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2812 result = makeStringInfo();
2814 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2815 appendStringInfoString(result, "\n");
2818 appendStringInfo(result, "%s\n\n", xmlschema);
2822 nspid_list = database_get_xml_visible_schemas();
2826 foreach(cell, nspid_list)
2828 Oid nspid = lfirst_oid(cell);
2831 subres = schema_to_xml_internal(nspid, NULL, nulls,
2832 tableforest, targetns, false);
2834 appendStringInfoString(result, subres->data);
2835 appendStringInfoChar(result, '\n');
2841 xmldata_root_element_end(result, xmlcn);
2848 database_to_xml(PG_FUNCTION_ARGS)
2850 bool nulls = PG_GETARG_BOOL(0);
2851 bool tableforest = PG_GETARG_BOOL(1);
2852 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2854 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2855 tableforest, targetns)));
2860 database_to_xmlschema_internal(bool nulls, bool tableforest,
2861 const char *targetns)
2869 result = makeStringInfo();
2871 xsd_schema_element_start(result, targetns);
2875 relid_list = database_get_xml_visible_tables();
2876 nspid_list = database_get_xml_visible_schemas();
2879 foreach(cell, relid_list)
2883 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2884 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2885 heap_close(rel, NoLock);
2888 appendStringInfoString(result,
2889 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2891 appendStringInfoString(result,
2892 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2894 xsd_schema_element_end(result);
2903 database_to_xmlschema(PG_FUNCTION_ARGS)
2905 bool nulls = PG_GETARG_BOOL(0);
2906 bool tableforest = PG_GETARG_BOOL(1);
2907 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2909 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2910 tableforest, targetns)));
2915 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2917 bool nulls = PG_GETARG_BOOL(0);
2918 bool tableforest = PG_GETARG_BOOL(1);
2919 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2920 StringInfo xmlschema;
2922 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2924 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2925 nulls, tableforest, targetns)));
2930 * Map a multi-part SQL name to an XML name; see SQL/XML:2008 section
2934 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2936 StringInfoData result;
2938 initStringInfo(&result);
2941 appendStringInfo(&result, "%s",
2942 map_sql_identifier_to_xml_name(a, true, true));
2944 appendStringInfo(&result, ".%s",
2945 map_sql_identifier_to_xml_name(b, true, true));
2947 appendStringInfo(&result, ".%s",
2948 map_sql_identifier_to_xml_name(c, true, true));
2950 appendStringInfo(&result, ".%s",
2951 map_sql_identifier_to_xml_name(d, true, true));
2958 * Map an SQL table to an XML Schema document; see SQL/XML:2008
2961 * Map an SQL table to XML Schema data types; see SQL/XML:2008 section
2965 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2966 bool tableforest, const char *targetns)
2970 char *tabletypename;
2972 StringInfoData result;
2974 initStringInfo(&result);
2976 if (OidIsValid(relid))
2979 Form_pg_class reltuple;
2981 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
2982 if (!HeapTupleIsValid(tuple))
2983 elog(ERROR, "cache lookup failed for relation %u", relid);
2984 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2986 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2989 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2990 get_database_name(MyDatabaseId),
2991 get_namespace_name(reltuple->relnamespace),
2992 NameStr(reltuple->relname));
2994 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2995 get_database_name(MyDatabaseId),
2996 get_namespace_name(reltuple->relnamespace),
2997 NameStr(reltuple->relname));
2999 ReleaseSysCache(tuple);
3008 tabletypename = "TableType";
3009 rowtypename = "RowType";
3012 xsd_schema_element_start(&result, targetns);
3014 appendStringInfoString(&result,
3015 map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
3017 appendStringInfo(&result,
3018 "<xsd:complexType name=\"%s\">\n"
3019 " <xsd:sequence>\n",
3022 for (i = 0; i < tupdesc->natts; i++)
3024 if (tupdesc->attrs[i]->attisdropped)
3026 appendStringInfo(&result,
3027 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3028 map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
3030 map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
3031 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3034 appendStringInfoString(&result,
3035 " </xsd:sequence>\n"
3036 "</xsd:complexType>\n\n");
3040 appendStringInfo(&result,
3041 "<xsd:complexType name=\"%s\">\n"
3043 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3044 " </xsd:sequence>\n"
3045 "</xsd:complexType>\n\n",
3046 tabletypename, rowtypename);
3048 appendStringInfo(&result,
3049 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3050 xmltn, tabletypename);
3053 appendStringInfo(&result,
3054 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3055 xmltn, rowtypename);
3057 xsd_schema_element_end(&result);
3064 * Map an SQL schema to XML Schema data types; see SQL/XML:2008
3068 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
3069 bool tableforest, const char *targetns)
3074 char *schematypename;
3075 StringInfoData result;
3078 dbname = get_database_name(MyDatabaseId);
3079 nspname = get_namespace_name(nspid);
3081 initStringInfo(&result);
3083 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3085 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3090 appendStringInfo(&result,
3091 "<xsd:complexType name=\"%s\">\n", schematypename);
3093 appendStringInfoString(&result,
3096 appendStringInfoString(&result,
3097 " <xsd:sequence>\n");
3099 foreach(cell, relid_list)
3101 Oid relid = lfirst_oid(cell);
3102 char *relname = get_rel_name(relid);
3103 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3104 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3110 appendStringInfo(&result,
3111 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3112 xmltn, tabletypename);
3114 appendStringInfo(&result,
3115 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3116 xmltn, tabletypename);
3120 appendStringInfoString(&result,
3123 appendStringInfoString(&result,
3124 " </xsd:sequence>\n");
3125 appendStringInfoString(&result,
3126 "</xsd:complexType>\n\n");
3128 appendStringInfo(&result,
3129 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3130 xmlsn, schematypename);
3137 * Map an SQL catalog to XML Schema data types; see SQL/XML:2008
3141 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
3142 bool tableforest, const char *targetns)
3146 char *catalogtypename;
3147 StringInfoData result;
3150 dbname = get_database_name(MyDatabaseId);
3152 initStringInfo(&result);
3154 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3156 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3161 appendStringInfo(&result,
3162 "<xsd:complexType name=\"%s\">\n", catalogtypename);
3163 appendStringInfoString(&result,
3166 foreach(cell, nspid_list)
3168 Oid nspid = lfirst_oid(cell);
3169 char *nspname = get_namespace_name(nspid);
3170 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3171 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3176 appendStringInfo(&result,
3177 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3178 xmlsn, schematypename);
3181 appendStringInfoString(&result,
3183 appendStringInfoString(&result,
3184 "</xsd:complexType>\n\n");
3186 appendStringInfo(&result,
3187 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3188 xmlcn, catalogtypename);
3195 * Map an SQL data type to an XML name; see SQL/XML:2008 section 9.4.
3198 map_sql_type_to_xml_name(Oid typeoid, int typmod)
3200 StringInfoData result;
3202 initStringInfo(&result);
3208 appendStringInfo(&result, "CHAR");
3210 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3214 appendStringInfo(&result, "VARCHAR");
3216 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3220 appendStringInfo(&result, "NUMERIC");
3222 appendStringInfo(&result, "NUMERIC_%d_%d",
3223 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3224 (typmod - VARHDRSZ) & 0xffff);
3227 appendStringInfo(&result, "INTEGER");
3230 appendStringInfo(&result, "SMALLINT");
3233 appendStringInfo(&result, "BIGINT");
3236 appendStringInfo(&result, "REAL");
3239 appendStringInfo(&result, "DOUBLE");
3242 appendStringInfo(&result, "BOOLEAN");
3246 appendStringInfo(&result, "TIME");
3248 appendStringInfo(&result, "TIME_%d", typmod);
3252 appendStringInfo(&result, "TIME_WTZ");
3254 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3258 appendStringInfo(&result, "TIMESTAMP");
3260 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3262 case TIMESTAMPTZOID:
3264 appendStringInfo(&result, "TIMESTAMP_WTZ");
3266 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3269 appendStringInfo(&result, "DATE");
3272 appendStringInfo(&result, "XML");
3277 Form_pg_type typtuple;
3279 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3280 if (!HeapTupleIsValid(tuple))
3281 elog(ERROR, "cache lookup failed for type %u", typeoid);
3282 typtuple = (Form_pg_type) GETSTRUCT(tuple);
3284 appendStringInfoString(&result,
3285 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3286 get_database_name(MyDatabaseId),
3287 get_namespace_name(typtuple->typnamespace),
3288 NameStr(typtuple->typname)));
3290 ReleaseSysCache(tuple);
3299 * Map a collection of SQL data types to XML Schema data types; see
3300 * SQL/XML:2008 section 9.7.
3303 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
3305 List *uniquetypes = NIL;
3307 StringInfoData result;
3310 /* extract all column types used in the set of TupleDescs */
3311 foreach(cell0, tupdesc_list)
3313 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3315 for (i = 0; i < tupdesc->natts; i++)
3317 if (tupdesc->attrs[i]->attisdropped)
3319 uniquetypes = list_append_unique_oid(uniquetypes,
3320 tupdesc->attrs[i]->atttypid);
3324 /* add base types of domains */
3325 foreach(cell0, uniquetypes)
3327 Oid typid = lfirst_oid(cell0);
3328 Oid basetypid = getBaseType(typid);
3330 if (basetypid != typid)
3331 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
3334 /* Convert to textual form */
3335 initStringInfo(&result);
3337 foreach(cell0, uniquetypes)
3339 appendStringInfo(&result, "%s\n",
3340 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
3349 * Map an SQL data type to a named XML Schema data type; see
3350 * SQL/XML:2008 sections 9.5 and 9.6.
3352 * (The distinction between 9.5 and 9.6 is basically that 9.6 adds
3353 * a name attribute, which this function does. The name-less version
3354 * 9.5 doesn't appear to be required anywhere.)
3357 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
3359 StringInfoData result;
3360 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3362 initStringInfo(&result);
3364 if (typeoid == XMLOID)
3366 appendStringInfo(&result,
3367 "<xsd:complexType mixed=\"true\">\n"
3369 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3370 " </xsd:sequence>\n"
3371 "</xsd:complexType>\n");
3375 appendStringInfo(&result,
3376 "<xsd:simpleType name=\"%s\">\n", typename);
3383 appendStringInfo(&result,
3384 " <xsd:restriction base=\"xsd:string\">\n");
3386 appendStringInfo(&result,
3387 " <xsd:maxLength value=\"%d\"/>\n",
3389 appendStringInfo(&result,
3390 " </xsd:restriction>\n");
3394 appendStringInfo(&result,
3395 " <xsd:restriction base=\"xsd:%s\">\n"
3396 " </xsd:restriction>\n",
3397 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3402 appendStringInfo(&result,
3403 " <xsd:restriction base=\"xsd:decimal\">\n"
3404 " <xsd:totalDigits value=\"%d\"/>\n"
3405 " <xsd:fractionDigits value=\"%d\"/>\n"
3406 " </xsd:restriction>\n",
3407 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3408 (typmod - VARHDRSZ) & 0xffff);
3412 appendStringInfo(&result,
3413 " <xsd:restriction base=\"xsd:short\">\n"
3414 " <xsd:maxInclusive value=\"%d\"/>\n"
3415 " <xsd:minInclusive value=\"%d\"/>\n"
3416 " </xsd:restriction>\n",
3417 SHRT_MAX, SHRT_MIN);
3421 appendStringInfo(&result,
3422 " <xsd:restriction base=\"xsd:int\">\n"
3423 " <xsd:maxInclusive value=\"%d\"/>\n"
3424 " <xsd:minInclusive value=\"%d\"/>\n"
3425 " </xsd:restriction>\n",
3430 appendStringInfo(&result,
3431 " <xsd:restriction base=\"xsd:long\">\n"
3432 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3433 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3434 " </xsd:restriction>\n",
3435 (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3436 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3440 appendStringInfo(&result,
3441 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3445 appendStringInfo(&result,
3446 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3450 appendStringInfo(&result,
3451 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3457 const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3460 appendStringInfo(&result,
3461 " <xsd:restriction base=\"xsd:time\">\n"
3462 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3463 " </xsd:restriction>\n", tz);
3464 else if (typmod == 0)
3465 appendStringInfo(&result,
3466 " <xsd:restriction base=\"xsd:time\">\n"
3467 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3468 " </xsd:restriction>\n", tz);
3470 appendStringInfo(&result,
3471 " <xsd:restriction base=\"xsd:time\">\n"
3472 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3473 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3478 case TIMESTAMPTZOID:
3480 const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3483 appendStringInfo(&result,
3484 " <xsd:restriction base=\"xsd:dateTime\">\n"
3485 " <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"
3486 " </xsd:restriction>\n", tz);
3487 else if (typmod == 0)
3488 appendStringInfo(&result,
3489 " <xsd:restriction base=\"xsd:dateTime\">\n"
3490 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3491 " </xsd:restriction>\n", tz);
3493 appendStringInfo(&result,
3494 " <xsd:restriction base=\"xsd:dateTime\">\n"
3495 " <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"
3496 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3501 appendStringInfo(&result,
3502 " <xsd:restriction base=\"xsd:date\">\n"
3503 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3504 " </xsd:restriction>\n");
3508 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3511 int32 base_typmod = -1;
3513 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3515 appendStringInfo(&result,
3516 " <xsd:restriction base=\"%s\"/>\n",
3517 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3521 appendStringInfo(&result,
3522 "</xsd:simpleType>\n");
3530 * Map an SQL row to an XML element, taking the row from the active
3531 * SPI cursor. See also SQL/XML:2008 section 9.10.
3534 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3535 bool nulls, bool tableforest,
3536 const char *targetns, bool top_level)
3542 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3552 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3554 appendStringInfoString(result, "<row>\n");
3556 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3562 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3564 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3565 SPI_tuptable->tupdesc,
3571 appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3574 appendStringInfo(result, " <%s>%s</%s>\n",
3576 map_sql_value_to_xml_value(colval,
3577 SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
3583 xmldata_root_element_end(result, xmltn);
3584 appendStringInfoChar(result, '\n');
3587 appendStringInfoString(result, "</row>\n\n");
3592 * XPath related functions
3598 * Convert XML node to text (dump subtree in case of element,
3599 * return value otherwise)
3602 xml_xmlnodetoxmltype(xmlNodePtr cur)
3606 if (cur->type == XML_ELEMENT_NODE)
3610 buf = xmlBufferCreate();
3613 xmlNodeDump(buf, NULL, cur, 0, 1);
3614 result = xmlBuffer_to_xmltype(buf);
3628 str = xmlXPathCastNodeToString(cur);
3631 /* Here we rely on XML having the same representation as TEXT */
3632 char *escaped = escape_xml((char *) str);
3634 result = (xmltype *) cstring_to_text(escaped);
3650 * Convert an XML XPath object (the result of evaluating an XPath expression)
3651 * to an array of xml values, which is returned at *astate. The function
3652 * result value is the number of elements in the array.
3654 * If "astate" is NULL then we don't generate the array value, but we still
3655 * return the number of elements it would have had.
3657 * Nodesets are converted to an array containing the nodes' textual
3658 * representations. Primitive values (float, double, string) are converted
3659 * to a single-element array containing the value's string representation.
3662 xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
3663 ArrayBuildState **astate)
3673 switch (xpathobj->type)
3676 if (xpathobj->nodesetval != NULL)
3678 result = xpathobj->nodesetval->nodeNr;
3683 for (i = 0; i < result; i++)
3685 datum = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3686 *astate = accumArrayResult(*astate, datum,
3688 CurrentMemoryContext);
3697 datum = BoolGetDatum(xpathobj->boolval);
3698 datumtype = BOOLOID;
3704 datum = Float8GetDatum(xpathobj->floatval);
3705 datumtype = FLOAT8OID;
3711 datum = CStringGetDatum((char *) xpathobj->stringval);
3712 datumtype = CSTRINGOID;
3716 elog(ERROR, "xpath expression result type %d is unsupported",
3718 return 0; /* keep compiler quiet */
3721 /* Common code for scalar-value cases */
3722 result_str = map_sql_value_to_xml_value(datum, datumtype, true);
3723 datum = PointerGetDatum(cstring_to_xmltype(result_str));
3724 *astate = accumArrayResult(*astate, datum,
3726 CurrentMemoryContext);
3732 * Common code for xpath() and xmlexists()
3734 * Evaluate XPath expression and return number of nodes in res_items
3735 * and array of XML values in astate. Either of those pointers can be
3736 * NULL if the corresponding result isn't wanted.
3738 * It is up to the user to ensure that the XML passed is in fact
3739 * an XML document - XPath doesn't work easily on fragments without
3740 * a context node being known.
3743 xpath_internal(text *xpath_expr_text, xmltype *data, ArrayType *namespaces,
3744 int *res_nitems, ArrayBuildState **astate)
3746 PgXmlErrorContext *xmlerrcxt;
3747 volatile xmlParserCtxtPtr ctxt = NULL;
3748 volatile xmlDocPtr doc = NULL;
3749 volatile xmlXPathContextPtr xpathctx = NULL;
3750 volatile xmlXPathCompExprPtr xpathcomp = NULL;
3751 volatile xmlXPathObjectPtr xpathobj = NULL;
3756 xmlChar *xpath_expr;
3759 Datum *ns_names_uris;
3760 bool *ns_names_uris_nulls;
3764 * Namespace mappings are passed as text[]. If an empty array is passed
3765 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
3766 * Else, a 2-dimensional array with length of the second axis being equal
3767 * to 2 should be passed, i.e., every subarray contains 2 elements, the
3768 * first element defining the name, the second one the URI. Example:
3769 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
3770 * 'http://example2.com']].
3772 ndim = namespaces ? ARR_NDIM(namespaces) : 0;
3777 dims = ARR_DIMS(namespaces);
3779 if (ndim != 2 || dims[1] != 2)
3781 (errcode(ERRCODE_DATA_EXCEPTION),
3782 errmsg("invalid array for XML namespace mapping"),
3783 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3785 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3787 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3788 &ns_names_uris, &ns_names_uris_nulls,
3791 Assert((ns_count % 2) == 0); /* checked above */
3792 ns_count /= 2; /* count pairs only */
3796 ns_names_uris = NULL;
3797 ns_names_uris_nulls = NULL;
3801 datastr = VARDATA(data);
3802 len = VARSIZE(data) - VARHDRSZ;
3803 xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3806 (errcode(ERRCODE_DATA_EXCEPTION),
3807 errmsg("empty XPath expression")));
3809 string = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
3810 memcpy(string, datastr, len);
3813 xpath_expr = (xmlChar *) palloc((xpath_len + 1) * sizeof(xmlChar));
3814 memcpy(xpath_expr, VARDATA(xpath_expr_text), xpath_len);
3815 xpath_expr[xpath_len] = '\0';
3817 xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
3824 * redundant XML parsing (two parsings for the same value during one
3825 * command execution are possible)
3827 ctxt = xmlNewParserCtxt();
3828 if (ctxt == NULL || xmlerrcxt->err_occurred)
3829 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
3830 "could not allocate parser context");
3831 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3832 if (doc == NULL || xmlerrcxt->err_occurred)
3833 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3834 "could not parse XML document");
3835 xpathctx = xmlXPathNewContext(doc);
3836 if (xpathctx == NULL || xmlerrcxt->err_occurred)
3837 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
3838 "could not allocate XPath context");
3839 xpathctx->node = xmlDocGetRootElement(doc);
3840 if (xpathctx->node == NULL || xmlerrcxt->err_occurred)
3841 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
3842 "could not find root XML element");
3844 /* register namespaces, if any */
3847 for (i = 0; i < ns_count; i++)
3852 if (ns_names_uris_nulls[i * 2] ||
3853 ns_names_uris_nulls[i * 2 + 1])
3855 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3856 errmsg("neither namespace name nor URI may be null")));
3857 ns_name = TextDatumGetCString(ns_names_uris[i * 2]);
3858 ns_uri = TextDatumGetCString(ns_names_uris[i * 2 + 1]);
3859 if (xmlXPathRegisterNs(xpathctx,
3860 (xmlChar *) ns_name,
3861 (xmlChar *) ns_uri) != 0)
3862 ereport(ERROR, /* is this an internal error??? */
3863 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3868 xpathcomp = xmlXPathCompile(xpath_expr);
3869 if (xpathcomp == NULL || xmlerrcxt->err_occurred)
3870 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
3871 "invalid XPath expression");
3874 * Version 2.6.27 introduces a function named
3875 * xmlXPathCompiledEvalToBoolean, which would be enough for xmlexists,
3876 * but we can derive the existence by whether any nodes are returned,
3877 * thereby preventing a library version upgrade and keeping the code
3880 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3881 if (xpathobj == NULL || xmlerrcxt->err_occurred)
3882 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
3883 "could not create XPath object");
3886 * Extract the results as requested.
3888 if (res_nitems != NULL)
3889 *res_nitems = xml_xpathobjtoxmlarray(xpathobj, astate);
3891 (void) xml_xpathobjtoxmlarray(xpathobj, astate);
3896 xmlXPathFreeObject(xpathobj);
3898 xmlXPathFreeCompExpr(xpathcomp);
3900 xmlXPathFreeContext(xpathctx);
3904 xmlFreeParserCtxt(ctxt);
3906 pg_xml_done(xmlerrcxt, true);
3912 xmlXPathFreeObject(xpathobj);
3913 xmlXPathFreeCompExpr(xpathcomp);
3914 xmlXPathFreeContext(xpathctx);
3916 xmlFreeParserCtxt(ctxt);
3918 pg_xml_done(xmlerrcxt, false);
3920 #endif /* USE_LIBXML */
3923 * Evaluate XPath expression and return array of XML values.
3925 * As we have no support of XQuery sequences yet, this function seems
3926 * to be the most useful one (array of XML functions plays a role of
3927 * some kind of substitution for XQuery sequences).
3930 xpath(PG_FUNCTION_ARGS)
3933 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3934 xmltype *data = PG_GETARG_XML_P(1);
3935 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3937 ArrayBuildState *astate;
3939 xpath_internal(xpath_expr_text, data, namespaces,
3940 &res_nitems, &astate);
3942 if (res_nitems == 0)
3943 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3945 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
3953 * Determines if the node specified by the supplied XPath exists
3954 * in a given XML document, returning a boolean.
3957 xmlexists(PG_FUNCTION_ARGS)
3960 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3961 xmltype *data = PG_GETARG_XML_P(1);
3964 xpath_internal(xpath_expr_text, data, NULL,
3967 PG_RETURN_BOOL(res_nitems > 0);
3975 * Determines if the node specified by the supplied XPath exists
3976 * in a given XML document, returning a boolean. Differs from
3977 * xmlexists as it supports namespaces and is not defined in SQL/XML.
3980 xpath_exists(PG_FUNCTION_ARGS)
3983 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3984 xmltype *data = PG_GETARG_XML_P(1);
3985 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3988 xpath_internal(xpath_expr_text, data, namespaces,
3991 PG_RETURN_BOOL(res_nitems > 0);
3999 * Functions for checking well-formed-ness
4004 wellformed_xml(text *data, XmlOptionType xmloption_arg)
4007 volatile xmlDocPtr doc = NULL;
4009 /* We want to catch any exceptions and return false */
4012 doc = xml_parse(data, xmloption_arg, true, GetDatabaseEncoding());
4030 xml_is_well_formed(PG_FUNCTION_ARGS)
4033 text *data = PG_GETARG_TEXT_P(0);
4035 PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4039 #endif /* not USE_LIBXML */
4043 xml_is_well_formed_document(PG_FUNCTION_ARGS)
4046 text *data = PG_GETARG_TEXT_P(0);
4048 PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4052 #endif /* not USE_LIBXML */
4056 xml_is_well_formed_content(PG_FUNCTION_ARGS)
4059 text *data = PG_GETARG_TEXT_P(0);
4061 PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4065 #endif /* not USE_LIBXML */