jbobbins
jbobbins

Reputation: 1291

XML schema: How to change the maxLength of all strings in document

I want to set the schema-wide maxLength value of xsd:string in one place and have it apply anywhere xsd:string is used. I want to limit any and all strings to 50 characters, for example.

I know I can do this...

<xsd:simpleType name="MaxLen50">
  <xsd:restriction base="xsd:string">
    <xsd:maxLength value="50"/>
  </xsd:restriction>
</xsd:simpleType>
...
<xsd:element name="Foo" type="MaxLen50" />
<xsd:element name="Bar" type="MaxLen50" />
<xsd:element name="Baz" type="MaxLen50" />

...but the problem is that I would have to go to every string in the XSD and change it from xsd:string to MaxLen50. I just want to set the default for xsd:string and have it apply to all elements with type="xsd:string"

Upvotes: 1

Views: 2073

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

Personally, I think that defining a global limit of 50 characters on all strings is a pretty bad design choice. It takes one back to the days of punched cards.

There is no way for a user to change the semantics of built-in types like xs:string. You need to define a subtype, as you have suggested. And if you want to use the same subtype of xs:string everywhere, you are going to have to make the change everywhere. There are all sorts of ways you can automate the process of doing the global replace, but a global replace it will have to be.

Upvotes: 0

kjhughes
kjhughes

Reputation: 111541

You're confusing built-in and user-defined types.

Built-in/primitive types are meant to be the basic building blocks used to create user-defined types. You don't change the built-in types directly.

You have the right idea in defining a xsd:simpleType as a xsd:restriction of xsd:string and using that user-defined type where needed. Just don't bake the length into the name of the type. (Further, type names should be domain-based, not implementation-based.) That way you'll have a single place to adjust the 50 as requested.

Finally, note that a maintenance issue more worthy of your attention concerns the versioning of the XSD wrt existing documents, code, APIs, etc should you ever actually want to change that 50 in a published XSD.

Upvotes: 1

Related Questions