XML Schema : Elements vs. Attributes

It is fairly common to encounter a task to convert data from a legacy format to XML.
Defining the XML schema is one part of the problem. Let’s assume that you want to convert some metadata originally defined in Compact NodeType Definition(CND) used in Java Content Repository 1.x/2.x specification.
Here is an example of such data type:

  - rsr:type (STRING) mandatory
  - rsr:releasenotes (STRING) mandatory

This weird looking section of code actually defines a type called “rsr:distfile” with two attributes “rsr:type” and “rsr: releasenotes”.

A one-to-one attribute to attribute mapping will lead to this snippet of XML schema,

<xs:complexType name="distfile">
        <xs:attribute name="type" type="xs:string"/>
        <xs:attribute name=" releasenotes " type="xs:string"/>

However, this simple solution can cause nightmares, because – both the attributes are of string type, and, therefore, can hold any random string in the legacy store. You may have to URL escape the string content before adding them as XML attributes.

On the other hand, attribute-to-element mapping for the above case produces much cleaner XML.
Here is the new schema snippet:

<xs:complexType name="distfile">
<xs:element maxOccurs="1" minOccurs="1" name="type" type="xs:string"/>
<xs:element maxOccurs="1" minOccurs="1" name=" releasenotes " type="xs:string"/>        

You now also have an option to treat the string content as CDATA.
Here is an example XML produced out of the above schema.

<distfile name="copyservice.wsdl">
      < releasenotes ><[!CDATA[
1.2.4 Added a new element  “<error>”
1.2.5 Removed  element  “<error>”
    </ releasenotes >

About Amar Deka

Software Engineer
This entry was posted in Open Source, Technology, Thoughts. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s