Skip to content

Commit ea3b212

Browse files
committed
Commit newest version of xmlpath().
Nikolay Samokhvalov
1 parent f317a03 commit ea3b212

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

src/backend/utils/adt/xml.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.36 2007/03/22 20:14:58 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.37 2007/03/22 20:26:30 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -91,7 +91,7 @@ static xmlChar *xml_text2xmlChar(text *in);
9191
static int parse_xml_decl(const xmlChar *str, size_t *lenp, xmlChar **version, xmlChar **encoding, int *standalone);
9292
static bool print_xml_decl(StringInfo buf, const xmlChar *version, pg_enc encoding, int standalone);
9393
static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace, xmlChar *encoding);
94-
static text *xml_xmlnodetotext(xmlNodePtr cur);
94+
static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
9595

9696
#endif /* USE_LIBXML */
9797

@@ -2414,20 +2414,31 @@ SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename, bool n
24142414

24152415
#ifdef USE_LIBXML
24162416
/*
2417-
* Convert XML node to text (return value only, it's not dumping)
2417+
* Convert XML node to text (dump subtree in case of element, return value otherwise)
24182418
*/
24192419
text *
2420-
xml_xmlnodetotext(xmlNodePtr cur)
2420+
xml_xmlnodetoxmltype(xmlNodePtr cur)
24212421
{
2422-
xmlChar *str;
2423-
text *result;
2424-
size_t len;
2422+
xmlChar *str;
2423+
xmltype *result;
2424+
size_t len;
2425+
xmlBufferPtr buf;
24252426

2426-
str = xmlXPathCastNodeToString(cur);
2427-
len = strlen((char *) str);
2428-
result = (text *) palloc(len + VARHDRSZ);
2429-
SET_VARSIZE(result, len + VARHDRSZ);
2430-
memcpy(VARDATA(result), str, len);
2427+
if (cur->type == XML_ELEMENT_NODE)
2428+
{
2429+
buf = xmlBufferCreate();
2430+
xmlNodeDump(buf, NULL, cur, 0, 1);
2431+
result = xmlBuffer_to_xmltype(buf);
2432+
xmlBufferFree(buf);
2433+
}
2434+
else
2435+
{
2436+
str = xmlXPathCastNodeToString(cur);
2437+
len = strlen((char *) str);
2438+
result = (text *) palloc(len + VARHDRSZ);
2439+
SET_VARSIZE(result, len + VARHDRSZ);
2440+
memcpy(VARDATA(result), str, len);
2441+
}
24312442

24322443
return result;
24332444
}
@@ -2607,7 +2618,7 @@ xmlpath(PG_FUNCTION_ARGS)
26072618
{
26082619
Datum elem;
26092620
bool elemisnull = false;
2610-
elem = PointerGetDatum(xml_xmlnodetotext(xpathobj->nodesetval->nodeTab[i]));
2621+
elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
26112622
astate = accumArrayResult(astate, elem,
26122623
elemisnull, XMLOID,
26132624
CurrentMemoryContext);

src/test/regress/expected/xml.out

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ SELECT table_name, view_definition FROM information_schema.views
403403

404404
-- Text XPath expressions evaluation
405405
SELECT xmlpath('/value', data) FROM xmltest;
406-
xmlpath
407-
---------
408-
{one}
409-
{two}
406+
xmlpath
407+
----------------------
408+
{<value>one</value>}
409+
{<value>two</value>}
410410
(2 rows)
411411

412412
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
431431
{1,2}
432432
(1 row)
433433

434+
SELECT xmlpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
435+
xmlpath
436+
-------------------------
437+
{<b>two</b>,<b>etc</b>}
438+
(1 row)
439+

src/test/regress/expected/xml_1.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,5 @@ SELECT xmlpath('//text()', '<local:data xmlns:local="http://127.0.0.1"><local:pi
212212
ERROR: no XML support in this installation
213213
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']]);
214214
ERROR: no XML support in this installation
215+
SELECT xmlpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
216+
ERROR: no XML support in this installation

src/test/regress/sql/xml.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,4 @@ SELECT xmlpath(NULL, NULL) IS NULL FROM xmltest;
152152
SELECT xmlpath('', '<!-- error -->');
153153
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>');
154154
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']]);
155+
SELECT xmlpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy