Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java	(revision 25925)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java	(revision 25926)
@@ -935,5 +935,63 @@
 		// not found
 		return null;
-
+	}
+
+	public static NodeList getNamedElementsNS(Element parent, String namespace_uri, String node_local_name, String attribute_name, String attribute_value)
+	{
+		MyNodeList result = new MyNodeList();
+		NodeList children = parent.getChildNodes();
+		for (int i = 0; i < children.getLength(); i++)
+		{
+			Node child = children.item(i);
+			if (child.getNodeType() == Node.ELEMENT_NODE && child.getNamespaceURI().equals(namespace_uri) && child.getLocalName().equals(node_local_name))
+			{
+				if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
+					result.addNode(child);
+			}
+		}
+		return result;
+	}
+
+	public static NodeList getElementsWithAttributesNS(Element parent, String namespace_uri, String node_local_name, String[] attribute_names, String[] attribute_values)
+	{
+		if (attribute_names.length == 0 || attribute_names.length != attribute_values.length)
+		{
+			return new MyNodeList();
+		}
+
+		MyNodeList result = new MyNodeList();
+
+		NodeList matchingNodes = GSXML.getNamedElementsNS(parent, namespace_uri, node_local_name, attribute_names[0], attribute_values[0]);
+		for (int i = 0; i < matchingNodes.getLength(); i++)
+		{
+			Element current = (Element) matchingNodes.item(i);
+			boolean nodeMatches = true;
+			for (int j = 1; j < attribute_names.length; j++)
+			{
+				String currentName = attribute_names[j];
+				String currentValue = attribute_values[j];
+				if (!current.getAttribute(currentName).equals(currentValue))
+				{
+					nodeMatches = false;
+					break;
+				}
+			}
+
+			if (nodeMatches)
+			{
+				result.addNode(current);
+			}
+		}
+
+		return result;
+	}
+
+	public static void removeElementsWithAttributesNS(Element parent, String namespace_uri, String node_local_name, String[] attribute_names, String[] attribute_values)
+	{
+		NodeList matchingNodes = GSXML.getElementsWithAttributesNS(parent, namespace_uri, node_local_name, attribute_names, attribute_values);
+		for (int i = 0; i < matchingNodes.getLength(); i++)
+		{
+			parent.removeChild(matchingNodes.item(i));
+		}
 	}
 
@@ -955,5 +1013,4 @@
 			parent.removeChild(old_elem);
 		}
-
 	}
 
Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXSLT.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXSLT.java	(revision 25925)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXSLT.java	(revision 25926)
@@ -163,13 +163,6 @@
 			{
 				// if we have a name attribute, remove any other similarly named template
-				if (!template_name.equals(""))
-				{
-					GSXML.removeNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "template", "name", template_name);
-				}
-				// if we have a match attribute, remove other templates that match - 
-				if (!template_match.equals(""))
-				{
-					GSXML.removeNamedElementsNS(main, "http://www.w3.org/1999/XSL/Transform", "template", "match", template_match);
-				}
+				GSXML.removeElementsWithAttributesNS(main, "http://www.w3.org/1999/XSL/Transform", "template", new String[]{"name", "match", "mode"}, new String[]{template_name, template_match, template_mode});
+
 				// now add our good template in
 				main.appendChild(main_xsl.importNode(node, true));
@@ -181,10 +174,5 @@
 				// there can't be any duplicate named templates, so just look for matches
 				// we already have the one with highest import precedence (from the top most level) so don't add any more in
-				if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "template", "match", template_match) == null)
-				{
-					main.appendChild(main_xsl.importNode(node, true));
-				}
-
-				if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "template", "name", template_name) == null)
+				if (GSXML.getElementsWithAttributesNS(main, "http://www.w3.org/1999/XSL/Transform", "template", new String[]{"name", "match", "mode"}, new String[]{template_name, template_match, template_mode}).getLength() == 0)
 				{
 					main.appendChild(main_xsl.importNode(node, true));
