views:

184

answers:

3

Is it possible to specify the value of a tag or attribute should not be like some_value ?

I have a strange requirement, where the xsd isn't aware of the values being sent to it. The value of that particular tag can be a string with any value except one value ( say data_migration).

The sender should be acknowledged with the error, if that particular value is sent.

Is it possible to specify this restriction?

+1  A: 

Use regular expressions to specify a pattern or as in your case what a pattern should not contain.

http://www.w3schools.com/schema/schema_facets.asp

Yaneeve
You cannot do that using a regular expression.
xcut
Please look at the chosen answer above by Jens Granlund to the contrary...
Yaneeve
I stand corrected, I did not know groups could be negated in the schema dialect.
xcut
Grr, SO won't let me remove the downvote, sorry :(
xcut
Thanks for that :)
Yaneeve
@Yaneeve, I suspected it's possible with regular expression, but didn't know the significance of '^' char, I mean the syntax, The link w3schools isn't having info on that .. That is the reason I didn't accept your answer.
infant programmer
Thanks for your feedback though :) next time I'll search for a better link...
Yaneeve
I think I should upvote you :-)
infant programmer
Thanks :) much appreciated
Yaneeve
+1  A: 

I don't know if you can specifically exclude a value. I'm not sure if this helps, but you can create two separate enumerations and then create the union of the enumerations.

<xsd:simpleType name="IncludedEnumType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="pending" />
        <xsd:enumeration value="in_process" />
        <xsd:enumeration value="failed" />
        <xsd:enumeration value="unknown" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ExcludedEnumType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="data_migration" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="CombinedEnumType">
    <xsd:union memberTypes="IncludedEnumType ExcludedEnumType" />
</xsd:simpleType>

You would use either IncludedEnumType or CombinedEnumType as necessary. Using the IncludedEnumType would obviously excluded the values in ExcludedEnumType.

This approach uses Solution 2 from this article by IBM.

Ryan
helpful but not in this case ;) so +1.
infant programmer
+1  A: 

I´m no expert on regex, but this simpleType makes everything starting with data_migration invalid.

<xs:simpleType name ="notDataMigration">
    <xs:restriction base ="xs:string">
      <xs:pattern value ="^(?!data_migration).*" />
    </xs:restriction>
  </xs:simpleType>
Jens Granlund