What are the best practices around defining XML element lengths in schema? Should you strongly type the elements so they reflect the field length of the ultimate persistance for the data point or should you leave them undefined so as to better adapt to potential future changes in the length of the field as it is pe开发者_如何转开发rsisted (and used elsewhere through a system)? Is there a good rule of thumb when to do one versus the other for example strongly type for publicly exposed schemas and not for internally used?
The rule of thumb you have proposed sounds as reasonable as anything else to me. Really this will be a case by case matter, where the consumers of the schema (business people, internal and external developer etc...) need to agree upon its usage.
An externally facing schema is your contract with the people (and their computer systems) using this schema. So yes, you should as much as possible use the schema to express your expectations of incoming data - nothing is worse than being handed a schema which is riddled with xs:any elements!
If having the contents of an element greater than a given length will cause an error, then place that information in the schema.
However, in the real world you will often find situations where things need to change for one reason or another. Having the compromise of internal schemas with looser restrictions will save you some pain down the track. Particulally when mapping lots of external schemas to your internal schema it is very common to have a super set of requirements represented by the internal schema. Though of course that does mean that your internal error handling needs to be more robust.
I think there are two valid reasons for imposing a restriction on the length of a field.
(a) the restriction really exists in the real world - vehicle registrations in the UK are always 7 characters
(b) you want to protect a piece of software from input data that it can't handle. Personal names can exceed 40 characters, but you've got a legacy application that can't handle more than that, so you want to prevent longer names reaching it.
Don't impose a limit just because you think there should be one. By default, there shouldn't. We don't live in a world of punched cards any more.
精彩评论