Version: 1.0

Type: Function

Category: Databases

License: GNU General Public License

Description: This function translates a Postgres Query Result into an XML DOM object. Please check requirements for using this function.



<?
	//------------------------------------------------------------------------------------------------------------------
	// Description: This function handles postgres->xml data
	//				transformations.
	// Author:  Cody McCain (mcm99c@acu.edu)
	// License: GNU Public
	// Last Updated: 5/7/2001
	//
	// Requirements:	To use XML/XSLT with PHP you must have DOM XML
	//					(libxml-2.2.7) [www.xmlsoft.org],
	//					Sablot [www.gingerall.com] (or another xsl parser)
	//					compiled into PHP.
	//
	//					This function does not require Sablot, but I encourage
	//					you to use XSLT stylesheets as this separates content
	//					from data (very important in multi-tier environment).
	//
	// Abstract:	I am constantly transforming recordsets (SQL Query Results)
	//				so I can provide the appropriate data for my XSL stylesheets.
	//				This function takes a postgres query result and converts it
	//				to an XML DOM object.
	//
	// usage example:
	//       $error = recordset_to_xml_ext ( $result, "items", "item", \
	//					array ("field1", "field2"), $xmldoc );
	//
	// The xml document will be assigned to $xmldoc.
	//
	// You can convert select fields of the recordset to
	// attributes instead of children of the respective row
	// by including it in the attribute array.
	//
	//------------------------------------------------------------------------------------------------------------------
	
	
	function recordset_to_xml_ext ( $result_query, $collection, $entry, $attributes, &$xmldoc )
	{
		// Make sure we have a valid  result from our query
		// Check PostgreSQL functions for details on how to make database queries.
		
		if( !$result_query )
		{
			trigger_error( "The query result passed was invalid.  Please check the SQL Query." );
			return 1;
		}

		$fieldnum = pg_numfields( $result_query );
		$rownum = pg_numrows( $result_query );

		// Create array that lists all of our fields

		for( $i=0; $i < $fieldnum; $i++ )
		{
			$fieldnames[] = pg_fieldname( $result_query, $i );
		}
		
		// Create new XML Document

		$xmldoc = domxml_new_xmldoc("1.0");

		if( strlen( $collection ) == 0 )
		{
			trigger_error( "Collection (root of xml) label cannot be null." );
			return 1;
		}

		if( strlen( $entry ) == 0 )
		{
			trigger_error( "Entry (children of root in xml) label cannot be null." );
			return 1;
		}

		// Here's where we add all the recordset to the xml document.

		$xmlroot = $xmldoc->add_root( $collection );

		for( $i=0; $i < $rownum; $i++ )
		{
			$row = pg_fetch_row( $result_query, $i );
			$xmlentry = $xmlroot->new_child( $entry, "" );
			for( $j=0; $j < $fieldnum; $j++ )
			{
				if( in_array( $fieldnames[$j], $attributes, false ) )
				{
					$xmlentry->set_attribute( $fieldnames[$j], $row[$j] );
				}
				else
				{
					$xmlentry->new_child( $fieldnames[$j], $row[$j] );
				}
			}
		}

		return 0;

	}
?>