1

I am looking for a way to have an attribute of type xs:string with a minimum length.

I have found a way to do it here, but the solution involves introducing a new type.

As i am using XML mapping/binding, it results in a shiny new class (cluttering the code with its presence as well as additional method calls) which is totally useless - it is still just a plain old String.

Is there a way to avoid introducing the custom type?

Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224
  • Filburt, I'm truly sorry for my long AFK. It was for a reason though. I know how it feels when your contribution is ignored, so please excuse the inconvenience. – kostja Feb 14 '11 at 18:58

1 Answers1

4

You shouldn't need to declare a new (named) type - just put the restriction inside your attribute definition:

<xs:element name="foo">
    <xs:complexType>
        <xs:attribute name="bar">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:minLength value="5" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Thank you, Filburt. This way I do not need to introduce a new type. Unfortunately, when generating JAXB classes (XML binding for Java), the restriction remains only in the comments and is not enforced in the code itself, so un/marshalling an invalid object still works. But violations are recognized during validation, so there is a way to to enforce restrictions without tweaking the the code itself after generation (which is a pain as the code is generated during each build). – kostja Feb 14 '11 at 18:54