根据XSD中元素的属性值来限制元素出现的次数?

时间:2018-09-13 15:00:25

标签: xml xsd schema

我正在研究基于XML的配置文件的架构。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Configuration">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Services">
          <xs:complexType>
            <xs:sequence>
              <!--
                Each service represents a specific web service. During the validation of the XML configuration
                against the schema an additional check for multiple occurrences of the same service must take place to ensure proper configuration.

                If support for more services is added increase maxOccurs accordingly. Currently supported are
                  * Service1
                  * Service2
                  * Service3
              -->
              <xs:element maxOccurs="3" name="Service">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="ConnectionStatusUsedFor">
                      <xs:complexType>
                        <xs:attribute name="Table" type="xs:string" use="required" />
                        <xs:attribute name="Column" type="xs:string" use="required" />
                        <xs:attribute name="Row" type="xs:unsignedByte" use="required" />
                      </xs:complexType>
                    </xs:element>
                    <!--
                      Used only by service of type DeviceService
                    -->
                    <xs:element minOccurs="0" maxOccurs="1" name="VersionInfo">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Application" type="xs:string" default="Ultimate Software" />
                          <xs:element name="OS" type="xs:string" default="Debian 9" />
                          <xs:element name="Manufacturer" type="xs:string" default="Some company" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="port" type="xs:unsignedShort" use="required" />
                  <!--
                    The URI is added as a path to the address where the respective service is made available for the
                    clients to connect to. The format is

                    http://127.0.0.1:[port]/[uri]

                    with [port] being the value taken from the port-attribute
                  -->
                  <xs:attribute name="uri" use="required">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:enumeration value="Service1.soap"/>
                        <xs:enumeration value="Service2.soap"/>
                        <xs:enumeration value="Service3.soap"/>
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:attribute>
                  <xs:attribute name="Timeout" type="xs:duration" use="required" />
                  <!--
                    The version of a service (see operation GetServiceVersion) defines which operations the service supports.
                    The limited range here is defined as the smallest and largest version number available among all
                    services. This also means that some services do not support a given higher version so additional
                    service-specific check must take place during the validation of the XML configuration against the
                    schema
                  -->
                  <xs:attribute name="version" use="required">
                    <xs:simpleType>
                      <xs:restriction base="xs:unsignedByte">
                        <xs:minInclusive value="0" />
                        <xs:maxInclusive value="5" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:attribute>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>

        <!-- rest of configuration -->

      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

以下是要针对XSD进行验证的XML文档的示例:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <Services>
    <!--
    Service:
      port    The port that we listen to for incomming data from a client
      uri     The path for the client to access the respective web service

      Note that all web services are hosted on localhost, which (given a
      port and uri) always results in http://127.0.0.1:<port>/<uri> as the
      service end point for the client to connect to

      version The version of the web service. Based on it's value (an integer)
    -->
    <Service port="8081" uri="HelloWorldService1.soap" Timeout="PT30S" version="5"/>
    <Service port="8085" uri="HelloWorldService2.soap" Timeout="PT30S" version="0"/>
    <Service port="8082" uri="HelloWorldService3.soap" Timeout="PT30S" version="1">
      <VersionInfo>
        <Application>My Software</Application>
        <OS>Debian 9</OS>
        <Manufacturer>Hello World</Manufacturer>
      </VersionInfo>
    </Service>
  </Services>

  <!-- rest of configuration -->

</Configuration>

现在的问题是,我想将Service元素保留为所添加的每个服务的通用元素,同时基于URI将实例限制为单个 / strong>。例如,以下操作(在当前的XSD中)应该是不可能的:

<Service port="8082" uri="HelloWorldService3.soap" Timeout="PT30S" version="0">
<Service port="8085" uri="HelloWorldService3.soap" Timeout="PT30S" version="1">

原因:两个uri元素的Service属性具有相同的值。其他属性可以(并且很合理)是相同的。

这可能吗?我可能会在某个时候将每种服务类型的元素分开,因为有些将包含在其他上下文中未使用的配置服务元素。

0 个答案:

没有答案