s4s-elt-invalid-content.1:内容无效。元素'属性'无效,放错位置或过于频繁发生

时间:2016-12-08 02:41:52

标签: xml xsd xsd-validation xml-validation

我有一个用于XSD的XML,除了一个错误之外它都很好:

  

Ln 25 Col 50 - s4s-elt-invalid-content.1:内容   '#AnonType_tracks'无效。元素'属性'无效,   错位,或经常发生。

这是我的XML代码:

<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="items.xsd">
<item>
  <title>Kind of Blue</title>
  <priceus>US: $11.99</priceus>
  <priceuk>UK: £8.39</priceuk>
  <artist>Miles Davis</artist>
  <tracks>
     <track length="9:22">So What</track>
     <track length="5:37">Blue in Green</track>
     <track length="11:33">All Blues</track>
     <track length="9:26">Flamenco Sketches</track>
  </tracks>
</item>
<item>
  <title>Blue Train</title>
  <priceus>US: $8.99</priceus>
  <priceuk>UK: £6.29</priceuk>
  <artist>John Coltrane</artist>
  <tracks>
     <track length="10:39">Blue Train</track>
     <track length="9:06">Moment's Notice</track>
     <track length="7:11">Locomotion</track>
  </tracks>
</item>
</items>

这是我的XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="title" type="xs:string" />
<xs:element name="priceus" type="xs:string" />
<xs:element name="priceuk" type="xs:string" />
<xs:element name="artist" type="xs:string" />
<xs:attribute name="track" type="xs:string" />

<xs:element name="tracks">
  <xs:complexType>
     <xs:simpleContent>
        <xs:attribute ref="track" use="required" />
     </xs:simpleContent>
  </xs:complexType>
 </xs:element>

<xs:element name="item">
  <xs:complexType>
     <xs:sequence>
        <xs:element ref="title" />
        <xs:element ref="priceus" />
        <xs:element ref="priceuk" />
        <xs:element ref="artist" />
        <xs:element ref="tracks"/>
     </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="items">
  <xs:complexType>
     <xs:sequence>
        <xs:element ref="item" minOccurs="1" maxOccurs="unbounded" />
     </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

2 个答案:

答案 0 :(得分:3)

<xs:simpleContent>声明不能包含<attribute>声明,只能包含<restriction><extension>。可以在<restriction><extension>块中指定属性。

请参阅https://www.safaribooksonline.com/library/view/xml-schema/0596002521/re49.html

答案 1 :(得分:3)

除了Jim Garrison's correct answer about adding attributes to simple content elements之外,另一个问题是,对于您的XML,track 元素 ,而不是 属性

所以改变,

<xs:attribute name="track" type="xs:string" />

<xs:element name="tracks">
  <xs:complexType>
    <xs:simpleContent>
      <xs:attribute ref="track" use="required" />
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

<xs:element name="track">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="length" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

<xs:element name="tracks">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="track" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

,您的XSD将成功验证您的XML。