Index: /main/trunk/greenstone2/bin/script/explode_metadata_database.pl
===================================================================
--- /main/trunk/greenstone2/bin/script/explode_metadata_database.pl	(revision 24403)
+++ /main/trunk/greenstone2/bin/script/explode_metadata_database.pl	(revision 24404)
@@ -411,5 +411,5 @@
 	foreach my $pair (@$record_metadata) {
 	    my ($field, $value) = (@$pair);
-	    $field =~ s/^ex\.//; # remove ex. if present
+	    $field =~ s/^ex\.([^.]+)$/$1/; #remove any ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
 	    $value =~ s/\\\\/\\/g;
 	    my $document_file_full;
@@ -509,6 +509,5 @@
 	next if ($field =~ /\^all$/);  # ISISPlug specific!
 
-	# strip off any ex.
-	$field =~ s/^ex\.//;
+	$field =~ s/^ex\.([^.]+)$/$1/; #remove any ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
 
 	# Square brackets in metadata values need to be escaped so they don't confuse Greenstone/GLI
Index: /main/trunk/greenstone2/perllib/basebuildproc.pm
===================================================================
--- /main/trunk/greenstone2/perllib/basebuildproc.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/basebuildproc.pm	(revision 24404)
@@ -244,5 +244,6 @@
     my $self = shift (@_);
     my ($lang_meta, $langarr) = @_;
-    $lang_meta =~ s/^ex\.//; # strip ex. if there
+    $lang_meta =~ s/^ex\.([^.]+)$/$1/; # strip any ex. namespace iff it's the only namespace prefix (will leave ex.dc.* intact)
+
     $self->{'lang_meta'} = $lang_meta;
     $self->{'langarr'} = $langarr;
@@ -512,6 +513,6 @@
 		$value =~ s/\n/\\n/g;
 		$value =~ s/\r/\\r/g;
-		# remove ex. if there
-		$field =~ s/^ex\.//;
+		# remove any ex. iff it's the only namespace prefix (will leave ex.dc.* intact)
+		$field =~ s/^ex\.([^.]+)$/$1/; # $field =~ s/^ex\.//; 
 
 		# special case for UTF8URL metadata
@@ -696,5 +697,5 @@
 	    }
 	    else {
-		$field =~ s/^ex\.//; #strip ex. if present
+		$field =~ s/^ex\.([^.]+)$/$1/; # remove any ex. iff it's the only namespace prefix (will leave ex.dc.* intact)
 		@metadata_values = @{$doc_obj->get_metadata($doc_obj->get_top_section(), $field)};
 	    }
Index: /main/trunk/greenstone2/perllib/classify/BaseClassifier.pm
===================================================================
--- /main/trunk/greenstone2/perllib/classify/BaseClassifier.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/classify/BaseClassifier.pm	(revision 24404)
@@ -383,6 +383,9 @@
     return $metadata unless defined $metadata && $metadata =~ /\S/;
 
-    $metadata =~ s/^ex\.//;
-    $metadata =~ s/([,;:\/])ex\./$1/g;
+    # only remove ex. metadata prefix if there are no other prefixes after it
+    $metadata =~ s/(,|;|:|\/)/$1 /g; # insert a space separator so meta names like flex.Image don't become fl.Image
+    $metadata =~ s/(^| )ex\.([^.,;:\/]+)(,|;|:|\/|$)/$1$2$3/g; 
+					 $metadata =~ s/(,|;|:|\/) /$1/g;
+
     return $metadata;
 }
Index: /main/trunk/greenstone2/perllib/doc.pm
===================================================================
--- /main/trunk/greenstone2/perllib/doc.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/doc.pm	(revision 24404)
@@ -807,7 +807,7 @@
     }
 
-    # Remove the namespace if we are being told to ignore them
+    # Remove any namespace if we are being told to ignore them
     if($ignore_namespace) {
-	$field =~ s/^\w*\.//;
+	$field =~ s/^.*\.//; #$field =~ s/^\w*\.//;
     }
 
