简体   繁体   English

XML 架构 - 将元素限制为枚举或 keyref

[英]XML Schema - Restrict element to enum or keyref

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在我的 XML 我有

<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>我的问题是如何构造<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.创建另一个命名简单类型,它允许枚举值(“anEnum”和“anotherEnum”)的 rest,就像您创建“TypeEnum”类型一样。 Then use <xsd:union> to combine these types and set <a> to use this combined type.然后使用<xsd:union>组合这些类型并设置<a>以使用这种组合类型。

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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM