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?

1
  • 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. Commented Aug 1, 2011 at 12:32

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
For your XML xml.node.(attribute("foo") == 1) works fine. Did you tried something like xml.node.(attribute(attr) == 1)?
Or even xml.node.(attribute(attr) == value)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.