开发者

XML Schema - Restrict element to enum or keyref

开发者 https://www.devze.com 2023-03-28 20:42 出处:网络
I am trying to write a schema, and I want to restrict the value or an element to either an enumerated list, or to a key reference.Is this possible? ie, im my schema I have

I am trying to write a schema, and I want to restrict the value or an element to either an enumerated list, or to a key reference. Is this possible? ie, im my schema I have

   <xsd:simpleType name="TypeEnum">
      <xsd:restriction base="xsd:string">
   开发者_开发百科      <xsd:enumeration value="uint8" />
         <xsd:enumeration value="uint16" />
         <xsd:enumeration value="uint32" />
         <xsd:enumeration value="uint64" />
      </xsd:restriction>
   </xsd:simpleType>

and in my XML i have

<root>
   <a>anEnum<a> <!-- This value should be restricted to either anEnum, anotherEnum or anything in TypeEnum -->
   <AdditionalTypes>
      <Enum Name="anEnum" />
      <Enum Name="anotherEnum" />
   </AdditionalTypes>
</root>

My question is how do I structure the schema element definition for <a>

Thanks in advance for any help.


Create another named simple type that allows the rest of the enumerated values ("anEnum" and "anotherEnum") just like you created the "TypeEnum" type. Then use <xsd:union> to combine these types and set <a> to use this combined type.

Code example

<xsd:simpleType name="TypeEnum">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="uint8" />
        <xsd:enumeration value="uint16" />
        <xsd:enumeration value="uint32" />
        <xsd:enumeration value="uint64" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="TypeAdditionalEnum">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="anEnum" />
        <xsd:enumeration value="anotherEnum" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="TypeUnionEnum">
    <xsd:union memberTypes="TypeAdditionalEnum TypeEnum" />
</xsd:simpleType>

<xsd:element name="a" type="TypeUnionEnum" />
0

精彩评论

暂无评论...
验证码 换一张
取 消