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)
569569if (isnull )
570570str = NULL ;
571571else
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 );
573573named_arg_strings = lappend (named_arg_strings ,str );
574574i ++ ;
575575}
@@ -587,7 +587,7 @@ xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
587587if (!isnull )
588588{
589589str = map_sql_value_to_xml_value (value ,
590- exprType ((Node * )e -> expr ));
590+ exprType ((Node * )e -> expr ), true );
591591arg_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{
15871596StringInfoData buf ;
15881597
@@ -1616,7 +1625,7 @@ map_sql_value_to_xml_value(Datum value, Oid type)
16161625appendStringInfoString (& buf ,"<element>" );
16171626appendStringInfoString (& buf ,
16181627map_sql_value_to_xml_value (elem_values [i ],
1619- elmtype ));
1628+ elmtype , true ));
16201629appendStringInfoString (& buf ,"</element>" );
16211630}
16221631
@@ -1774,8 +1783,8 @@ map_sql_value_to_xml_value(Datum value, Oid type)
17741783getTypeOutputInfo (type ,& typeOut ,& isvarlena );
17751784str = 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 )
17791788return str ;
17801789
17811790/* otherwise, translate special characters as needed */
@@ -3183,7 +3192,7 @@ SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
31833192appendStringInfo (result ," <%s>%s</%s>\n" ,
31843193colname ,
31853194map_sql_value_to_xml_value (colval ,
3186- SPI_gettypeid (SPI_tuptable -> tupdesc ,i )),
3195+ SPI_gettypeid (SPI_tuptable -> tupdesc ,i ), true ),
31873196colname );
31883197}
31893198