Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web services API design: XML elements vs. attributes [duplicate]

I'm designing an API for a web service and I can't decide between using XML attributes, elements or a mixed architecture.

Let me show you an example. Let's assume I have an object called Domain. This model has 3 properties (tld, sld, trd and the name itself) and a method valid? that returns true if the domain is valid.

# I'm using ruby but 
# consider this as pseudo-code
class Domain
  attr_accessor :tld, :sld, :trd, :name

  def valid?
    true # force true
  end
end

My api called /domain/parser takes a domain in input and returns the parsed response. The first possibility is to use an element for every domain attribute.

<result>
  <domain>
    <name>www.google.it</name>
    <tld>it</tld>
    ...
    <valid>true</true>
  </domain>
</result>

But some interfaces use attributes.

<result>
  <domain tld="it" sld="google.com" trd="www" rule="*.foo" name="www.google.it" valid="true" />
</result>

And don't forget attributes and value.

<result>
  <domain tld="it" sld="google.com" trd="www" rule="*.foo" name="www.google.it" valid="true">
    www.google.it
  </domain>
</result>

In your opinion, which is the more powerful, flexible and expressive choice? Also, consider the response will be served in XML and JSON (soon).

like image 213
Simone Carletti Avatar asked Nov 30 '09 21:11

Simone Carletti


People also ask

What is the difference between attributes and elements in XML?

Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements.

Can attributes have multiple values in XML?

attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes)

How many attributes can an XML element have?

Rules for XML attributes XML element can have more than one attributes. This we have already seen in the above example, where brand and category attributes are linked to the element <car>. 3. Attributes cannot contain duplicate multiple values.

Can XML attributes be empty?

An element with no content is said to be empty. The two forms produce identical results in XML software (Readers, Parsers, Browsers). Empty elements can have attributes.


3 Answers

The pattern I use is:

  • elements are for data
  • attribute are for meta data (i.e. data about the data)

if you use XSD schema's most if not all of your meta data should go in there, but if you don't attributes are good place for it.

So with your example I might do something like this:

<result>
  <domain valid="true">
    <name>www.google.it</name>
    <tld>it</tld>
    ...
  </domain>
</result>
like image 126
dkackman Avatar answered Oct 16 '22 10:10

dkackman


WCF's designers chose to avoid attributes, mostly for performance reasons. The WCF default serializer, the DataContractSerializer, doesn't support attributes (so that might be something to take into consideration), but it's roughly 10% faster than the more flexible XmlSerializer in .NET.

So if you ever see yourself serving up content to be consumed by a WCF client, you will likely try to steer clear of attributes, if ever possible.

Attributes are only ever going to be "atomic", e.g. a string, an int etc. - elements offer much more flexibility that way. Also, attributes never exist on their own - they're always tacked onto an element.

From my personal experience and personal preference, I would most likely try to use mostly elements and avoid attributes as much as I can. But that's really just a personal preference and taste - attributes are absolutely valid in XML, no problem.

like image 5
marc_s Avatar answered Oct 16 '22 11:10

marc_s


It's mainly a matter of taste, but there are some technical considerations. Attributes are slightly more restricted in what characters they can contain. They have the advantage (?) that order is immaterial but they cannot be repeated. This may slightly influence your choice according to what toolset you have

like image 3
peter.murray.rust Avatar answered Oct 16 '22 10:10

peter.murray.rust