*** a/contrib/xml2/xpath.c
--- b/contrib/xml2/xpath.c
***************
*** 27,33 **** PG_MODULE_MAGIC;
/* externally accessible functions */
- Datum xml_is_well_formed(PG_FUNCTION_ARGS);
Datum xml_encode_special_chars(PG_FUNCTION_ARGS);
Datum xpath_nodeset(PG_FUNCTION_ARGS);
Datum xpath_string(PG_FUNCTION_ARGS);
--- 27,32 ----
***************
*** 70,97 **** pgxml_parser_init(void)
xmlLoadExtDtdDefaultValue = 1;
}
-
- /* Returns true if document is well-formed */
-
- PG_FUNCTION_INFO_V1(xml_is_well_formed);
-
- Datum
- xml_is_well_formed(PG_FUNCTION_ARGS)
- {
- text *t = PG_GETARG_TEXT_P(0); /* document buffer */
- int32 docsize = VARSIZE(t) - VARHDRSZ;
- xmlDocPtr doctree;
-
- pgxml_parser_init();
-
- doctree = xmlParseMemory((char *) VARDATA(t), docsize);
- if (doctree == NULL)
- PG_RETURN_BOOL(false); /* i.e. not well-formed */
- xmlFreeDoc(doctree);
- PG_RETURN_BOOL(true);
- }
-
-
/* Encodes special characters (<, >, &, " and \r) as XML entities */
PG_FUNCTION_INFO_V1(xml_encode_special_chars);
--- 69,74 ----
*** a/doc/src/sgml/func.sgml
--- b/doc/src/sgml/func.sgml
***************
*** 8554,8562 **** SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
]]>
! XML Predicates
IS DOCUMENT
--- 8554,8566 ----
]]>
+
+
+
+ XML Predicates
! IS DOCUMENT
IS DOCUMENT
***************
*** 8574,8579 **** SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
--- 8578,8675 ----
between documents and content fragments.
+
+
+ xml_is_well_formed
+
+
+ xml_is_well_formed
+ well formed
+
+
+
+ xml_is_well_formed(text)
+
+
+
+ The function xml_is_well_formed evaluates whether
+ the text is well formed XML content, returning
+ a boolean.
+
+
+ Example:
+ bar');
+ xml_is_well_formed
+ --------------------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('barstuff');
+ xml_is_well_formed
+ --------------------
+ f
+ (1 row)
+ ]]>
+
+
+ In addition to the structure checks, the function ensures that namespaces are correcty matched.
+ bar');
+ xml_is_well_formed
+ --------------------
+ f
+ (1 row)
+
+ SELECT xml_is_well_formed('bar');
+ xml_is_well_formed
+ --------------------
+ t
+ (1 row)
+ ]]>
+
+
+ This function can be combined with the IS DOCUMENT predicate to prevent
+ invalid XML content errors from occuring in queries. For example, given a
+ table that may have rows with invalid XML mixed in with rows of valid
+ XML, xml_is_well_formed can be used to filter out all
+ the invalid rows.
+
+
+ Example:
+ bar
+ barbarfoo
+ barfoo'
+ barbar
+
+
*** a/src/backend/utils/adt/xml.c
--- b/src/backend/utils/adt/xml.c
***************
*** 3293,3298 **** xml_xmlnodetoxmltype(xmlNodePtr cur)
--- 3293,3366 ----
}
#endif
+ Datum
+ xml_is_well_formed(PG_FUNCTION_ARGS)
+ {
+ #ifdef USE_LIBXML
+ text *data = PG_GETARG_TEXT_P(0);
+ bool result;
+ int res_code;
+ int32 len;
+ const xmlChar *string;
+ xmlParserCtxtPtr ctxt;
+ xmlDocPtr doc = NULL;
+
+ len = VARSIZE(data) - VARHDRSZ;
+ string = xml_text2xmlChar(data);
+
+ /* Start up libxml and its parser (no-ops if already done) */
+ pg_xml_init();
+ xmlInitParser();
+
+ ctxt = xmlNewParserCtxt();
+ if (ctxt == NULL)
+ xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
+ "could not allocate parser context");
+
+ PG_TRY();
+ {
+ size_t count;
+ xmlChar *version = NULL;
+ int standalone = -1;
+
+ res_code = parse_xml_decl(string, &count, &version, NULL, &standalone);
+ if (res_code != 0)
+ result = 0;
+ else
+ {
+
+ doc = xmlNewDoc(version);
+ doc->encoding = xmlStrdup((const xmlChar *) "UTF-8");
+ doc->standalone = 1;
+
+ res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, string + count, NULL);
+
+ result = !res_code;
+ }
+ }
+ PG_CATCH();
+ {
+ if (doc)
+ xmlFreeDoc(doc);
+ if (ctxt)
+ xmlFreeParserCtxt(ctxt);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (doc)
+ xmlFreeDoc(doc);
+ if (ctxt)
+ xmlFreeParserCtxt(ctxt);
+
+ return result;
+ #else
+ NO_XML_SUPPORT();
+ return 0;
+ #endif
+ }
+
/*
* Evaluate XPath expression and return array of XML values.
*** a/src/include/catalog/pg_proc.h
--- b/src/include/catalog/pg_proc.h
***************
*** 4391,4396 **** DESCR("evaluate XPath expression, with namespaces support");
--- 4391,4399 ----
DATA(insert OID = 2932 ( xpath PGNSP PGUID 14 1 0 0 f f f t f i 2 0 143 "25 142" _null_ _null_ _null_ _null_ "select pg_catalog.xpath($1, $2, ''{}''::pg_catalog.text[])" _null_ _null_ _null_ ));
DESCR("evaluate XPath expression");
+ DATA(insert OID = 3037 ( xml_is_well_formed PGNSP PGUID 12 1 0 0 f f f t f i 1 0 16 "25" _null_ _null_ _null_ _null_ xml_is_well_formed _null_ _null_ _null_ ));
+ DESCR("determine if a text fragment is well formed XML");
+
/* uuid */
DATA(insert OID = 2952 ( uuid_in PGNSP PGUID 12 1 0 0 f f f t f i 1 0 2950 "2275" _null_ _null_ _null_ _null_ uuid_in _null_ _null_ _null_ ));
DESCR("I/O");
*** a/src/include/utils/xml.h
--- b/src/include/utils/xml.h
***************
*** 46,51 **** extern Datum query_to_xmlschema(PG_FUNCTION_ARGS);
--- 46,52 ----
extern Datum cursor_to_xmlschema(PG_FUNCTION_ARGS);
extern Datum table_to_xml_and_xmlschema(PG_FUNCTION_ARGS);
extern Datum query_to_xml_and_xmlschema(PG_FUNCTION_ARGS);
+ extern Datum xml_is_well_formed(PG_FUNCTION_ARGS);
extern Datum schema_to_xml(PG_FUNCTION_ARGS);
extern Datum schema_to_xmlschema(PG_FUNCTION_ARGS);
*** a/src/test/regress/expected/xml.out
--- b/src/test/regress/expected/xml.out
***************
*** 502,504 **** SELECT xpath('//b', 'one two three etc');
--- 502,577 ----
{two,etc}
(1 row)
+ -- Test xml_is_well_formed
+ SELECT xml_is_well_formed('<>');
+ xml_is_well_formed
+ --------------------
+ f
+ (1 row)
+
+ SELECT xml_is_well_formed('abc');
+ xml_is_well_formed
+ --------------------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('');
+ xml_is_well_formed
+ --------------------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('bar');
+ xml_is_well_formed
+ --------------------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('barbaz');
+ xml_is_well_formed
+ --------------------
+ f
+ (1 row)
+
+ SELECT xml_is_well_formed('number one');
+ xml_is_well_formed
+ --------------------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('bar') AND 'bar' IS DOCUMENT;
+ ?column?
+ ----------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('barbaz') AND 'barbaz' IS NOT DOCUMENT;
+ ?column?
+ ----------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('barfoo') AND 'barfoo' IS NOT DOCUMENT;
+ ?column?
+ ----------
+ t
+ (1 row)
+
+ SELECT xml_is_well_formed('bar');
+ xml_is_well_formed
+ --------------------
+ f
+ (1 row)
+
+ SELECT xml_is_well_formed('bar');
+ xml_is_well_formed
+ --------------------
+ t
+ (1 row)
+
*** a/src/test/regress/sql/xml.sql
--- b/src/test/regress/sql/xml.sql
***************
*** 163,165 **** SELECT xpath('', '');
--- 163,180 ----
SELECT xpath('//text()', 'number one');
SELECT xpath('//loc:piece/@id', 'number one', ARRAY[ARRAY['loc', 'http://127.0.0.1']]);
SELECT xpath('//b', 'one two three etc');
+
+ -- Test xml_is_well_formed
+
+ SELECT xml_is_well_formed('<>');
+ SELECT xml_is_well_formed('abc');
+ SELECT xml_is_well_formed('');
+ SELECT xml_is_well_formed('bar');
+ SELECT xml_is_well_formed('barbaz');
+ SELECT xml_is_well_formed('number one');
+ SELECT xml_is_well_formed('bar') AND 'bar' IS DOCUMENT;
+ SELECT xml_is_well_formed('barbaz') AND 'barbaz' IS NOT DOCUMENT;
+ SELECT xml_is_well_formed('barfoo') AND 'barfoo' IS NOT DOCUMENT;
+ SELECT xml_is_well_formed('bar');
+ SELECT xml_is_well_formed('bar');