File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Enforcing specific order of attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Enforcing specific order of attributes" Watch "Enforcing specific order of attributes" New topic

Enforcing specific order of attributes

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
With an XML Schema, is it possible to enforce a specific order of an element's attributes? If attributes are not in the specified order, them a validation error is generated.

For example, can I say:

<element attr="g" isdate="2" status="0"/>

The order is attr, isdate, status.

The following would generate validation error.

<element isdate="2" status="0" attr="g" />

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

No. In fact the XML Recommendation specifically says that the order of attributes in an element is not significant.

Edit: after letting that sit for a while, I thought it might be a bit hasty. After all you can use a schema to require all kinds of things which the XML Recommendation doesn't mention. So why not this one?

Here's why I think not. Since the Recommendation says that the order of attributes isn't significant, XML serializers can (and do) use this rule to output attributes in any order they see fit. Now suppose that a schema could override that and make the order significant. Then it would be necessary for the calling program to communicate that information to the serializer, perhaps by passing a reference to the schema to the serializer.

But I observe that XML serializers don't have that facility built into them. So that suggests to me that the committee who designed XML Schema wouldn't have allowed that feature to exist in Schema, because it would break all existing serializers.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Thanks. I thought so, but wanted to see if I was just unaware. For the application we are building, we need to enforce a specific order so that the flat files generated from the XML follow the defined format for each record.

We'll put a filter in place to extract the attibutes of the important elements and get them in the right order...(extra processing time.) Was just trying to see if this can be done via schema. Thanks again.
I agree. Here's the link:
subject: Enforcing specific order of attributes
It's not a secret anymore!