如何在 XML 架构中合并多个枚举?

How to merge multiple enumerations in XML Schema?

我可以将取自不同 *.xsd 文件的多个枚举组合成单个枚举,如下所示吗?

第一个文件内容:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified" targetNamespace="http://module1namespace"
            xmlns:tns="http://module1namespace">
    <xsd:simpleType name="LocalEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="LocalEnumValue1" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

第二个文件内容:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified" targetNamespace="http://module2namespace"
            xmlns:tns="http://module2namespace">
    <xsd:simpleType name="LocalEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="LocalEnumValue2" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

正在合并文件内容:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified" targetNamespace="http://controllernamespace"
            xmlns:tns="http://controllernamespace">
   <xsd:import namespace="module1namespace" schemaLocation="pathToFirstXsd"/>
   <xsd:import namespace="module2namespace" schemaLocation="pathToSecondXsd"/>
....
</xsd:schema>

等于:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified" targetNamespace="http://controllernamespace"
            xmlns:tns="http://controllernamespace">
   <xsd:simpleType name="GlobalEnum">
     <xsd:restriction base="xsd:string">
        <xsd:enumeration value="LocalEnumValue1" />
        <xsd:enumeration value="LocalEnumValue2" />
     </xsd:restriction>
   </xsd:simpleType>
</xsd:schema>

您可以通过 xsd:union.

组合两个枚举

这是一个完整的工作示例,它还修复了名称空间声明和引用的一致性:

Main.xsd

感谢 sergioFC 在这里使用@memberTypes 的好建议,这比将单独的 simpleType 子级合并更干净:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://controllernamespace"
            xmlns:m1="http://module1namespace"
            xmlns:m2="http://module2namespace">

  <xsd:import namespace="http://module1namespace" schemaLocation="1.xsd"/>
  <xsd:import namespace="http://module2namespace" schemaLocation="2.xsd"/>

  <xsd:simpleType name="GlobalEnum">
    <xsd:union memberTypes="m1:LocalEnum m2:LocalEnum"/>
  </xsd:simpleType>

</xsd:schema>

1.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://module1namespace">
  <xsd:simpleType name="LocalEnum">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="LocalEnumValue1" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

2.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://module2namespace">
  <xsd:simpleType name="LocalEnum">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="LocalEnumValue2" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>