1

Using e4x in flex:

var attr : String = "foo";
var xml : XML = 
    <resultSet>
       <node foo="1"/>
    </resultSet>;

How can I use the variable "attr" to access

xml.node.@foo 

I thought I could do it with

xml.node.@[attr]

But this doesn't seem to work. How can I access this attribute by a dynamic value like this?

EDIT: Both

xml.node.@[attr];

and

xml.node.attribute(attr);

work, as Constantiner suggested.

Updates:

Say I have an XMLList in this form:

var bar:XML = 
    <resultSet>
         <node>value</node>
    </resultSet>;

I want to filter the original xml above by matching "foo" attributes with the "value" from node in bar.

Essentially I want a sublist of the original xml such that

xml.node.@foo == bar.value 

for each xml row in the original value

As Constantiner mentioned, I can filter the original list by the value in foo, but what if I want to filter on multiple values?

Can I do something like:

xml.node.(bar.node.contains(attribute(foo)) ? attribute(foo) : null);

Or perhaps a cleaner method instead of the null?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
pclem12
  • 449
  • 10
  • 23
  • trace(xml.node.@[attr]) returns "1" for me, but it's cleaner (in my opinion) to follow Constantiner's answer and pass the string as a parameter to the attribute method. – Chunky Chunk Aug 01 '11 at 12:32

1 Answers1

2

Try to use xml.node.attribute(attr).

Constantiner
  • 14,231
  • 4
  • 27
  • 34
  • Thanks, turns out both the xml.node.attribute and xml.node.@[attribute] work for a single node, but what about for an e4x filter? like if I want to get an XMLList of just the nodes matching a certain attribute? xml.node.(@[attribute] == value) doesn't seem to work? – pclem12 Aug 01 '11 at 12:47
  • For your XML `xml.node.(attribute("foo") == 1)` works fine. Did you tried something like `xml.node.(attribute(attr) == 1)`? – Constantiner Aug 01 '11 at 12:53
  • Or even `xml.node.(attribute(attr) == value)`? – Constantiner Aug 01 '11 at 12:54