Skip to content

Commit 68ba403

Browse files
author
Japleen Kaur
committed
empty element name
1 parent f29c9e5 commit 68ba403

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

contrib/babelfishpg_tsql/src/tsql_for/forxml.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,34 @@ tsql_row_to_xml_raw(StringInfo state, Datum record, const char *element_name, bo
229229
tmptup.t_data = td;
230230
tuple = &tmptup;
231231

232-
/* Output opening tag */
233-
if (elements)
232+
/*
233+
* Empty element name without ELEMENTS mode is not allowed — attribute-centric
234+
* serialization requires a row tag name.
235+
*/
236+
if (element_name[0] == '\0' && !elements)
234237
{
235-
/* ELEMENTS mode: <row><col>value</col></row> */
236-
if (xsinil)
237-
appendStringInfo(state, "<%s " XML_XMLNS_XSI ">", element_name);
238-
else
239-
appendStringInfo(state, "<%s>", element_name);
238+
ereport(ERROR,
239+
(errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
240+
errmsg("Row tag omission (empty row tag name) cannot be used "
241+
"with attribute-centric FOR XML serialization.")));
240242
}
241-
else
243+
244+
/* Output opening tag (only when element_name is non-empty) */
245+
if (element_name[0] != '\0')
242246
{
243-
/* ATTRIBUTES mode: <row col="value"/> */
244-
appendStringInfo(state, "<%s", element_name);
247+
if (elements)
248+
{
249+
/* ELEMENTS mode: <row><col>value</col></row> */
250+
if (xsinil)
251+
appendStringInfo(state, "<%s " XML_XMLNS_XSI ">", element_name);
252+
else
253+
appendStringInfo(state, "<%s>", element_name);
254+
}
255+
else
256+
{
257+
/* ATTRIBUTES mode: <row col="value"/> */
258+
appendStringInfo(state, "<%s", element_name);
259+
}
245260
}
246261

247262
for (int i = 0; i < tupdesc->natts; i++)
@@ -296,7 +311,14 @@ tsql_row_to_xml_raw(StringInfo state, Datum record, const char *element_name, bo
296311
/* Output closing tag */
297312
if (elements)
298313
{
299-
if (allnull)
314+
if (element_name[0] == '\0')
315+
{
316+
/*
317+
* Empty element name with ELEMENTS: no wrapper tag needed.
318+
* Just output the child elements directly, same as PATH('').
319+
*/
320+
}
321+
else if (allnull)
300322
{
301323
/*
302324
* If all column values are NULL, produce a self-closing element

test/JDBC/expected/forxml-raw-elements-vu-verify.out

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ SELECT NULL AS a, NULL AS b FOR XML RAW, ELEMENTS;
143143
GO
144144
~~START~~
145145
ntext
146-
<row></row>
146+
<row/>
147147
~~END~~
148148

149149

@@ -170,7 +170,7 @@ SELECT NULL AS a, NULL AS b, NULL AS c, NULL AS d FOR XML RAW, ELEMENTS;
170170
GO
171171
~~START~~
172172
ntext
173-
<row></row>
173+
<row/>
174174
~~END~~
175175

176176

@@ -198,7 +198,7 @@ SELECT NULL AS a, NULL AS b FOR XML RAW, ELEMENTS ABSENT;
198198
GO
199199
~~START~~
200200
ntext
201-
<row></row>
201+
<row/>
202202
~~END~~
203203

204204

@@ -510,7 +510,7 @@ SELECT name, salary FROM forxml_raw_elements_t1 FOR XML RAW, ELEMENTS;
510510
GO
511511
~~START~~
512512
ntext
513-
<row><name>John</name><salary>50000</salary></row><row><name>Jane</name><salary>60000</salary></row><row><name>Bob</name></row><row><name>Alice</name><salary>70000</salary></row><row></row>
513+
<row><name>John</name><salary>50000</salary></row><row><name>Jane</name><salary>60000</salary></row><row><name>Bob</name></row><row><name>Alice</name><salary>70000</salary></row><row/>
514514
~~END~~
515515

516516

@@ -950,7 +950,7 @@ SELECT CAST(NULL AS VARCHAR(10)) AS null_val FOR XML RAW, ELEMENTS;
950950
GO
951951
~~START~~
952952
ntext
953-
<row></row>
953+
<row/>
954954
~~END~~
955955

956956

@@ -971,10 +971,20 @@ SELECT 1 AS a, 2 AS b FOR XML RAW(''), ELEMENTS;
971971
GO
972972
~~START~~
973973
ntext
974-
<><a>1</a><b>2</b></>
974+
<a>1</a><b>2</b>
975975
~~END~~
976976

977977

978+
-- Empty element name without ELEMENTS (attribute-centric) - should error
979+
SELECT 1 AS a, 2 AS b FOR XML RAW('');
980+
GO
981+
~~START~~
982+
ntext
983+
~~ERROR (Code: 33557097)~~
984+
985+
~~ERROR (Message: Row tag omission (empty row tag name) cannot be used with attribute-centric FOR XML serialization.)~~
986+
987+
978988
-- Element name with spaces
979989
SELECT 1 AS a FOR XML RAW('element name'), ELEMENTS;
980990
GO

test/JDBC/input/forxml/forxml-raw-elements-vu-verify.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ GO
414414
SELECT 1 AS a, 2 AS b FOR XML RAW(''), ELEMENTS;
415415
GO
416416

417+
-- Empty element name without ELEMENTS (attribute-centric) - should error
418+
SELECT 1 AS a, 2 AS b FOR XML RAW('');
419+
GO
420+
417421
-- Element name with spaces
418422
SELECT 1 AS a FOR XML RAW('element name'), ELEMENTS;
419423
GO

0 commit comments

Comments
 (0)