s4s-elt-invalid-content.1:内容无效。元素 'attribute' 无效、放错位置或出现得太频繁



我有一个XML的XSD,除了一个错误外,一切都很好:

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>

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

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

除了吉姆·加里森(Jim Garrison)关于将属性添加到简单内容元素中的正确答案外,另一个问题是,对于您的xml, track element ,而不是属性

所以更改,

<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>

to

<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。

相关内容

最新更新