You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Steve Pannier <St...@jacada.com> on 2003/04/09 17:07:50 UTC
[JXPath] Question with 'name' attribute in Map element access
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
Re: [JXPath] Question with 'name' attribute in Map element access
Posted by Dmitri Plotnikov <dm...@apache.org>.
Steve,
I fixed the PredicateContext class to handle the collection of maps (or
beans for that matter) properly. The fix will be available as of tonight's
(May 6, 2003) build.
Thank you very much for reporting the problem.
- Dmitri
----- Original Message -----
From: "Steve Pannier" <St...@jacada.com>
To: <co...@jakarta.apache.org>
Sent: Wednesday, April 09, 2003 11:07 AM
Subject: [JXPath] Question with 'name' attribute in Map element access
> 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
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>
Re: [JXPath] Question with 'name' attribute in Map element access
Posted by Dmitri Plotnikov <dp...@yahoo.com>.
Steve,
I thnik you are right expecting all of the fruits to be returned by the
iterator. I'll look into that.
Thank you for the report.
- Dmitri
--- Steve Pannier <St...@jacada.com> wrote:
> 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
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com