XSD - 内容无效。

时间:2011-10-20 05:02:39

标签: xsd

我正在尝试使用Nokogiri Ruby解析器读取XSD文件,并抛出以下错误 Nokogiri :: XML :: SyntaxError(元素'{http://www.w3.org/2001/XMLSchema}element':内容无效。预期是(注释?,((simpleType | complexType)?,(唯一) | 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 个答案:

答案 0 :(得分:3)

您收到错误是因为xsd:restriction不允许xsd:element成为xsd:restriction的孩子。尝试将xsd:simpleType添加到xsd:element,然后在xsd:simpleType中指定该类型。

可以直接将xsd:element添加到<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> ,但由于您使用了相同的限制3次,因此将其放入simpleType更有意义在元素之外。

这是一个例子。我将simpleType命名为“stackOverflowTest”:

{{1}}

希望这有帮助。