77 * Portions Copyright (c) 1996-2009, 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.89 2009/06/08 21:32:33 petere Exp $
10+ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.90 2009/06/09 22:00:57 petere Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -569,7 +569,7 @@ xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
569569 if (isnull )
570570 str = NULL ;
571571 else
572- str = map_sql_value_to_xml_value (value , exprType ((Node * ) e -> expr ));
572+ str = map_sql_value_to_xml_value (value , exprType ((Node * ) e -> expr ), false );
573573 named_arg_strings = lappend (named_arg_strings , str );
574574 i ++ ;
575575 }
@@ -587,7 +587,7 @@ xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
587587 if (!isnull )
588588 {
589589 str = map_sql_value_to_xml_value (value ,
590- exprType ((Node * ) e -> expr ));
590+ exprType ((Node * ) e -> expr ), true );
591591 arg_strings = lappend (arg_strings , str );
592592 }
593593 }
@@ -1580,9 +1580,18 @@ map_xml_name_to_sql_identifier(char *name)
15801580
15811581/*
15821582 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1583+ *
1584+ * When xml_escape_strings is true, then certain characters in string
1585+ * values are replaced by entity references (< etc.), as specified
1586+ * in SQL/XML:2003 section 9.16 GR 8) ii). This is normally what is
1587+ * wanted. The false case is mainly useful when the resulting value
1588+ * is used with xmlTextWriterWriteAttribute() to write out an
1589+ * attribute, because that function does the escaping itself. The SQL
1590+ * standard of 2003 is somewhat buggy in this regard, so we do our
1591+ * best to make sense.
15831592 */
15841593char *
1585- map_sql_value_to_xml_value (Datum value , Oid type )
1594+ map_sql_value_to_xml_value (Datum value , Oid type , bool xml_escape_strings )
15861595{
15871596 StringInfoData buf ;
15881597
@@ -1616,7 +1625,7 @@ map_sql_value_to_xml_value(Datum value, Oid type)
16161625 appendStringInfoString (& buf , "<element>" );
16171626 appendStringInfoString (& buf ,
16181627 map_sql_value_to_xml_value (elem_values [i ],
1619- elmtype ));
1628+ elmtype , true ));
16201629 appendStringInfoString (& buf , "</element>" );
16211630 }
16221631
@@ -1774,8 +1783,8 @@ map_sql_value_to_xml_value(Datum value, Oid type)
17741783 getTypeOutputInfo (type , & typeOut , & isvarlena );
17751784 str = OidOutputFunctionCall (typeOut , value );
17761785
1777- /* ... exactly as-is for XML */
1778- if (type == XMLOID )
1786+ /* ... exactly as-is for XML, and when escaping is not wanted */
1787+ if (type == XMLOID || ! xml_escape_strings )
17791788 return str ;
17801789
17811790 /* otherwise, translate special characters as needed */
@@ -3183,7 +3192,7 @@ SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
31833192 appendStringInfo (result , " <%s>%s</%s>\n" ,
31843193 colname ,
31853194 map_sql_value_to_xml_value (colval ,
3186- SPI_gettypeid (SPI_tuptable -> tupdesc , i )),
3195+ SPI_gettypeid (SPI_tuptable -> tupdesc , i ), true ),
31873196 colname );
31883197 }
31893198
0 commit comments