views:

197

answers:

3

I am trying to parse a document with nodes that look something like this:

<doc>
<results>
        <result xmlns="http://www.inktomi.com/"&gt;
            <title>Senate Panel to Review Election Unit Sale</title>
        </result>
</result>
</doc>

However the namespace and the nodename of the result could be different. If it were not so, this would work:

results..*::title //>Senate Panel to ...

but doing this doesnt:

var myvar = "title"
results..*::[myvar]

any clues?

A: 

Apparently the square bracket way of accessing children and use of * to select any namespace doesn't work together

var doc:XML = 
<doc> 
    <results> 
        <result xmlns="http://www.inktomi.com/"&gt;  
            <title>Senate Panel to Review Election Unit Sale</title> 
        </result>
    </results>
</doc>;
var ns:Namespace = new Namespace("http://www.inktomi.com/");
trace(doc..*::title.toXMLString()); //These three 
trace(doc.results.*::result);       //lines compile
trace(doc.results.ns::["result"]);  //and run as expected
//This commented out line compiles but throws 2 verify errors in the run time
//trace(doc.results.*::["result"]);

VerifyError: Error #1080: Illegal value for namespace.
ReferenceError: Error #1065: Variable Test is not defined.

The VerifyError class represents an error that occurs when a malformed or corrupted SWF file is encountered.

Amarghosh
A: 

Not an E4X solution, but you could iterate through all available namespaces returned by xml.namespaceDeclarations() then either get the first child or use square brackets to access it.

You could also just pre-parse the xml and make all the namespaces the same as a quick fix.

Ian
+1  A: 

So the correct solution apparently is:

var myvar = "title"
var ans = results..*.(localName()==myvar);

Thanks to @xtyler on Twitter for finding the answer

Arpit
That's a nice one.
Amarghosh