Index: src/edu/ucsb/nceas/metacat/QuerySpecification.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 3770)
+++ src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 3771)
@@ -896,14 +896,22 @@
                 self.append("xml_nodes.nodedata, ");
                 self.append("xml_nodes.parentnodeid, ");
                 self.append("xml_nodes.nodetype ");
-                self.append("from xml_nodes, xml_documents ");
-                self.append("where parentnodeid IN ");
+                //self.append("from xml_nodes, xml_documents ");
+                self.append("from xml_nodes ");
+                self.append("where ");
                 self.append(QueryTerm.useNestedStatements(path));
 
                 self.append(" AND xml_nodes.docid in (");
                 self.append(doclist);
-                self.append(") AND xml_nodes.nodetype = 'TEXT'");
-                self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
+                if (returnFieldIsAttribute(path))
+                {
+                    self.append(")");
+                }
+                else
+                {
+                     self.append(") AND xml_nodes.nodetype = 'TEXT'");
+                }
+                //self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
 
                 //addAccessRestrictionSQL(unaccessableNodePair, self);
             }
@@ -911,6 +919,34 @@
             return self.toString();
         }
     }
+    
+    /*
+     * Determines the returnfield is an attribute of not. 
+     * For given returnfield, this programm will cut the part of path after last slash.
+     * If no slash in the path, the original string will be considered as last part.
+     * If first character of last part is @ it will retrun true. 
+     */
+    private boolean returnFieldIsAttribute(String path)
+    {
+    	boolean isAttribute = false;
+    	if (path != null)
+    	{
+    	    int slashIndex = path.lastIndexOf("/");
+    	    if (slashIndex !=-1)
+    	    {
+    	    	// if there is slash in the path, path should be replace by the last part
+    	    	path = path.substring(slashIndex+1);
+    	    }
+    	    logMetacat.debug("In QuerySpecification.returnFieldIsAttribute method, final path is "+path);
+    	    // if first of character of path is @, the path is attribute
+    	    if (path.charAt(0) == '@')
+    	    {
+    	    	logMetacat.debug("it is attribute");
+    	    	isAttribute = true;
+    	    }
+    	}
+    	return isAttribute;
+    }
 
     /**
      * This method prints sql based upon the &lt;returnfield&gt; tag in the
Index: src/edu/ucsb/nceas/metacat/QueryTerm.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QueryTerm.java	(revision 3770)
+++ src/edu/ucsb/nceas/metacat/QueryTerm.java	(revision 3771)
@@ -303,7 +303,8 @@
                 }
                 else {
                     // without using XML Index; using nested statements instead
-                    self.append("AND parentnodeid IN ");
+                    //self.append("AND parentnodeid IN ");
+                	self.append("AND ");
                     self.append(useNestedStatements(pathexpr));
                 }
             }
@@ -335,8 +336,12 @@
     {
         log.info("useNestedStatements()");
         log.info("pathexpr: " + pathexpr);
+        String elementPrefix = " parentnodeid IN ";
+        String attributePrefix  =  " nodeid IN ";
+        boolean lastOneIsAttribute = false;
         StringBuffer nestedStmts = new StringBuffer();
         String path = pathexpr.trim();
+        String sql = "";
 
         if (path.indexOf('/') == 0)
         {
@@ -366,6 +371,11 @@
             {
                 // no and it's the last node
                 node = path;
+                if (node != null && node.indexOf(QuerySpecification.ATTRIBUTESYMBOL) != -1)
+                {
+                	lastOneIsAttribute = true;
+                	node = removeAttributeSymbol(node);
+                }
                 path = "";
             }
             else
@@ -426,9 +436,57 @@
             nestedStmts.append(") ");
         }
         while (!path.equals(""));
-
-        return nestedStmts.toString();
+        if (lastOneIsAttribute)
+        {
+        	sql = attributePrefix+nestedStmts.toString();
+        }
+        else
+        {
+        	sql = elementPrefix+nestedStmts.toString();
+        }
+        return sql;
     }
+    
+    
+    /*
+     * Removes @ symbol from path. For example, if path is @entity, entity will be returned.
+     * If path is entity, entity will be returned. 
+     */
+    private static String removeAttributeSymbol(String path)
+    {
+    	String newPath  ="";
+    	log.debug("Original string before removing @ is " + path);
+    	if (path != null)
+    	{
+    		
+    		int attribute = path.indexOf(QuerySpecification.ATTRIBUTESYMBOL);
+    		if (attribute != -1)
+    		{
+    			// has attribute symbol. Reomve it and return the remained part. 
+    			try
+    			{
+    		         newPath = path.substring(attribute + 1).trim();
+    			}
+    			catch (Exception e)
+    			{
+    				newPath = path;
+    			}
+    		}
+    		else
+    		{
+    			// doesn't have attribute symbol. Return original string
+    			newPath = path;
+    		}
+    	}
+    	else
+    	{
+    		// if is null, return null;
+    		newPath = path;
+    	}
+    	log.debug("String after removing @ is " + newPath);
+    	return newPath;
+    	
+    }
 
     /**
      * 