@@ -816,9 +816,11 @@
 	my $data_name = $data->[0];
 
-	# Remove the any namespace if we are being told to ignore them
+	# Remove any namespace if we are being told to ignore them
 	if($ignore_namespace) {
-	    $data_name =~ s/^\w*\.//;
-	}
-	$data_name =~ s/^ex\.//; # we always remove ex. - it maybe there in doc_obj, but we will never ask for it.
+	    $data_name =~ s/^.*\.//; #$data_name =~ s/^\w*\.//;
+	}
+	# we always remove ex. (but not any subsequent namespace) - ex. maybe there in doc_obj, but we will never ask for it.
+	$data_name =~ s/^ex\.([^.]+)$/$1/; #$data_name =~ s/^ex\.//; 
+	
 	return $data->[1] if (scalar(@$data) >= 2 && $data_name eq $field);
     }
@@ -842,7 +844,7 @@
     }
 
-    # Remove the any namespace if we are being told to ignore them
+    # Remove any namespace if we are being told to ignore them
     if($ignore_namespace) {
-	$field =~ s/^\w*\.//;
+	$field =~ s/^.*\.//;
     }
 
@@ -851,9 +853,12 @@
 
 	my $data_name = $data->[0];
-	# Remove the any namespace if we are being told to ignore them
+
+	# Remove any namespace if we are being told to ignore them
 	if($ignore_namespace) {
-	    $data_name =~ s/^\w*\.//;
-	}
-	$data_name =~ s/^ex\.//; # we always remove ex. - it maybe there in doc_obj, but we will never ask for it.
+	    $data_name =~ s/^.*\.//;
+	}	
+	# we always remove ex. (but not any subsequent namespace) - ex. maybe there in doc_obj, but we will never ask for it.
+	$data_name =~ s/^ex\.([^.]+)$/$1/;
+
         push (@metadata, $data->[1]) if ($data_name eq $field);
     }
Index: /main/trunk/greenstone2/perllib/lucenebuildproc.pm
===================================================================
--- /main/trunk/greenstone2/perllib/lucenebuildproc.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/lucenebuildproc.pm	(revision 24404)
@@ -226,5 +226,5 @@
 		}
 		else {
-		    $submeta =~ s/^ex\.//; #strip off ex.
+		    $submeta =~ s/^ex\.([^.]+)$/$1/; #strip off ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
 
 		    # its a metadata element
Index: /main/trunk/greenstone2/perllib/mgbuilder.pm
===================================================================
--- /main/trunk/greenstone2/perllib/mgbuilder.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/mgbuilder.pm	(revision 24404)
@@ -77,9 +77,13 @@
 	push (@{$self->{'collect_cfg'}->{'indexes'}}, "dummy:text");	
     }
-    # remove any ex.
+    # remove any ex. but only if there are no other metadata prefixes
     my @orig_indexes = @{$self->{'collect_cfg'}->{'indexes'}};
     $self->{'collect_cfg'}->{'indexes'} = [];
     foreach my $index (@orig_indexes) {
-	$index =~ s/([:,])ex\./$1/g;
+	#$index =~ s/ex\.([^.,:]+)(,|:|$)/$1$2/g; # doesn't preserve flex.Image, which is turned into fl.Image 
+	$index =~ s/(,|:)/$1 /g;
+	$index =~ s/(^| )ex\.([^.,:]+)(,|:|$)/$1$2$3/g;
+	$index =~ s/(,|:) /$1/g;
+
 	push (@{$self->{'collect_cfg'}->{'indexes'}}, $index);
     }
