Finding nodes with a namespaced attribute using e4x

When digging through XML, e4x is definitely your friend but it can be tricky sometimes. I recently came across a situation where I needed to find all nodes with a certain namespaced attribute. I didn’t care what the attribute’s value was; I just needed to know which nodes had the attribute.

Take the following XML:

<catalog xmlns:ah="http://aaronhardy.com/music">
<cd>
<title>Empire Burlesque</title>
<artist ah:verified="false">Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title ah:verified="true">Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd ah:verified="false">
<title ah:verified="true">Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country ah:verified="false">EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>

In this case I’m trying to find all nodes that have a “verified” attribute. Again, I don’t care if the attribute’s value is true or false. If the attribute weren’t in the “ah” namespace I would use this line of code:

var nodes:XMLList = myXML..*.(hasOwnProperty('@verified'));

However, since the attribute is in the “ah” namespace, the query takes this form:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="cc()">
<mx:Script>
<![CDATA[
protected function cc():void
{
var myXML:XML =
<catalog xmlns:ah="http://aaronhardy.com/music">
<cd>
<title>Empire Burlesque</title>
<artist ah:verified="false">Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title ah:verified="true">Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd ah:verified="false">
<title ah:verified="true">Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country ah:verified="false">EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>;

var ns_ah:Namespace = new Namespace('http://aaronhardy.com/music');
var verified:QName = new QName(ns_ah, '@verified');
var nodes:XMLList = myXML..*.(hasOwnProperty(verified));
trace(nodes.toXMLString());
}
]]>
</mx:Script>
</mx:Application>

Hope that helps someone!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.