diff -cpr head/src/backend/executor/execQual.c not-encode-xmlattributes/src/backend/executor/execQual.c
*** head/src/backend/executor/execQual.c	Fri Apr 10 09:40:51 2009
--- not-encode-xmlattributes/src/backend/executor/execQual.c	Thu May 28 19:26:47 2009
*************** ExecEvalXml(XmlExprState *xmlExpr, ExprC
*** 3242,3248 ****
  				{
  					appendStringInfo(&buf, "<%s>%s</%s>",
  									 argname,
! 									 map_sql_value_to_xml_value(value, exprType((Node *) e->expr)),
  									 argname);
  					*isNull = false;
  				}
--- 3242,3248 ----
  				{
  					appendStringInfo(&buf, "<%s>%s</%s>",
  									 argname,
! 									 map_sql_value_to_xml_value(value, exprType((Node *) e->expr), true),
  									 argname);
  					*isNull = false;
  				}
diff -cpr head/src/backend/utils/adt/xml.c not-encode-xmlattributes/src/backend/utils/adt/xml.c
*** head/src/backend/utils/adt/xml.c	Mon May 18 12:23:11 2009
--- not-encode-xmlattributes/src/backend/utils/adt/xml.c	Thu May 28 19:26:47 2009
*************** xmlelement(XmlExprState *xmlExpr, ExprCo
*** 569,575 ****
  		if (isnull)
  			str = NULL;
  		else
! 			str = map_sql_value_to_xml_value(value, exprType((Node *) e->expr));
  		named_arg_strings = lappend(named_arg_strings, str);
  		i++;
  	}
--- 569,575 ----
  		if (isnull)
  			str = NULL;
  		else
! 			str = map_sql_value_to_xml_value(value, exprType((Node *) e->expr), false);
  		named_arg_strings = lappend(named_arg_strings, str);
  		i++;
  	}
*************** xmlelement(XmlExprState *xmlExpr, ExprCo
*** 587,593 ****
  		if (!isnull)
  		{
  			str = map_sql_value_to_xml_value(value,
! 											 exprType((Node *) e->expr));
  			arg_strings = lappend(arg_strings, str);
  		}
  	}
--- 587,593 ----
  		if (!isnull)
  		{
  			str = map_sql_value_to_xml_value(value,
! 											 exprType((Node *) e->expr), true);
  			arg_strings = lappend(arg_strings, str);
  		}
  	}
*************** map_xml_name_to_sql_identifier(char *nam
*** 1582,1588 ****
   * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
   */
  char *
! map_sql_value_to_xml_value(Datum value, Oid type)
  {
  	StringInfoData buf;
  
--- 1582,1588 ----
   * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
   */
  char *
! map_sql_value_to_xml_value(Datum value, Oid type, bool encode)
  {
  	StringInfoData buf;
  
*************** map_sql_value_to_xml_value(Datum value, 
*** 1616,1622 ****
  			appendStringInfoString(&buf, "<element>");
  			appendStringInfoString(&buf,
  								   map_sql_value_to_xml_value(elem_values[i],
! 															  elmtype));
  			appendStringInfoString(&buf, "</element>");
  		}
  
--- 1616,1622 ----
  			appendStringInfoString(&buf, "<element>");
  			appendStringInfoString(&buf,
  								   map_sql_value_to_xml_value(elem_values[i],
! 															  elmtype, true));
  			appendStringInfoString(&buf, "</element>");
  		}
  
*************** map_sql_value_to_xml_value(Datum value, 
*** 1774,1781 ****
  		getTypeOutputInfo(type, &typeOut, &isvarlena);
  		str = OidOutputFunctionCall(typeOut, value);
  
! 		/* ... exactly as-is for XML */
! 		if (type == XMLOID)
  			return str;
  
  		/* otherwise, translate special characters as needed */
--- 1774,1781 ----
  		getTypeOutputInfo(type, &typeOut, &isvarlena);
  		str = OidOutputFunctionCall(typeOut, value);
  
! 		/* ... exactly as-is for XML or encode is not required */
! 		if (type == XMLOID || !encode)
  			return str;
  
  		/* otherwise, translate special characters as needed */
*************** SPI_sql_row_to_xmlelement(int rownum, St
*** 3179,3185 ****
  			appendStringInfo(result, "  <%s>%s</%s>\n",
  							 colname,
  							 map_sql_value_to_xml_value(colval,
! 									SPI_gettypeid(SPI_tuptable->tupdesc, i)),
  							 colname);
  	}
  
--- 3179,3185 ----
  			appendStringInfo(result, "  <%s>%s</%s>\n",
  							 colname,
  							 map_sql_value_to_xml_value(colval,
! 								SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
  							 colname);
  	}
  
diff -cpr head/src/include/utils/xml.h not-encode-xmlattributes/src/include/utils/xml.h
*** head/src/include/utils/xml.h	Mon May 18 12:23:11 2009
--- not-encode-xmlattributes/src/include/utils/xml.h	Thu May 28 19:26:47 2009
*************** extern text *xmltotext_with_xmloption(xm
*** 73,79 ****
  
  extern char *map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period);
  extern char *map_xml_name_to_sql_identifier(char *name);
! extern char *map_sql_value_to_xml_value(Datum value, Oid type);
  
  typedef enum
  {
--- 73,79 ----
  
  extern char *map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period);
  extern char *map_xml_name_to_sql_identifier(char *name);
! extern char *map_sql_value_to_xml_value(Datum value, Oid type, bool encode);
  
  typedef enum
  {
diff -cpr head/src/test/regress/expected/xml.out not-encode-xmlattributes/src/test/regress/expected/xml.out
*** head/src/test/regress/expected/xml.out	Fri Apr 10 09:40:51 2009
--- not-encode-xmlattributes/src/test/regress/expected/xml.out	Thu May 28 19:26:47 2009
*************** SELECT xmlconcat(NULL, NULL);
*** 90,100 ****
  (1 row)
  
  SELECT xmlelement(name element,
!                   xmlattributes (1 as one, 'deuce' as two),
!                   'content');
!                    xmlelement                   
! ------------------------------------------------
!  <element one="1" two="deuce">content</element>
  (1 row)
  
  SELECT xmlelement(name element,
--- 90,100 ----
  (1 row)
  
  SELECT xmlelement(name element,
!                   xmlattributes (1 as one, 'deuce' as two, '<>&"''' as three),
!                   'content', '<>&"''');
!                                          xmlelement
! --------------------------------------------------------------------------------------------
!  <element one="1" two="deuce" three="&lt;&gt;&amp;&quot;'">content&lt;&gt;&amp;"'</element>
  (1 row)
  
  SELECT xmlelement(name element,
diff -cpr head/src/test/regress/sql/xml.sql not-encode-xmlattributes/src/test/regress/sql/xml.sql
*** head/src/test/regress/sql/xml.sql	Fri Apr 10 09:40:51 2009
--- not-encode-xmlattributes/src/test/regress/sql/xml.sql	Thu May 28 19:26:47 2009
*************** SELECT xmlconcat(NULL, NULL);
*** 31,38 ****
  
  
  SELECT xmlelement(name element,
!                   xmlattributes (1 as one, 'deuce' as two),
!                   'content');
  
  SELECT xmlelement(name element,
                    xmlattributes ('unnamed and wrong'));
--- 31,38 ----
  
  
  SELECT xmlelement(name element,
!                   xmlattributes (1 as one, 'deuce' as two, '<>&"''' as three),
!                   'content', '<>&"''');
  
  SELECT xmlelement(name element,
                    xmlattributes ('unnamed and wrong'));
