user1826936
user1826936

Reputation: 43

Why should XMLs have structure?

I have an XML schema that has been published to users. It has a fairly complex structure, but it also has some generic elements that allow us to add other data without breaking the published structure.

Example:

<record>
    <song>
        <name>thriller</name>
        <artist>Mike</artist>
        <genericData key="year">1980</genericData>
        <genericData key="duration">03:35</genericData>
    </song>
</record>

So here I added two genericData elements for year and duration.

We are being asked to add more data to our structure, and can probably use these genericData elements to meet those needs, but what is the drawback of doing this? I know it doesn't keep a relational model to the data (which is bad), but is there anything else will comeback to bite us? It's got a bad smell to me. I would prefer to add specific elements for the new data, but getting pushback about changing our schema.

Upvotes: 1

Views: 50

Answers (2)

Michael Kay
Michael Kay

Reputation: 163468

The "genericData" design pattern is seen quite often in XML, but in my view it's very rarely the best solution. I think one reason people use it is when they are using data-binding tools such as JAXB: these map the XML to data structures in a language like Java, and languages like Java can't handle the same level of flexibility as XML. Without the constraints of data binding (i.e. if you process the XML using tools designed for the job, like XSLT and XQuery), I would exploit the built-in flexibility of XML (either without a schema, or using wildcards in the schema), perhaps with namespaces to add a level of specification control: so

<record>
    <song>
        <name>thriller</name>
        <artist>Mike</artist>
        <m:year xmlns:m="http://me.com/ns">1980</m:year>
        <m:duration xmlns:m="http://me.com/ns">03:35</m:duration>
    </song>
</record>

Upvotes: 1

Vidya
Vidya

Reputation: 30320

I would say that the only compelling reason to use XML over considerably lighter formats like JSON or YAML is the schema definition and instance validation capability of XML. While there are exceptions like the JSON capabilities of Play Framework, you generally can't enforce a schema on these lighter formats. In fact, that can easily be seen as a plus depending on the use case. That's one of the selling points of "schema-less" NoSQL databases.

If you are relaxing your XML so much that the schema is almost incidental, that might be perfectly legitimate based on your use cases. But if that's so, you've lost the only compelling reason to use XML.

I would suggest making sure your "exceptional" cases of generic data are truly rare. There will be a natural inclination to relax the rules that you must resist. But if after careful consideration you feel a schema is too restrictive, then I would suggest abandoning XML altogether. Just know that you will be shifting the burden to your team to handle the logic that is otherwise available out of the box from XML.

Upvotes: 0

Related Questions