限制已在XSD中扩展的元素

时间:2014-09-02 17:12:33

标签: xsd

我正在尝试为要插入和更新的Web服务创建可重用的元素声明。我有以下声明:

<xs:complexType name="commonEntity">
  <xs:sequence>
    <xs:element name="id" type="snt-common:id" minOccurs="0"
                maxOccurs="1" />
    <xs:element name="createdByID" type="snt-common:id"
                minOccurs="0" maxOccurs="1" />
    <xs:element name="createDate" type="snt-common:dateTime"
                minOccurs="0" maxOccurs="1" />
    <xs:element name="modifiedByID" type="snt-common:id"
                minOccurs="0" maxOccurs="1" />
    <xs:element name="modifyDate" type="snt-common:dateTime"
                minOccurs="0" maxOccurs="1" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="contact">
  <xs:complexContent>
    <xs:extension base="snt-common:commonEntity">
      <xs:sequence>
        <xs:element name="firstName" type="snt-common:string255"
                    minOccurs="0" maxOccurs="1" />
        <xs:element name="lastName" type="snt-common:string255"
                    minOccurs="0" maxOccurs="1" />
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:element id="update" name="ContactUpdateRequest">
  <xs:complexType>
    <xs:complexContent>
      <xs:restriction base="snt:contact">
        <xs:sequence>
          <xs:element name="id" type="snt-common:id" minOccurs="1"
                      maxOccurs="1" />
          <xs:element name="modifyDate" type="snt-common:dateTime"
                      minOccurs="1" maxOccurs="1" />
          <xs:element name="firstName" type="snt-common:string255"
                      minOccurs="0" maxOccurs="1" />
          <xs:element name="lastName" type="snt-common:string255"
                      minOccurs="1" maxOccurs="1" />
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:element>

<xs:element id="insert" name="ContactInsertRequest">
  <xs:complexType>
    <xs:complexContent>
      <xs:restriction base="snt:contact">
        <xs:sequence>
          <xs:element name="firstName" type="snt-common:string255"
                      minOccurs="0" maxOccurs="1" />
          <xs:element name="lastName" type="snt-common:string255"
                      minOccurs="1" maxOccurs="1" />
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:element>

我想要实现的是创建插入和更新请求,这两者都是联系限制。依次联系是commonEntity的扩展,它用作所有模式的基础。

插入工作正常,因为没有其他限制,但是对于更新,我们需要请求中的id和modifyDate(这是我们强制执行版本控制的方式)。但是,验证架构时出现以下错误:

  

类型&#39;#AnonType_ContactUpdateRequest&#39;出错。该类型的粒子不是粒子的有效限制            基地。

好像我不能限制基本元素commonEntity中的元素。我尝试重新排序但没有成功。这是XSD的细微差别还是我错过了什么?

更新: common.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://predictivesolutions.com/schema/common"
    xmlns:snt-common="http://predictivesolutions.com/schema/common"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc" elementFormDefault="qualified"
    jaxb:version="2.0">

    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings>
                <xjc:javaType name="java.util.Calendar" xmlType="xs:dateTime"
                    adapter="com.ps.snt.ws.util.CalendarDateTimeAdapter" />
                <xjc:javaType name="java.util.Calendar" xmlType="xs:date"
                    adapter="com.ps.snt.ws.util.CalendarDateAdapter" />
            </jaxb:globalBindings>
        </xs:appinfo>
    </xs:annotation>

    <!-- Creating this id type to use in the event we need to change to a long, 
        we can just do it here. -->
    <xs:simpleType name="id">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="1"></xs:minInclusive>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="dateTime">
        <xs:restriction base="xs:dateTime">
            <!-- 2013-11-06T11:17:17.043-05:00 -->
            <xs:pattern value="\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\d[+\-]\d\d:\d\d" />
            <!-- 2013-11-06T11:17:17-05:00 -->
            <xs:pattern value="\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d[+\-]\d\d:\d\d" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="string255">
        <xs:restriction base="xs:string">
            <xs:maxLength value="255" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="commonEntity">
        <xs:sequence>
            <xs:element name="id" type="snt-common:id" minOccurs="0"
                maxOccurs="1" />
            <xs:element name="createdByID" type="snt-common:id"
                minOccurs="0" maxOccurs="1" />
            <xs:element name="createDate" type="snt-common:dateTime"
                minOccurs="0" maxOccurs="1" />
            <xs:element name="modifiedByID" type="snt-common:id"
                minOccurs="0" maxOccurs="1" />
            <xs:element name="modifyDate" type="snt-common:dateTime"
                minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

contact.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://predictivesolutions.com/schema/v1_1" xmlns:snt="http://predictivesolutions.com/schema/v1_1"
    xmlns:snt-common="http://predictivesolutions.com/schema/common"
    elementFormDefault="qualified">

    <xs:import namespace="http://predictivesolutions.com/schema/common"
        schemaLocation="../common.xsd" />

    <xs:complexType name="contact">
        <xs:complexContent>
            <xs:extension base="snt-common:commonEntity">
                <xs:sequence>
                    <xs:element name="firstName" type="snt-common:string255"
                        minOccurs="0" maxOccurs="1" />
                    <xs:element name="lastName" type="snt-common:string255"
                        minOccurs="0" maxOccurs="1" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <!-- For transporting data -->
    <xs:element id="contact" name="contact" type="snt:contact">
    </xs:element>

</xs:schema>

ContactService.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://predictivesolutions.com/schema/v1_1" xmlns:snt="http://predictivesolutions.com/schema/v1_1"
    xmlns:snt-common="http://predictivesolutions.com/schema/common"
    elementFormDefault="qualified">

    <xs:import namespace="http://predictivesolutions.com/schema/common"
        schemaLocation="../common.xsd" />

    <xs:include schemaLocation="contact.xsd" />

    <!-- Request/Response for inserting data -->
    <xs:element id="insert" name="ContactInsertRequest">
        <xs:complexType>
            <xs:complexContent>
                <xs:restriction base="snt:contact">
                    <xs:sequence>
                        <xs:element name="firstName" type="snt-common:string255"
                            minOccurs="0" maxOccurs="1" />
                        <xs:element name="lastName" type="snt-common:string255"
                            minOccurs="1" maxOccurs="1" />
                    </xs:sequence>
                </xs:restriction>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <!-- Request/Response for updating data -->
    <xs:element id="update" name="ContactUpdateRequest">
        <xs:complexType>
            <xs:complexContent>
                <xs:restriction base="snt:contact">
                    <xs:sequence>
                        <xs:element name="id" type="snt-common:id" minOccurs="1"
                            maxOccurs="1" />
                        <xs:element name="modifyDate" type="snt-common:dateTime"
                            minOccurs="1" maxOccurs="1" />
                        <xs:element name="firstName" type="snt-common:string255"
                            minOccurs="0" maxOccurs="1" />
                        <xs:element name="lastName" type="snt-common:string255"
                            minOccurs="1" maxOccurs="1" />
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

</xs:schema>

0 个答案:

没有答案