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?

1
  • 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, 2011 at 18:58

1 Answer 1

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>
1
  • 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, 2011 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.