0

My last Q: E4X select Nodes where descendants can be either A OR B or A && B was concerning how to query for multiple attribute values in E4X expressions, which @Patrick answered with this:

xml.Item.(descendants('ProductRange').(@id=="1" || @id=="2").length()>0);

Now the question is, how do we make the values dynamic using an array or string?

A bit like this, but this DOES NOT work:

var attributeValues:String = "@id==\"1\" || @id==\"2\" || @id==\"3\" || @id==\"4\"";
xml.Item.(descendants('ProductRange').(attributeValues).length()>0);

Many Thanks

Community
  • 1
  • 1
ukmikeb
  • 303
  • 1
  • 3
  • 15

1 Answers1

0

An array holding your values can be made for example and then using an indexOf search to find the id in it :

var xml:XML=<Items>
<Item name="aaa">
    <ProductRanges>
        <ProductRange id="1" />
    </ProductRanges>
</Item>
<Item name="bbb">
    <ProductRanges>
        <ProductRange id="2" />
    </ProductRanges>
</Item>
<Item name="ccc">
    <ProductRanges>
        <ProductRange id="1" />
        <ProductRange id="3" />
        <ProductRange id="2" />
    </ProductRanges>
</Item>
</Items>;

// you values filled from whatever source
var valuesToFind:Array=["1","2", "3"]; 

// search if @id exist into your values
// and unsure that there is any node returned
var res:XMLList=xml.Item.(descendants('ProductRange').(valuesToFind.indexOf(@id.toString())>=0).length()>0);
trace(res);
Patrick
  • 15,702
  • 1
  • 39
  • 39
  • and to confirm, this indexOf technique, won't falsely find the id value "1" in the value "14" ??? Seems to work OK for me. – ukmikeb May 29 '12 at 13:42
  • indexOf will just search through the array for the value you want, as there is no "14" in your values to find there is no pb. – Patrick May 29 '12 at 13:46
  • was just double checking that IF, you had a node it wouldn't be wrongly found by the search for "1" (because "1" is IN "14")... however it does seem fine, which is great! – ukmikeb May 29 '12 at 13:54
  • id="14", but array values are only "1", "2", "3" so everything is fine . indexOf will not search inside each string but only for each value. – Patrick May 29 '12 at 13:57