Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Default xml namespace and attributes

New Here ,
Jul 31, 2009 Jul 31, 2009

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

TOPICS
ActionScript
574
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 31, 2009 Jul 31, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 31, 2009 Jul 31, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 31, 2009 Jul 31, 2009
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines