XSD - Содержание недействительно. - PullRequest
2 голосов
/ 20 октября 2011

Я пытаюсь прочитать XSD-файл, используя Nokogiri Ruby Parser, и он выдает следующую ошибку Nokogiri :: XML :: SyntaxError (Элемент '{http://www.w3.org/2001/XMLSchema}element': Содержимое недействительно. Ожидается (аннотация ?, ((simpleType | complexType) ?, (unique | key | keyref) *)).):

Кто-нибудь знает, что не так с xsd?

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="company_donation_request" type="company_donation_requestType" />
  <xsd:complexType name="company_donation_requestType">
    <xsd:sequence>
      <xsd:element name="order" type="orderType"></xsd:element> 
      <xsd:element name="donation" type="donationType"></xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="donationType">
    <xsd:sequence>
      <xsd:element name="campaign_key" type="xsd:string" minOccurs="1" maxOccurs="1" >
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="2"/>
            <xsd:maxLength value="255"/>
        </xsd:restriction>
      </xsd:element>
      <xsd:element name="amount" type="xsd:decimal" minOccurs="1" maxOccurs="1" ></xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="orderType">
    <xsd:sequence>
      <xsd:element name="id" type="xsd:string" minOccurs="1" maxOccurs="1" >
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="2"/>
            <xsd:maxLength value="255"/>
        </xsd:restriction>
      </xsd:element>
      <xsd:element name="fulfillment_date" type="xsd:dateTime" minOccurs="1" maxOccurs="1" >
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="2"/>
            <xsd:maxLength value="255"/>
        </xsd:restriction>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

1 Ответ

3 голосов
/ 20 октября 2011

Вы получаете сообщение об ошибке, потому что xsd:restriction недопустимо в качестве дочернего элемента xsd:element.Попробуйте добавить xsd:restriction к xsd:simpleType, а затем укажите этот тип в xsd:element.

Вы могли бы добавить xsd:simpleType непосредственно к xsd:element, но, поскольку вы используете одно и то же ограничение 3 раза, имеет смысл поместить его в simpleType внеэлементы.

Вот пример.Я назвал простой тип "stackOverflowTest":

<xsd:schema elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="company_donation_request" type="company_donation_requestType" />
  <xsd:complexType name="company_donation_requestType">
    <xsd:sequence>
      <xsd:element name="order" type="orderType"></xsd:element> 
      <xsd:element name="donation" type="donationType"></xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="donationType">
    <xsd:sequence>
      <xsd:element name="campaign_key" type="stackOverflowTest" minOccurs="1" maxOccurs="1"/>
      <xsd:element name="amount" type="xsd:decimal" minOccurs="1" maxOccurs="1"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="orderType">
    <xsd:sequence>
      <xsd:element name="id" type="stackOverflowTest" minOccurs="1" maxOccurs="1"/>      
      <xsd:element name="fulfillment_date" type="stackOverflowTest" minOccurs="1" maxOccurs="1"/>      
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="stackOverflowTest">
    <xsd:restriction base="xsd:string">
      <xsd:minLength value="2"/>
      <xsd:maxLength value="255"/>
    </xsd:restriction>  
  </xsd:simpleType>
</xsd:schema>

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...