tags:

views:

117

answers:

1

Hi, i have the following piece of xml:

<MyField>
    <FieldName>Blah</FieldName>
    <ValueFormatting xsi:type="DateFormatter">
        <Format>dd/MM/yy</Format>
    </ValueFormatting>
</MyField>

in my xsd, how can i limit or restrict the values that are supplied for the xsi:type attribute on the ValueFormatting element, as i have a list of four or five types that are valid (i.e. TextFormatter, NumberFormatter, DateFormatter, etc.)?

Also, in my xsd, how can i enforce that the attribute name is "xsi:type"? Is it correct that I could probably get away with having an attribute name of "type" instead, but then i could be risking a collision if "type" is declared in other namespaces?

Thanks!

+1  A: 

To limit the allowed values for the "type" attribute, use tags within your "type" attribute's XSD definition.

As for the attribute name itself, the XML needs to define a namespace (default or otherwise) that uses a given prefix, and then the XSD needs to specify that same namespace in the "targetNamespace" attribute of the "type" attribute's definition. You can't force the XML to use the "xsi" prefix specifically (in fact, the "xsi" prefix is reserved anyway), but you can force which namespace it points to to make sure the XML is using your "type" attribute and not someone else's.

For example:

<xsd:element name="ValueFormatting">
  <xsd:complexType>
    <xsd:attribute name="type" minOccurs="1" maxOccurs="1" targetNamespace="http://..."&gt;
      <xsd:simpleType>
        <xsd:restriction base="xsd:string">
          <xsd:enumeration value="TextFormatter" />
          <xsd:enumeration value="NumberFormatter" />
          <xsd:enumeration value="DateFormatter" />
        </xsd:restriction>
      </xsd:simpleType>
    </xsd:attribute>
    ...
  </xsd:complexType>
</xsd:element> 

<MyField> 
    <FieldName>Blah</FieldName> 
    <ValueFormatting xmlns:myns="http://..." myns:type="DateFormatter"> 
        <Format>dd/MM/yy</Format> 
    </ValueFormatting> 
</MyField>
Remy Lebeau - TeamB