【问题标题】:s4s-elt-invalid-content.1: Content is invalid. Element 'attribute' is invalid, misplaced, or occurs too oftens4s-elt-invalid-content.1:内容无效。元素“属性”无效、放错位置或过于频繁地出现
【发布时间】:2017-05-16 18:01:45
【问题描述】:

我正在为一个项目开发 XSD,这就是我所拥有的:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.example.org/Elizabeth schema" 
xmlns:elz="http://www.example.org/Elizabeth_schema" elementFormDefault="qualified">
<xs:element name ="year">
<xs:complexType mixed = "true">
<xs:sequence>
<xs:element name="entry">
    <xs:complexType mixed ="true">
    <xs:simpleContent>
        <xs:attribute name ="when" type = "xs:string"/> 
        <xs:attribute name = "place" type = "xs:string"/>
        <xs:element name = "items" type = "xs:string"/>
        <xs:element name = "characters">
            <xs:complexType>
            <xs:attribute name ="character" type = "xs:string"/>
            </xs:complexType>
        </xs:element>
        <xs:element name = "eventscollection">
            <xs:complexType>
            <xs:attribute name = "type" type = "xs:string"/>
            </xs:complexType>
        </xs:element>
        <xs:element name ="entrytxt">
            <xs:complexType>
            <xs:any minOccurs = "0"/>
            <xs:anyAttribute minOccurs="0"/>
            </xs:complexType>
        </xs:element>
        </xs:simpleContent>
    </xs:complexType>
    </xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

我还没有将 xls 指向架构,因为我想让架构首先工作。我的 IDE 向我抛出了一个错误:

s4s-elt-invalid-content.1: '#AnonType_entryyear' 的内容是 无效的。元素“属性”无效、放错位置或也出现 经常。

第 11 行是我在元素“entry”下引入第一个属性的地方。我发现如果我删除第 10 行(“simpleContent”),我会得到相同的错误,但在第 13 行我在“条目”下引入了第一个元素。

我是这方面的初学者,我在互联网上做了一些刺激,但似乎无法弄清楚我的代码是怎么回事。有什么想法吗?

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案1】:

    您的 XSD 存在许多问题,包括

    • entry 没有简单的内容。将xs:simpleContent 更改为 xs:sequencexs:complexType 内。
    • 然后,将 xs:attribute 声明移到 xs:sequence 之外。
    • xs:anyAttribute 中删除minOccurs

    请注意,您可能希望删除属性 = 符号周围的间距 - 不是必需的,但它看起来相当不合常规,并且与您所拥有的不一致。

    这是完全更正的 XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               targetNamespace="http://www.example.org/Elizabeth schema"
               xmlns:elz="http://www.example.org/Elizabeth_schema" 
               elementFormDefault="qualified">
      <xs:element name="year">
        <xs:complexType mixed="true">
          <xs:sequence>
            <xs:element name="entry">
              <xs:complexType mixed="true">
                <xs:sequence>
                  <xs:element name="items" type="xs:string"/>
                  <xs:element name="characters">
                    <xs:complexType>
                      <xs:attribute name="character" type="xs:string"/>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name="eventscollection">
                    <xs:complexType>
                      <xs:attribute name="type" type="xs:string"/>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name="entrytxt">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:any minOccurs="0"/>
                      </xs:sequence>
                      <xs:anyAttribute/>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
                <xs:attribute name="when" type="xs:string"/> 
                <xs:attribute name="place" type="xs:string"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    【讨论】:

    • 非常感谢。我非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多