【问题标题】:Why is my XML file not validating, but is well-formed?为什么我的 XML 文件没有验证,但格式正确?
【发布时间】:2015-09-16 04:08:30
【问题描述】:

我对 XML 非常陌生,过去 3 天我一直在为这个简单的文件而烦恼。就我现在而言,我声明的每个元素和属性都是正确的,我按照教科书重新阅读并重新完成了两次作业,但一直收到这个错误。我错过了什么,我将如何纠正错误并最终提交我的作业(迟到)?

这是我不断遇到的错误,尽管完全按照书中所说的那样做:

[Xerces-J 2.9.1] Validating "mdpba1.xml" against "Internal DocType Declaration" ...

Ln 33 Col 47 - 必须为元素类型“bands”声明属性“name”。 1 错误

这是我的代码:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!--
   New Perspectives on XML
   Tutorial 2
   Case Problem 2

   Filename:         mdpba.xml 
-->
<!DOCTYPE bands [

<!ENTITY celtic SYSTEM "celtic.jpg" NDATA JPG>
<!ENTITY badger SYSTEM "badger.jpg" NDATA JPG>

<!ELEMENT bands (band+)>
<!ELEMENT band (name, city, logo?, competition)>
<!ATTLIST band cid ID #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ATTLIST name grade (1 | 2 | 3 | 4 | 5 | juvenile | novice) #REQUIRED>
<!ELEMENT city (#PCDATA)>
<!ELEMENT logo EMPTY>
<!ATTLIST logo source ENTITY #REQUIRED>
<!ELEMENT competition (event, event)>
<!ELEMENT event (tune+)>
<!ATTLIST event type (MSR | Medley) #REQUIRED>
<!ELEMENT tune (#PCDATA)>
<!NOTATION JPG SYSTEM "image/jpg">

]>
<bands name="Midwest Developmental Pipe Band">
   <band cid="c0001">
      <name grade="juvenile">School of Celtic Bagpipes &amp; Drumming</name>
      <city>Delafield</city>
      <logo source="celtic" />
      <competition>
         <event type="MSR">
            <tune>Charlies Welcome</tune>
            <tune>Mrs MacPherson of Inveran</tune>
            <tune>Brown Haired Maiden</tune>
         </event>
         <event type="Medley">
            <tune>Rowd's Hornpipe</tune>
         </event>
      </competition>
   </band>
   <band cid="c0002">
      <name grade="juvenile">Badger Pipes and Drums</name>
      <city>Madison</city>
      <logo source="badger" />
      <competition>
         <event type="MSR">
            <tune>Lord Alexander Kennedy</tune>
            <tune>Bob of Fettercairn</tune>
            <tune>Brown Haired Maiden</tune>
         </event>
         <event type="Medley">
            <tune>Out of the Air</tune>
         </event>
      </competition>
   </band>
   <band cid="c0003">
      <name grade="novice">Pardeeville School of Piping and Drumming</name>
      <city>Pardeeville</city>
      <competition>
         <event type="MSR">
            <tune>Lord Alexander Kennedy</tune>
            <tune>Bob of Fettercairn</tune>
            <tune>The Little Cascade</tune>
         </event>
         <event type="Medley">
            <tune>The Radar Racketeer</tune>
         </event>
      </competition>
   </band>
   <band cid="c0004">
      <name grade="3">Zoar Scottish Pipe Band</name>
      <city>Zoar</city>
      <competition>
         <event type="MSR">
            <tune>Charlies Welcome</tune>
            <tune>Mrs MacPherson of Inveran</tune>
            <tune>Major Manson</tune>
         </event>
         <event type="Medley">
            <tune>Miss Lily</tune>
         </event>
      </competition>
   </band>
   <band cid="c0005">
      <name grade="juvenile">Stockholm Pipe Band</name>
      <city>Stockholm</city>
      <competition>
         <event type="MSR">
            <tune>Pretty Marion</tune>
            <tune>The Sheepwife</tune>
            <tune>Brown Haired Maiden</tune>
         </event>
         <event type="Medley">
            <tune>Farewell to Erin</tune>
          </event>
      </competition>
   </band>
</bands>​

我太缺乏经验了,我不确定如何在不搞砸的情况下解决这个问题,任何帮助都将不胜感激,谢谢...

【问题讨论】:

  • 验证和格式良好是两个独立的概念。文档可以格式正确但无效。 (格式错误的文档永远不会有效,但原因很明显。)
  • XMLValidation.com“必须为元素类型 "bands" 声明属性 "name"。”.
  • 看起来您的架构不希望 name="Midwest Developmental Pipe Band" 属性出现在 &lt;bands&gt; 元素上...
  • 但这就是书籍/数据文件中的内容。我只创建了 DTD/元素和属性。每次我尝试将“名称”添加到“乐队”元素时,它都会要求一个类型,我不知道要使用哪种类型而不影响其他东西......
  • 这似乎与昨天的your deleted question 重复。

标签: html xml validation


【解决方案1】:

您的问题是因为您在 XML 标头中的 DTD 规范没有将 name 指定为元素 &lt;bands&gt; 的有效或预期属性

如果将此属性所需的 DTD 定义添加到 XML,它将验证。

&lt;!ATTLIST bands name CDATA #REQUIRED&gt;

这是您更正后的 XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!--
   New Perspectives on XML
   Tutorial 2
   Case Problem 2

   Filename:         mdpba.xml 
-->
<!DOCTYPE bands [

<!ENTITY celtic SYSTEM "celtic.jpg" NDATA JPG>
<!ENTITY badger SYSTEM "badger.jpg" NDATA JPG>

<!ELEMENT bands (band+)>
<!ATTLIST bands name CDATA #REQUIRED>
<!ELEMENT band (name, city, logo?, competition)>
<!ATTLIST band cid ID #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ATTLIST name grade (1 | 2 | 3 | 4 | 5 | juvenile | novice) #REQUIRED>
<!ELEMENT city (#PCDATA)>
<!ELEMENT logo EMPTY>
<!ATTLIST logo source ENTITY #REQUIRED>
<!ELEMENT competition (event, event)>
<!ELEMENT event (tune+)>
<!ATTLIST event type (MSR | Medley) #REQUIRED>
<!ELEMENT tune (#PCDATA)>
<!NOTATION JPG SYSTEM "image/jpg">

]>
<bands name="Midwest Developmental Pipe Band">
   <band cid="c0001">
      <name grade="juvenile">School of Celtic Bagpipes &amp; Drumming</name>
      <city>Delafield</city>
      <logo source="celtic" />
      <competition>
         <event type="MSR">
            <tune>Charlies Welcome</tune>
            <tune>Mrs MacPherson of Inveran</tune>
            <tune>Brown Haired Maiden</tune>
         </event>
         <event type="Medley">
            <tune>Rowd's Hornpipe</tune>
         </event>
      </competition>
   </band>
   <band cid="c0002">
      <name grade="juvenile">Badger Pipes and Drums</name>
      <city>Madison</city>
      <logo source="badger" />
      <competition>
         <event type="MSR">
            <tune>Lord Alexander Kennedy</tune>
            <tune>Bob of Fettercairn</tune>
            <tune>Brown Haired Maiden</tune>
         </event>
         <event type="Medley">
            <tune>Out of the Air</tune>
         </event>
      </competition>
   </band>
   <band cid="c0003">
      <name grade="novice">Pardeeville School of Piping and Drumming</name>
      <city>Pardeeville</city>
      <competition>
         <event type="MSR">
            <tune>Lord Alexander Kennedy</tune>
            <tune>Bob of Fettercairn</tune>
            <tune>The Little Cascade</tune>
         </event>
         <event type="Medley">
            <tune>The Radar Racketeer</tune>
         </event>
      </competition>
   </band>
   <band cid="c0004">
      <name grade="3">Zoar Scottish Pipe Band</name>
      <city>Zoar</city>
      <competition>
         <event type="MSR">
            <tune>Charlies Welcome</tune>
            <tune>Mrs MacPherson of Inveran</tune>
            <tune>Major Manson</tune>
         </event>
         <event type="Medley">
            <tune>Miss Lily</tune>
         </event>
      </competition>
   </band>
   <band cid="c0005">
      <name grade="juvenile">Stockholm Pipe Band</name>
      <city>Stockholm</city>
      <competition>
         <event type="MSR">
            <tune>Pretty Marion</tune>
            <tune>The Sheepwife</tune>
            <tune>Brown Haired Maiden</tune>
         </event>
         <event type="Medley">
            <tune>Farewell to Erin</tune>
          </event>
      </competition>
   </band>
</bands>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多