web-dev-qa-db-ja.com

タイプのコンテンツタイプは要素のみであるため、要素に文字[子供]を含めることはできません

XSDファイルでXMLファイルを検証しようとしましたが、次のエラーメッセージが表示されます。

[エラー]:cvc-complex-type.2.3:タイプのコンテンツタイプが要素のみであるため、要素 'paragraph'に文字[children]を含めることはできません

これは(私がそれを正しく理解している場合)、複合型は、単純型要素と複合型要素の組み合わせを子として持つことができないことを意味します。

しかし、どうすれば修正できますか?私はまだこれから少し新しいので、解決策は簡単でしょうか?

私のコードは以下に示されています:

XSD:

<?xml version="1.0" encoding="UTF-8"?>
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xs:element name="biography">
               <xs:complexType>
                     <xs:sequence>
                           <xs:element name="paragraph" maxOccurs="unbounded">
                                 <xs:complexType>
                                       <xs:sequence>
                                             <xs:element name="name">
                                                   <xs:complexType>
                                                         <xs:sequence>
                                                               <xs:element name="first_name" type="xs:string" minOccurs="0"></xs:element>
                                                               <xs:element name="last_name" type="xs:string"></xs:element>
                                                         </xs:sequence>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="emphasize" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
                                             <xs:element name="profession" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
                                             <xs:element name="date" minOccurs="0">
                                                   <xs:complexType>
                                                         <xs:sequence>
                                                               <xs:element name="month" type="xs:string"></xs:element>
                                                               <xs:element name="day" type="xs:int"></xs:element>
                                                               <xs:element name="year" type="xs:int"></xs:element>
                                                         </xs:sequence>
                                                   </xs:complexType>
                                             </xs:element>
                                       </xs:sequence>
                                 </xs:complexType>
                           </xs:element>
                           <xs:element name="definition" maxOccurs="unbounded">
                                 <xs:complexType>
                                       <xs:sequence>
                                             <xs:element name="term" type="xs:string"></xs:element>
                                       </xs:sequence>
                                 </xs:complexType>
                           </xs:element>
                     </xs:sequence>
               </xs:complexType>
         </xs:element>
   </xs:schema>

XML:

<biography>
 <paragraph>
  <name><first_name>Alex</first_name> <last_name>Turing</last_name></name>
  was one of the first people to truly deserve the name <emphasize>computer
  scientist</emphasize>. Although his contributions to the fields are too
  numerous to lst, his best-known are the famous <emphasize>Turing
  Test</emphasize> and <emphasize>Turing Machine</emphasize>.
 </paragraph>

 <definition>The <term>Turing Test</term> is to this day the standard test
  for determining whether a computer is truly intelligent. This test yet
  has to be passed.</definition>

 <definition>A <term>Turing Machine</term> is an abstract finite state
 automaton with infinite memory that can be proven equivalent to any other
 finite state automaton with arbitrarily large memory. Thus what is true
 for one Turing machine is true for all Turing machines no matter how
 implemented.</definition>

 <paragraph>
  <name><last_name>Turing</last_name></name> was also an accomplished
  <profession>mathematician</profession> and
  <profession>cryptographer</profession>. His assistance was crucial in
  helping the Allies decode the German Enigma cipher. He committed suicide
  on <date><month>June</month> <day>7</day>, <year>1954</year></date>
  after being convicted of homosexuality and forced to take female hormone
  injections.
  </paragraph>
</biography>
9
Toshan

これがまさにmixedコンテンツの目的です。

    <xs:element name="paragraph" maxOccurs="unbounded">
      <xs:complexType mixed="true">

そして:

    <xs:element name="definition" maxOccurs="unbounded">
      <xs:complexType mixed="true">

また、XMLを前提として、paragraphおよびdefinitionxs:choice macOccurs="unbounded"に含めることもできます。

以下は、XMLが有効になるように必要なすべての変更で更新されたXSDです。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="biography">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="paragraph">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="name">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="first_name" type="xs:string" minOccurs="0"></xs:element>
                    <xs:element name="last_name" type="xs:string"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="emphasize" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
              <xs:element name="profession" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
              <xs:element name="date" minOccurs="0">
                <xs:complexType mixed="true">
                  <xs:sequence>
                    <xs:element name="month" type="xs:string"></xs:element>
                    <xs:element name="day" type="xs:int"></xs:element>
                    <xs:element name="year" type="xs:int"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="definition">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="term" type="xs:string"></xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
6
kjhughes

私にとってこのエラーは、xmlの奇妙な文字が原因で発生します。「BeyondCompare」ツールで類似したデータを持つ2つのファイルを比較すると、表示されない不明な文字が見つかりました。 「Visual Studio Code」で不完全なファイルを開くと、いくつかの特殊文字が表示されました。

文字列をオンラインツールにコピーして貼り付けて特殊文字を見つけたところ、特殊文字(&#65279)であることがわかりました。

この性格のため、上記のエラーが発生しました。解決策を見つけるまでに2週間かかりました。

5
Varun M