Index: /trunk/gsdl/perllib/doc.pm
===================================================================
--- /trunk/gsdl/perllib/doc.pm	(revision 2266)
+++ /trunk/gsdl/perllib/doc.pm	(revision 2267)
@@ -143,5 +143,5 @@
 
 
-sub buffer_section {
+sub buffer_section_gml {
     my $self = shift (@_);
     my ($section, $suppress_subject_info) = @_;
@@ -171,5 +171,6 @@
     # output all the subsections
     foreach $subsection (@{$section_ptr->{'subsection_order'}}) {
-	$all_text .= $self->buffer_section("$section.$subsection", $suppress_subject_info);
+	$all_text .= $self->buffer_section_gml("$section.$subsection", 
+					       $suppress_subject_info);
     }
     
@@ -180,9 +181,77 @@
 }
 
+
+sub buffer_section_xml {
+    my $self = shift (@_);
+    my ($section, $dtd_metadata, $suppress_subject_info) = @_;
+
+    $suppress_subject_info = 0 unless defined $suppress_subject_info;
+    my ($all_text, $data, $subsection);
+    
+    my $section_ptr = $self->_lookup_section ($section);
+    my ($section_num) = $section =~ /(\d+)$/;
+    
+    return "" unless defined $section_ptr;
+
+    # output the section header (including the section number
+    # and metadata)
+
+    $all_text .= "<gsdlsection";
+    $all_text .= " gsdlnum=\"$section_num\"" if defined $section_num;
+    $all_text .= ">\n";
+    
+    $all_text .= "  <metadata>\n";
+    
+    # output metadata
+    foreach $data (@{$section_ptr->{'metadata'}}) {
+	my $tag_name = $data->[0];
+	my $tag_value = &_escape_text($data->[1]);
+
+	unless ($suppress_subject_info && $tag_name eq "Subject")
+	{
+	    if (defined $dtd_metadata)
+	    {
+		$dtd_metadata->{$tag_name}++;
+	    }
+
+	    $all_text .= "    <$tag_name>$tag_value</$tag_name>\n";
+	}
+    }
+
+    $all_text .= "  </metadata>\n";
+
+    # output the text
+    $all_text .= &_escape_text($section_ptr->{'text'});
+
+    # output all the subsections
+    foreach $subsection (@{$section_ptr->{'subsection_order'}}) {
+	$all_text .= $self->buffer_section_xml("$section.$subsection", 
+					       $dtd_metadata,
+					       $suppress_subject_info);
+    }
+    
+    # output the closing tag
+    $all_text .=  "</gsdlsection>\n";
+
+    return $all_text;
+}
+
 sub output_section {
     my $self = shift (@_);
-    my ($handle, $section, $suppress_subject_info) = @_;
-
-    my $all_text = $self->buffer_section($section, $suppress_subject_info);
+    my ($handle, $section, $colname, $dtd_metadata, 
+	$suppress_subject_info) = @_;
+
+    my $all_text = $self->buffer_section_xml($section, $dtd_metadata, 
+					     $suppress_subject_info);
+
+    # xml header    
+    if (defined $collection)
+    {
+	my $xml_head
+	    = '<? xml version="1.0" standalone="no" encoding="UTF-8" ?>';
+	$xml_head .= "\n<!DOCTYPE gsdl:$colname SYSTEM \"$colname.dtd\">\n";
+	$all_text = $xml_head.$all_text;
+    }
+
     print $handle $all_text;
 }
@@ -262,5 +331,6 @@
 		print STDERR "doc::set_OID could not write to $filename\n";
 	    } else {
-		$self->output_section('OUTFILE', $self->get_top_section(), 1);
+		$self->output_section('OUTFILE', $self->get_top_section(), 
+				      undef, 1);
 		close (OUTFILE);
 	    }
