views:

122

answers:

1

Hello,

I'm working on some code generation for an existing project and I want to start from a xsd. So I can use tools as Xsd2Code / xsd.exe to generate the code and also the use the xsd to validate the xml. That part works without any problems.

I also want to translate some of the restrictions to DataAnnotations (enrich Xsd2Code). For example xs:minInclusive / xs:maxInclusive I can translate to a RangeAttribute.

But what to do with custom validation attributes that we created? Can I add custom facets / restrictions? And how? Or is there another solution / best practice.

I would like to collect everything in a single (xsd) file so that one file contains the structure of the class (model) including the validation (attributes) that has to be added.

<xs:element name="CertainValue">
  <xs:simpleType>
    <xs:restriction base="xs:double">
      <xs:minInclusive value="1" />
      <xs:maxInclusive value="100" />
      <xs_custom:customRule attribute="value" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>
A: 

XML schema is itself schema constrained, so you can't add an arbitrary (in its eyes) element. There is a facility for adding anything you like, xs:annotation/xs:appinfo, that's available for addition to most nodes. Perhaps try something like this:

<xs:element name="CertainValue">
<xs:simpleType>
  <xs:restriction base="xs:double">
    <xs:annotation>
      <xs:appinfo>
        <xs_custom:customRule attribute="value" />       
      </xs:appinfo>
    </xs:annotation>
    <xs:minInclusive value="1" />
    <xs:maxInclusive value="100" />
  </xs:restriction>
</xs:simpleType>

It depends on where Xsd2Code is looking for the stuff, but if they expect XSDs passed in to validate, appinfo is probably their only choice. Just which element to add the appinfo on it also of question.