Is xs: selection the equivalent of a C ++ enumeration?

We have a lot of serialization done through MS XML 4. When we serialize C ++ enums, we use a table to translate each possible value into a string, and they save that string as an attribute value. When we deserialize, we read this attribute value, compare it with all the elements in the table, and extract the corresponding enumeration value. If we cannot find a mistake, we will make a mistake.

To facilitate the creation of XML files by external programs, we have published XML schemas for all data types of interest. Attributes for enumerations are defined as follows:

<xs:complexType>
    //other fields here
    <xs:attribute name="Color" type="xs:string"></xs:attribute>
</xs:complexType>

It works, but does not contain definitions for possible string values. How can I add possible values ​​to this definition? I am using xs: choice for this?

+3
source share
2 answers

No, it xs:choiceprovides a diagram with information that states: "At this point you can have one or another, but not a combination"; You can learn more about xs:choicehere .

To create an enumeration, you need to define them as part of a limited type based on xs:string.

For example:

<xs:simpleType name="ColorType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="white"/>
    <xs:enumeration value="black"/>
    <xs:enumeration value="blue"/>
  </xs:restriction>
</xs:simpleType>

Then you can use this type like any other:

<xs:complexType>
  <xs:attribute name="Color" type="ColorType" />
</xs:complexType>

xs:restriction XSD www.w3schools.com. , , XHTML, XSLT, XPath XSD ( javascript AJAX).

+3
<xs:complexType>
  //other fields here
  <xs:attribute name="Color">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="RED"/>
        <xs:enumeration value="BLUE"/>
        <xs:enumeration value="GREEN"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
</xs:complexType>

:

<xs:complexType>
  //other fields here
  <xs:attribute name="Color" type="Color"/>
</xs:complexType>
<xs:simpleType name="Color">
  <xs:restriction base="xs:string">
    <xs:enumeration value="RED"/>
    <xs:enumeration value="BLUE"/>
    <xs:enumeration value="GREEN"/>
  </xs:restriction>
</xs:simpleType>

<xs:choice> - . XML- . .

0

Source: https://habr.com/ru/post/1710676/


All Articles