commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitri Plotnikov <dplot...@yahoo.com>
Subject Re: [JXPath] Problem with maps and variables
Date Tue, 14 Jan 2003 22:52:28 GMT
Steve,

The explanation for the seemingly inconsistent behavior of JXPath is
quite simple. 

The value for the key "Count" in your example is not really an integer,
but a list containing an integer.  That makes a difference.  According
to the XPath specification, the predicate (the expression in brackets)
is used differently for different types.  If the value of the
expression is a number, it is used as an index.  If it is not a number,
it is converted to a boolean.  The nodes for which it evaluates to true
are included in the resulting collection. In our case the value of the
expression is a non-empty list, so it evaluates to true for every
element of the collection. This is why every element is included in the
resulting list.

There are two solutions to the problem:

1. Put a number in the map, instead of a list
2. Change the xpath like this:
"$map/dataMap/Foo/Bar[$map/dataMap/Foo/Count[1]]".

I hope this helps.

- Dmitri


--- Steve Pannier <Steve_Pannier.CST@jacada.com> wrote:
> I am having some problems using JXPath with Maps and variables.
> I have one or more maps that I add as variables to a global context.
> When I retrieve a value from a map containing repeating data using a
> hard-coded index in the expression, it works OK as follows:
> 
>   Iterator iter =
> (Iterator)context.iterate("$map/dataMap/Foo/Bar[3]");
> 
> If I replace the hard-coded index value with a simple Integer-type
> variable,
> it also works:
> 
>   context.getVariables.declareVariable("index", new Integer(3));
>   Iterator iter = (Iterator)context.iterate("$map/dataMap/Foo/Bar
> [$index]");
> 
> But, if I use a variable that points to a map containing an Integer
> value
> as the index, I don't get the expected results.
> 
>   Iterator iter = (Iterator)context.iterate("$map/dataMap/Foo/Bar
> [$map/dataMap/Foo/Count]");
> 
> It seems to return the entire contents of "$map/dataMap/Foo/Bar".
> Furthermore, if I evaluate the expression "$map/dataMap/Foo/Count",
> the result is 3 of type Integer, as I would expect.
> 
> Has anyone else seen any problems in this area?
> 
> The following is a simple program showing the above problems.
> 
> 
> import java.util.*;
> import org.apache.commons.jxpath.JXPathContext;
> import org.apache.commons.jxpath.JXPathException;
> 
> public class TestMap3 {
> 
>     public TestMap3(String args[]) {
>     }
> 
>     private void run() {
>         MapHolder mh1 = new MapHolder();
>         JXPathContext context = JXPathContext.newContext(null, null);
>         context.getVariables().declareVariable("map1", mh1);
>         context.getVariables().declareVariable("index", new
> Integer(2));
> 
>         // Hard-coding array index works OK
>         getXPathValue(context, "$map1/dataMap/Foo/Dog[2]");
> 
>         // Using a simple Integer variable as the index works OK
>         getXPathValue(context, "$map1/dataMap/Foo/Dog[$index]");
> 
>         // 1st problem: If Foo/Count is 2, this expression should
> return
> one
>         // value only ("St. Bernard"), but instead returns entire
> list of
> dogs.
>         // (In fact, no matter what Foo/Count is (1, 2, or 299) it
> always
> returns
>         // entire list of dogs.)
>         getXPathValue(context, "$map1/dataMap/Foo/Count");  // verify
> index
> value
>         getXPathValue(context, "$map1/dataMap/Foo/Dog
> [$map1/dataMap/Foo/Count]");
> 
>         // Another related problem: Use getValue(): No matter what
> the
> value of
>         // Foo/Count is, it always returns 1st dog ("Shepard")
>         Object result = context.getValue("$map1/dataMap/Foo/Dog
> [$map1/dataMap/Foo/Count]");
>         System.out.println("\n   ****** Result using getValue():");
>         System.out.println("        " + result + ";\tType: " +
> result.getClass().getName());
> 
>         System.out.println("\n MainMap: " + mh1.getDataMap());
>     }
> 
>     /**
>      * Evaluate the given expression within the JXPath context and
>      * display the results.
>      *
>      * Returns the result object (if more than one, returns last one)
>      */
>     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[]) {
>         TestMap3 testMap = new TestMap3(args);
>         testMap.run();
>     }
> 
> 
> public class MapHolder {
>     private Map dataMap = null;
> 
>     public MapHolder() {
>         // Populate map contents
>         dataMap = new HashMap();
>         List l0 = new ArrayList();
>         dataMap.put("Foo", l0);
> 
>         Map m = new HashMap();
>         List l = new ArrayList();
>         m.put("Count", l);
>         l.add(new Integer(299));
>         l0.add(m);
> 
>         m = new HashMap();
>         l = new ArrayList();
>         m.put("Dog", l);
>         l.add("Shepard");
>         l.add("St. Bernard");
>         l0.add(m);
>     }
> 
>     public Map getDataMap() {
>         return dataMap;
>     }
> }
> 
> }
> 
> 
> 
> Steve Pannier
> Jacada, Inc.
> (763) 201-0002 Ext. 219
> spannier@jacada.com
> http://www.jacada.com
> 
> 
> 
> --
> To unsubscribe, e-mail:  
> <mailto:commons-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:commons-user-help@jakarta.apache.org>
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

Mime
View raw message