Index: /main/trunk/greenstone2/perllib/mgbuildproc.pm
===================================================================
--- /main/trunk/greenstone2/perllib/mgbuildproc.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/mgbuildproc.pm	(revision 24404)
@@ -107,5 +107,5 @@
 		    } else {
 			my $first = 1;
-			$real_field =~ s/^ex\.//; # remove ex namespace if there
+			$real_field =~ s/^ex\.([^.]+)$/$1/; # remove ex. namespace iff it's the only namespace prefix (will leave ex.dc.* intact)
 			my @section_metadata = @{$doc_obj->get_metadata ($section, $real_field)};
 			if ($level eq "section" && $section ne $doc_obj->get_top_section() && $self->{'indexing_text'} && defined ($self->{'sections_index_document_metadata'})) {
Index: /main/trunk/greenstone2/perllib/mgppbuilder.pm
===================================================================
--- /main/trunk/greenstone2/perllib/mgppbuilder.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/mgppbuilder.pm	(revision 24404)
@@ -101,8 +101,13 @@
     if (defined $indexes) {
 	$self->{'collect_cfg'}->{'indexes'} = [];
-	my $single_index = join(';', @$indexes).";";
-	# remove any ex. from index spec
-	$single_index =~ s/^ex\.//;
-	$single_index =~ s/([,;])ex\./$1/g;
+
+	# remove any ex. from index spec but iff it is the only namespace in the metadata name
+	my @indexes_copy = @$indexes; # make a copy, as 'map' changes entry in array
+	#map { $_ =~ s/(^|,|;)ex\.([^.]+)$/$1$2/; } @indexes_copy; # No. Will replace metanames like flex.Image with fl.Image
+	map { $_ =~ s/(,|;)/$1 /g; } @indexes_copy; # introduce a space after every separator
+	map { $_ =~ s/(^| )ex\.([^.,:]+)(,|;|$)/$1$2$3/g; } @indexes_copy; # replace all <ex.> at start of metanames or <, ex.> when in a comma separated list
+	map { $_ =~ s/(,|:) /$1/g; } @indexes_copy; # remove space introduced after every separator
+	my $single_index = join(';', @indexes_copy).";";
+
 	push (@{$self->{'collect_cfg'}->{'indexes'}}, $single_index);
     }
Index: /main/trunk/greenstone2/perllib/mgppbuildproc.pm
===================================================================
--- /main/trunk/greenstone2/perllib/mgppbuildproc.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/mgppbuildproc.pm	(revision 24404)
@@ -326,5 +326,5 @@
 		    }
 		    else {
-			$submeta =~ s/^ex\.//; #strip off ex.
+			$submeta =~ s/^ex\.([^.]+)$/$1/; #strip off ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
 			# its a metadata element
 			my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
Index: /main/trunk/greenstone2/perllib/plugins/BasePlugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/BasePlugin.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/plugins/BasePlugin.pm	(revision 24404)
@@ -204,6 +204,6 @@
     $self->{'plugin_type'} = $plugin_name;
 
-    # remove ex. from OIDmetadata
-    $self->{'OIDmetadata'} =~ s/^ex\.// if defined $self->{'OIDmetadata'};
+    # remove ex. from OIDmetadata iff it's the only namespace prefix
+    $self->{'OIDmetadata'} =~ s/^ex\.([^.]+)$/$1/ if defined $self->{'OIDmetadata'};
     $self->{'num_processed'} = 0;
     $self->{'num_not_processed'} = 0;
Index: /main/trunk/greenstone2/perllib/plugins/EmbeddedMetadataPlugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/EmbeddedMetadataPlugin.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/plugins/EmbeddedMetadataPlugin.pm	(revision 24404)
@@ -287,5 +287,5 @@
 
 
-sub metadata_read()
+sub metadata_read
 {
     my $self = shift (@_);
@@ -314,5 +314,5 @@
 }
 
-sub process()
+sub process
 {
     # not used
Index: /main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm	(revision 24404)
@@ -90,6 +90,6 @@
 	return bless $self, $class;
     }
-    # trim ex. from document field (if there)
-    $self->{'document_field'} =~ s/^ex\.//;
+    # trim any ex. from document field iff it's the only metadata namespace prefix    
+    $self->{'document_field'} =~ s/^ex\.([^.]+)$/$1/;
     return bless $self, $class;
 }
@@ -531,5 +531,7 @@
 
 	    # so that GLI can see this metadata, store here as ex.dc.Title etc
-	    my $ex_metaname = "ex.$metaname";
+	    my $ex_metaname = $metaname;
+	    $ex_metaname =~ s/^ex\.//; # remove any pre-existing ex. prefix
+	    $ex_metaname = "ex.$ex_metaname"; # at last can prefix ex.
 
 	    if (defined $metadata->{$ex_metaname})
Index: /main/trunk/greenstone2/perllib/plugouts/BasePlugout.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugouts/BasePlugout.pm	(revision 24403)
+++ /main/trunk/greenstone2/perllib/plugouts/BasePlugout.pm	(revision 24404)
@@ -965,5 +965,7 @@
 
 # returns an XML representation of the dublin core metadata
-# if dc meta is not found, try ex mete
+# if dc meta is not found, try ex meta
+# This method is not used by the DSpacePlugout, which has its
+# own method to save its dc metadata
 sub get_dc_metadata {
     my $self = shift(@_);
@@ -978,7 +980,15 @@
 
     my $explicit_dc = {};
+    my $explicit_ex_dc = {};
     my $explicit_ex = {};
 
     my $all_text="";
+    
+    # We want high quality dc metadata to go in first, so we store all the
+    # assigned dc.* values first. Then, for all those dc metadata names in
+    # the official dc set that are as yet unassigned, we look to see whether
+    # embedded ex.dc.* metadata has defined some values for them. If not,
+    # then for the same missing dc metadata names, we look in ex metadata.
+
     foreach my $data (@{$section_ptr->{'metadata'}}){
 	my $escaped_value = &docprint::escape_text($data->[1]);
@@ -1002,8 +1012,20 @@
 	    }
 
-	} 
-	elsif (($data->[0] =~ m/^ex\./) || ($data->[0] !~ m/\./)) {
+	} elsif ($data->[0]=~ m/^ex\.dc\./) { # now look through ex.dc.* to fill in as yet unassigned fields in dc metaset
+	    $data->[0] =~ m/^ex\.dc\.(.*)/;
+	    my $ex_dc_element = $1;
+	    my $lc_ex_dc_element = lc($ex_dc_element);
+
+	    # only store the ex.dc value for this dc metaname if no dc.* was assigned for it
+	    if (defined $dc_set->{$ex_dc_element}) { 
+		if (!defined $explicit_ex_dc->{$lc_ex_dc_element}) {
+		    $explicit_ex_dc->{$lc_ex_dc_element} = [];
+		}
+		push(@{$explicit_ex_dc->{$lc_ex_dc_element}},$escaped_value);
+	    }
+	}
+	elsif (($data->[0] =~ m/^ex\./) || ($data->[0] !~ m/\./)) { # look through ex. meta (incl. meta without prefix)
 	    $data->[0] =~ m/^(ex\.)?(.*)/;
-	    my $ex_element =  $2;
+	    my $ex_element = $2;
 	    my $lc_ex_element = lc($ex_element);
 
@@ -1023,6 +1045,20 @@
 
 	if (!defined $explicit_dc->{$lc_k}) {
-	    if (defined $explicit_ex->{$lc_k}) {
-
+	    # try to find if ex.dc.* defines this dc.* meta,
+	    # if not, then look for whether there's an ex.* equivalent
+
+	    if (defined $explicit_ex_dc->{$lc_k}) {
+		foreach my $v (@{$explicit_ex_dc->{$lc_k}}) {
+		    my $dc_element    = $lc_k;
+		    my $escaped_value = $v;
+		    
+		    if (defined $version && ($version eq "oai_dc")) {
+			$all_text .= "   <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
+		    }
+		    else {
+			$all_text .= '   <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
+		    }		    
+		}
+	    } elsif (defined $explicit_ex->{$lc_k}) {
 		foreach my $v (@{$explicit_ex->{$lc_k}}) {
 		    my $dc_element    = $lc_k;
@@ -1035,5 +1071,4 @@
 			$all_text .= '   <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
 		    }
-		    
 		}
 	    }
