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).
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.
attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes)
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.
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.
The pattern I use is:
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>
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With