@@ -283,5 +353,6 @@
     if (!defined $OID) 
     {
-	my $hash_text = $self->buffer_section($self->get_top_section(), 1);
+	my $hash_text = $self->buffer_section_gml($self->get_top_section(),
+						  undef, 1);
 	my $hash_len = length($hash_text);
 
@@ -493,6 +564,4 @@
     my $section = $mastersection;
     my $sectionref = $self;
-
-####    print STDERR "*** mastersection = $mastersection\n";
 
     while ($section ne "") {
Index: /trunk/gsdl/perllib/docprint.pm
===================================================================
--- /trunk/gsdl/perllib/docprint.pm	(revision 2266)
+++ /trunk/gsdl/perllib/docprint.pm	(revision 2267)
@@ -60,5 +60,6 @@
     
     # output document
-    $doc_obj->output_section(STDOUT, $doc_obj->get_top_section());
+    $doc_obj->output_section(STDOUT, $doc_obj->get_top_section(),
+			     undef, undef, 0);
 }
 
Index: /trunk/gsdl/perllib/docsave.pm
===================================================================
--- /trunk/gsdl/perllib/docsave.pm	(revision 2266)
+++ /trunk/gsdl/perllib/docsave.pm	(revision 2267)
@@ -53,8 +53,11 @@
     $self->{'groupsize'} = $groupsize;
     $self->{'gs_count'} = 0;
-    
+
+    # keep an associate array of all metavalues used by collection to
+    # help generate the XML DTD 
+    $self->{'dtd_metadata'} = {}; 
+
     $self->{'outhandle'} = STDERR;
     $self->{'outhandle'} = $outhandle if defined $outhandle;
-    
     # set a default for the archive directory
     $self->{'archive_dir'} = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
@@ -113,5 +116,9 @@
 
 	# save this document
-	$doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
+	$doc_obj->output_section('docsave::OUTDOC', 
+				 $doc_obj->get_top_section(),
+				 $self->{'collection'},
+				 $self->{'dtd_metadata'},0);
+
 	close OUTDOC;
 
@@ -184,5 +191,8 @@
 
     # save this document
-    $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
+    $doc_obj->output_section('docsave::OUTDOC', 
+			     $doc_obj->get_top_section(),
+			     $self->{'collection'},
+			     $self->{'dtd_metadata'},0);
 
     $self->{'gs_count'}++;
Index: /trunk/gsdl/perllib/plugins/GMLPlug.pm
===================================================================
--- /trunk/gsdl/perllib/plugins/GMLPlug.pm	(revision 2266)
+++ /trunk/gsdl/perllib/plugins/GMLPlug.pm	(revision 2267)
@@ -96,5 +96,5 @@
 
 	    my @indenting_sections = split("<gsdlsection", $gml);
-	    shift(@indenting_sections); # first entry is trivially empty
+	    shift(@indenting_sections); # skips over xml header if present
 
 	    foreach $gml (@indenting_sections) {
@@ -127,5 +127,6 @@
 		}
 	    
-		# add the tags
+		# add the metadata
+		# could be stored as either attributes or ....
 		while ((defined $tags) && ($tags =~ s/^\s*(\S+)=\"([^\"]*)\"//o)) {
 		    $doc_obj->add_utf8_metadata($section, $1, &GMLPlug::_unescape_text($2)) 
@@ -134,5 +135,20 @@
 		}
 		
+		# ... or tags (xml compliant)
+		if ($text =~ m/^\s*<metadata>/)
+		{
+		    my $metadata;
+		    ($metadata,$text) 
+			= ($text =~ m/\s*<metadata>\s*(<.*)\s*<\/metadata>(.*)$/s);
+		    while ((defined $metadata) 
+			   && ($metadata =~ s/<(.*?)>(.*?)<\/.*?>//s))
+		    {
+			$doc_obj->add_utf8_metadata($section, $1, &GMLPlug::_unescape_text($2)) 
+			    if (defined $1 and defined $2);
+		    }
+		}
+
 		# add the text
+
 		$doc_obj->add_utf8_text($section, $text) 
 		    if ((defined $text) && ($text ne ""));		
