3

Can someone explain me what exactly namespaces (xmlns="...") in XML are for and how they have to be used in navigating an XML using E4X (..preferrably in ActionScript 3)?

I fail to fully understand their purpose and usage.

Mat
  • 4,281
  • 9
  • 44
  • 66

2 Answers2

1

In theory, XML namespaces are used to avoid conflict with tag names. So I can create a namespace that contains a tag named "mytag" and someone else creates a different namespace with the same tag "mytag" and there won't be any conflict. Each "mytag" tag will be clearly differentiated.

In practice, I found that XML namespaces are pretty much useless (how often do you have two developers creating tag for the same XML file?) and just make the parsing annoyingly difficult.

In EX4, you'll need to get the namespace, then prefix each tag with that namespace. So, for example, if you want to access the tag <somenamespace::somename> you will write:

var somenamespace:Namespace = xml.namespace("somenamespace");
var sometag:XML = somenamespace::somename[0];

If you search on Google, you'll find some AS3 classes that remove these useless namespaces from XML to make parsing easier.

laurent
  • 88,262
  • 77
  • 290
  • 428
  • alright - now that clarifies why i wasn't able to access the data in my nodes! thank you! – Mat Nov 10 '11 at 14:48
  • Keep in mind that each XML document has a deault namespace which is the one without a prefix. If you are working with a well difined XML structure that can be verified using an XSD file, and you can't modify the XSD file (e.g. for MathML) the only way to "extend" the XML is to introduce your own elements in a separate namespace that you own. (Each XSD can define where such extensoins are allowed, e.g. the app XML files used in the AIR SDK do not allow this.) – karfau Apr 28 '17 at 10:57
1

Many people point to James Clark's explanation as one of the clearest:

http://www.jclark.com/xml/xmlns.htm

Michael Kay
  • 156,231
  • 11
  • 92
  • 164