Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 5095 invoked from network); 9 Apr 2003 15:02:04 -0000 Received: from atlsmtp.jacada.com (12.166.230.15) by daedalus.apache.org with SMTP; 9 Apr 2003 15:02:04 -0000 Subject: [JXPath] Question with 'name' attribute in Map element access To: commons-user@jakarta.apache.org X-Mailer: Lotus Notes Release 5.0.8 June 18, 2001 Message-ID: From: "Steve Pannier" Date: Wed, 9 Apr 2003 10:07:50 -0500 X-MIMETrack: Serialize by Router on Jacada_Atl_Smtp/CST(Release 5.0.8 |June 18, 2001) at 04/09/2003 10:52:34 AM MIME-Version: 1.0 Content-type: text/plain; charset=us-ascii X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N I'm trying to get a better understanding of how the "name" attribute works in Map element access. Let's say I have the following Map: {stuff=[{fruit=apple, color=Red}, {fruit=apple, color=Green}, {fruit=orange, color=Orange}, {fruit=banana, color=Yellow}, {fruit=grape, color=Green}]} (The test program showing this is below.) The expression "/stuff/fruit" results in: apple; Type: java.lang.String apple; Type: java.lang.String orange; Type: java.lang.String banana; Type: java.lang.String grape; Type: java.lang.String While the expression "/stuff[@name='fruit']" results in: apple; Type: java.lang.String I was expecting to see all "fruits" returned with the second expression. Am I wrong to expect this? The following is my test program: import java.util.*; import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.jxpath.JXPathException; public class TestMap8 { public TestMap8(String args[]) { } private void run() { MapHolder mh1 = new MapHolder(); JXPathContext context = JXPathContext.newContext(mh1.getDataMap()); getXPathValue(context, "/stuff/fruit"); getXPathValue(context, "/stuff[@name='fruit']"); System.out.println("\n MainMap: " + mh1.getDataMap()); } /** * Evaluate the given expression within the JXPath context and * display the results. */ private static Object getXPathValue(JXPathContext context, String expr) { java.util.Iterator iter; Object result = null; System.out.println("\n ****** Result of \"" + expr + "\":"); iter = (java.util.Iterator)context.iterate(expr); if (!iter.hasNext()) System.out.println(" EMPTY!!!"); else { while (iter.hasNext()) { result = iter.next(); System.out.println(" " + result + ";\tType: " + result.getClass().getName()); } } return result; } /** * */ public static void main(String args[]) { TestMap8 testMap = new TestMap8(args); testMap.run(); } public class MapHolder { private Map dataMap = null; public MapHolder() { // Populate map contents dataMap = new HashMap(); List l0 = new ArrayList(); dataMap.put("stuff", l0); Map m = new HashMap(); m.put("fruit", "apple"); m.put("color", "Red"); l0.add(m); m = new HashMap(); m.put("fruit", "apple"); m.put("color", "Green"); l0.add(m); m = new HashMap(); m.put("fruit", "orange"); m.put("color", "Orange"); l0.add(m); m = new HashMap(); m.put("fruit", "banana"); m.put("color", "Yellow"); l0.add(m); m = new HashMap(); m.put("fruit", "grape"); m.put("color", "Green"); l0.add(m); } public Map getDataMap() { return dataMap; } } } Regards. Steve Pannier Jacada, Inc. (763) 201-0002 Ext. 219 spannier@jacada.com http://www.jacada.com