Commit ea3b212f authored by Bruce Momjian's avatar Bruce Momjian

Commit newest version of xmlpath().

Nikolay Samokhvalov
parent f317a037
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.36 2007/03/22 20:14:58 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.37 2007/03/22 20:26:30 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -91,7 +91,7 @@ static xmlChar *xml_text2xmlChar(text *in); ...@@ -91,7 +91,7 @@ static xmlChar *xml_text2xmlChar(text *in);
static int parse_xml_decl(const xmlChar *str, size_t *lenp, xmlChar **version, xmlChar **encoding, int *standalone); static int parse_xml_decl(const xmlChar *str, size_t *lenp, xmlChar **version, xmlChar **encoding, int *standalone);
static bool print_xml_decl(StringInfo buf, const xmlChar *version, pg_enc encoding, int standalone); static bool print_xml_decl(StringInfo buf, const xmlChar *version, pg_enc encoding, int standalone);
static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace, xmlChar *encoding); static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace, xmlChar *encoding);
static text *xml_xmlnodetotext(xmlNodePtr cur); static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
#endif /* USE_LIBXML */ #endif /* USE_LIBXML */
...@@ -2414,20 +2414,31 @@ SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename, bool n ...@@ -2414,20 +2414,31 @@ SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename, bool n
#ifdef USE_LIBXML #ifdef USE_LIBXML
/* /*
* Convert XML node to text (return value only, it's not dumping) * Convert XML node to text (dump subtree in case of element, return value otherwise)
*/ */
text * text *
xml_xmlnodetotext(xmlNodePtr cur) xml_xmlnodetoxmltype(xmlNodePtr cur)
{ {
xmlChar *str; xmlChar *str;
text *result; xmltype *result;
size_t len; size_t len;
xmlBufferPtr buf;
str = xmlXPathCastNodeToString(cur); if (cur->type == XML_ELEMENT_NODE)
len = strlen((char *) str); {
result = (text *) palloc(len + VARHDRSZ); buf = xmlBufferCreate();
SET_VARSIZE(result, len + VARHDRSZ); xmlNodeDump(buf, NULL, cur, 0, 1);
memcpy(VARDATA(result), str, len); result = xmlBuffer_to_xmltype(buf);
xmlBufferFree(buf);
}
else
{
str = xmlXPathCastNodeToString(cur);
len = strlen((char *) str);
result = (text *) palloc(len + VARHDRSZ);
SET_VARSIZE(result, len + VARHDRSZ);
memcpy(VARDATA(result), str, len);
}
return result; return result;
} }
...@@ -2607,7 +2618,7 @@ xmlpath(PG_FUNCTION_ARGS) ...@@ -2607,7 +2618,7 @@ xmlpath(PG_FUNCTION_ARGS)
{ {
Datum elem; Datum elem;
bool elemisnull = false; bool elemisnull = false;
elem = PointerGetDatum(xml_xmlnodetotext(xpathobj->nodesetval->nodeTab[i])); elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
astate = accumArrayResult(astate, elem, astate = accumArrayResult(astate, elem,
elemisnull, XMLOID, elemisnull, XMLOID,
CurrentMemoryContext); CurrentMemoryContext);
......
...@@ -403,10 +403,10 @@ SELECT table_name, view_definition FROM information_schema.views ...@@ -403,10 +403,10 @@ SELECT table_name, view_definition FROM information_schema.views
-- Text XPath expressions evaluation -- Text XPath expressions evaluation
SELECT xmlpath('/value', data) FROM xmltest; SELECT xmlpath('/value', data) FROM xmltest;
xmlpath xmlpath
--------- ----------------------
{one} {<value>one</value>}
{two} {<value>two</value>}
(2 rows) (2 rows)
SELECT xmlpath(NULL, NULL) IS NULL FROM xmltest; SELECT xmlpath(NULL, NULL) IS NULL FROM xmltest;
...@@ -431,3 +431,9 @@ SELECT xmlpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><l ...@@ -431,3 +431,9 @@ SELECT xmlpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><l
{1,2} {1,2}
(1 row) (1 row)
SELECT xmlpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
xmlpath
-------------------------
{<b>two</b>,<b>etc</b>}
(1 row)
...@@ -212,3 +212,5 @@ SELECT xmlpath('//text()', '<local:data xmlns:local="http://127.0.0.1"><local:pi ...@@ -212,3 +212,5 @@ SELECT xmlpath('//text()', '<local:data xmlns:local="http://127.0.0.1"><local:pi
ERROR: no XML support in this installation ERROR: no XML support in this installation
SELECT xmlpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc'], ARRAY['http://127.0.0.1']]); SELECT xmlpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc'], ARRAY['http://127.0.0.1']]);
ERROR: no XML support in this installation ERROR: no XML support in this installation
SELECT xmlpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
ERROR: no XML support in this installation
...@@ -152,3 +152,4 @@ SELECT xmlpath(NULL, NULL) IS NULL FROM xmltest; ...@@ -152,3 +152,4 @@ SELECT xmlpath(NULL, NULL) IS NULL FROM xmltest;
SELECT xmlpath('', '<!-- error -->'); SELECT xmlpath('', '<!-- error -->');
SELECT xmlpath('//text()', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>'); SELECT xmlpath('//text()', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>');
SELECT xmlpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc'], ARRAY['http://127.0.0.1']]); SELECT xmlpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc'], ARRAY['http://127.0.0.1']]);
SELECT xmlpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment