Default xml namespace and attributes
Copy link to clipboard
Copied
Hi,
I was trying to use default xml namespaces, but came upon something strange.
I got the following piece of xml:
<animal type="sheep" xmlns="http://blabla">
<name>josh</name>
<age>2</age>
</animal>
and the following code
public static var ns:Namespace = new Namespace("http://blabla");
var xml:XML; // xml will be read into this var, not going into details here
public function myfunc():void {
default xml namespace = ns;
trace( xml.animal.@type );
}
now for some reason, the elements are correctly traversed using the default namespace. But the attribute isn't found. After a bit of debugging, I found that I could only retrieve the attribute when using xml.animal.attribute(new QName("", "type")). Am I doing something wrong here? Why isn't the attribute in the same default namespace as the element?
Kind regards,
Jan

Copy link to clipboard
Copied
The attribute should be in the element's namespace. If I run this snippet on the timeline, it outputs the correct values.
var xml:XML =
<xml>
<animal type="sheep" xmlns="http://blabla">
<name>josh</name>
<age>2</age>
</animal>
<animal type="cow" xmlns="http://blabla">
<name>jim</name>
<age>2</age>
</animal>
</xml>
var ns:Namespace = new Namespace("http://blabla");
default xml namespace = ns;
trace( xml.animal.@type ); // OUTPUT: sheepcow
Copy link to clipboard
Copied
Hi,
thanks for trying this out. I was kinda stumped that it worked with your example, but then realised I simplified the example a bit too much. I was actually using another way to access the attribute because it had dashes in them, it should have been:
trace( xml.animal.@["type"] );
after some more playing around I found that using animal.@type is ok, as well as animal["@type"], but the one I originally chose animal.@["type"] seems to differ somehow. This does help me a lot, knowing that I can use animal["@type"], but any clue why the other one doesn't work?
trace( xmlTmp.animal.@type ); // OUTPUT: sheepcow
trace( xmlTmp.animal["@type"]); // OUTPUT: sheepcow
trace( xmlTmp.animal.@["type"]); // OUTPUT: nothing?
Regards

Copy link to clipboard
Copied
Sorry, no ideas. I've never used the syntax, but according to all documentation I've seen it should work.

