Sephie
Sephie

Reputation: 331

Origin of the "@attributes" element in JSON generated from XML

How and/or in which conventions are XML attributes put in an object named "@attributes" when converting XML to JSON? This style is used in this generic XML parser:

obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
  var attribute = xml.attributes.item(j);
  obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}

..to create JSON like this:

...
elem_array = [
  {
    "@attributes": {
      an-attribute: "",
      another-one: "mr.text"
    }
  }
]
...

I'm not looking for answers about element-centric vs. attribute-centric XML design, unless those things are more closely related to my question than I thought of course. ;)

Where did the @attributes notation come from and are there reasons to use it over using your own notation?

Thanks!

Upvotes: 2

Views: 455

Answers (1)

mplungjan
mplungjan

Reputation: 178285

it is likely evolved from the XPATH @attribute syntax

http://www.tizag.com/xmlTutorial/xpathattribute.php

Using the @ makes it recognisable to people used to XPATH

Example: XPath : Get nodes where child node contains an attribute

Upvotes: 2

Related Questions