Lance
Lance

Reputation: 5800

XSD attribute cannot equal value?

How can I restrict an attribute to not allow a particular value?

For instance, I have an <xs:key/> on an Id attribute of an element, but some yahoo has gone and thrown a magic number in there so now I cannot allow 3, but 1, 2, and 5 are perfectly acceptable.

Upvotes: 3

Views: 1241

Answers (4)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

If your IDs are integers, and you want to allow any integer except 3, your best bet (it seems to me) is not regexes on patterns, because as other commentors have observed it's easy to leave holes accidentally, but to define a subtype of integers for integers less than 3, and another for integers greater than tree, and then define their union. Your attribute gets the union type. And voila, you accept any integer but 3. All you have to do is hunt down the person who make 3 magic, and ensure that they don't add more magic numbers.

In XSD 1.1, you could also get around this awkwardness by adding an assertion that constrains the value to be not equal to 3.

Upvotes: 0

xan
xan

Reputation: 7731

I think a better pattern is [^3]|..+ which accepts all numbers with at least 2 digits. However, it still allows 03 which is another representation of 3. To disallow that, you can try something like [^3]|[^0].+ which disallows all representations with leading zeros.

Upvotes: 0

Meberem
Meberem

Reputation: 947

Have you tried having a look at the restriction tag link text, the code below, it should give you a tag called Id which only allows integers that doesn't start with a 3

<xs:key name="Id">
    <xs:simpleType>
        <xs:restriction base="xs:integer">
            <xs:pattern value="[^3]\d*"/>
       </xs:restriction>
  </xs:simpleType>
</xs:key>

Upvotes: 4

Aravind Yarram
Aravind Yarram

Reputation: 80176

You can specify restrictions using regular expressions. Take a look at this tutorial

Upvotes: 0

Related Questions