amit pal
amit pal

Reputation: 13

how to select xml element based on it attribute value start with "somthing" in actionscript3

say, I have an xml like as follows:

public static var keywords:XML = <keywords>
   <tag key="html" type="tag"/>
   <tag key="htmlNew" type="attr"/>
   <tag key="head" type="attr"/>
   <tag key="body" type="attr"/>
</keywords>;

I need to search this xml by attribute value. If user provides input as "html" then I need to return both <tag key="html" type="tag"/> <tag key="htmlNew" type="attr"/> node as XMLList. It kind of start with type searching by xml attribute. Please anybody provide any kind of solution or suggestion. for direct attribute matching I have used following code:

var closeMatchList:XMLList = xml.tag.(@key == "html") as XMLList;

It returns only <tag key="html" type="tag"/> tag

Is this kind of solution is possible?? please anybody provide any kind of solution. I am stuck with this problem for long time. thanks in advance.

Upvotes: 1

Views: 531

Answers (2)

Matt MacLean
Matt MacLean

Reputation: 19656

Without third party libraries you could also do this:

keywords.tag.(attribute("key").indexOf("html")==0)

Upvotes: 4

Tomalak
Tomalak

Reputation: 338326

There is an XPath library for ActionScript3 (xpath-as3) that can do this easily.

public static var keywords:XML = ...;

var thePath:String = "/keywords/tag[starts-with(@key, 'html')]";
var html:NodeSet = XPath.evaluate(thePath, keywords);  

Some other ways to use the library can be seen over here.

Upvotes: 0

Related Questions