Click here to Skip to main content
Click here to Skip to main content

XSD Tutorial - Part 2 of 5 - Conventions & Recommendations

, 3 Jul 2014
Rate this:
Please Sign up or sign in to vote.
This article gives a basic overview of the building blocks underlying XML Schemas.

XSD Tutorial Parts

  1. Elements and Attributes
  2. Conventions and Recommendations
  3. Extending Existing Types
  4. Namespaces
  5. Other Useful bits...

Introduction

This section covers conventions and recommendations when designing your schemas.

When to use Elements or Attributes

There is often some confusion over when to use an element or an attribute. Some people say that elements describe data and attributes describe the meta data. Another way to look at it is that attributes are used for small pieces of data such as order id's, but really, it is personal taste that dictates when to use an attribute. Generally it is best to use a child element if the information feels like data. Some of the problems with using attributes are:

  • Attributes cannot contain multiple values (child elements can)
  • Attributes are not easily expandable (to incorporate future changes to the schema)
  • Attributes cannot describe structures (child elements can)

lf you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.

Mixed Element Content

Mixed content is something you should try to avoid as much as possible. It is used heavily on the web in the form of XHTML, but that has many limitations. It is difficult to parse and it can lead to unforeseen complexity in the resulting data. XML Data Binding has limitations associated with it making it difficult to manipulate such documents.

Conventions

  • All Element and Attributes should use UCC camel case, eg (PostalAddress), avoid hyphens, spaces or other syntax.
  • Readability is more important than tag length. There is always a line to draw between document size and readability, wherever possible favor readability.
  • Try to avoid abbreviations and acronyms for element, attribute, and type names. Exceptions should be well known within your business area eg ID (Identifier), and POS (Point of Sale).
  • Postfix new types with the name 'Type'. eg AddressType, USAddressType.
  • Enumerations should use names not numbers, and the values should be UCC camel case.
  • Names should not include the name of the containing structure, eg CustomerName, should be Name within the sub element Customer.
  • Only produce complexTypes or simpleTypes for types that are likely to be re-used. If the structure only exists in one place, define it inline with an anonymous complexType.
  • Avoid the use of mixed content.
  • Only define root level elements if the element is capable of being the root element in an XML document.
  • Use consistent name space aliases
  • Try to think about versioning early on in your schema design. If it is important for a new versions of a schema to be backwardly compatible, then all additions to the schema should be optional. If it is important that existing products should be able to read newer versions of a given document, then consider adding any and anyAttribute entries to the end of your definitions. See Versioning recommendations.
  • Define a targetNamespace in your schema. This better identifies your schema and can make things easier to modularize and re-use.
  • Set elementFormDefault="qualified" in the schema element of your schema. This makes qualifying the namespaces in the resulting XML simpler (if not more verbose).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Simon Sprott
Software Developer (Senior) Liquid Technologies
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionWhat is mixed element content? PinmemberNathan Holt at CCEI25-Apr-07 11:00 
AnswerRe: What is mixed element content? PinmemberSprotty28-Apr-07 11:42 
QuestionA little more detail on some of the recommendations? PinmemberHerbCSO23-Apr-07 17:39 
I'm down with most of the recommendations (best practices are always wonderful to have - thanks for listing them!), but could you provide a little more detail on the following, please?
 
* Enumerations should use names not numbers, and the values should be UCC camel case.
Why?
 
* Only define root level elements if the element is capable of being the root element in an XML document.
When can an element not be a root element? How do we distinguish these?
 
* Use consistent name space aliases
o xml (defined in XML standard)
o xmlns (defined in Namespaces in XML standard)
o xs http://www.w3.org/2001/XMLSchema
o xsi http://www.w3.org/2001/XMLSchema-instance
What is the difference between these (if it's explained in one of the next parts (4?), then never mind... ;] )
 
* Set elementFormDefault="qualified" in the schema element of your schema. This makes qualifying the namespaces in the resulting XML simpler (if not more verbose).
How so?
 
--
Umm... what's a .sig? ;]

AnswerRe: A little more detail on some of the recommendations? PinmemberSprotty28-Apr-07 11:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 3 Jul 2014
Article Copyright 2007 by Simon Sprott
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid