XML DTD命名空间未绑定

时间:2013-04-23 06:11:25

标签: xml namespaces xml-namespaces dtd xml-dtd

好的,所以我需要为XML项目声明名称空间,并使用DTD进行验证。一切似乎都是正确的,甚至我的朋友和教授都这么说,但我不断收到一条错误消息,说“元素”st:course“的前缀”st“没有约束。”它看起来与我有关,我错过了什么?

XML:

   
<?xml-namespace ns="http://www.student_courses/data/st/ns" prefix="st"?>
<?xml-namespace ns="http://www.student_courses/data/cr/ns" prefix="cr"?>
<!DOCTYPE students SYSTEM "student_courses.dtd">

<students>
<student number="a101"> <!-- number is an ID, required-->
<Name title="Mr.">John Doe</Name><!-- title values can be 'Mr.','Ms.','Dr.'-->
<st:course xmlns= "http://www.student_courses/data/st/ns" section="01">WEB 225</st:course>
<enrolled>22</enrolled>

<content>
<level class="Intro"></level>
<comments>Great course</comments><!-- An optional element -->
<book isbn="">XML</book><!--isbn is required, but the element is optional -->
</content>
</student>

<student number="a102"><!-- number is an ID, required-->
<Name title="Dr.">Jane Williams</Name>
<st:course xmlns= "http://www.student_courses/data/st/ns">WEB 325</st:course>
<enrolled>22</enrolled>

<content>
<level class="Adv."></level>
</content>
</student>

<student number="a103"><!-- number is an ID, required-->
<Name title="Ms.">Jane Doe</Name><!-- title values can be 'Mr.','Ms.','Dr.'-->
<st:course xmlns= "http://www.student_courses/data/st/ns" section="03">WEB  440</st:course>
<enrolled>12</enrolled>

<content>
<level class="Adv."></level>
<comments>Great course</comments><!-- An optional element -->
</content>
</student>

<courses>

<cr:course xmlns = "http://www.student_courses/data/cr/ns" id="WEB225">
<name>Web Development II</name>
<offered>Spring</offered>
<pre_reqs>WEB125</pre_reqs>
</cr:course>

<cr:course xmlns = "http://www.student_courses/data/cr/ns" id="WEB125">
<name>Web Development I</name>
<offered>Fall</offered>
</cr:course>

<cr:course xmlns = "http://www.student_courses/data/cr/ns" id="WEB325">
<name>Client-Side Scripting</name>
<offered>Spring</offered>
<offered>Fall</offered>
<pre_reqs>WEB225</pre_reqs>
</cr:course>

</courses>

</students>

这是我的DTD:

<!ELEMENT students (student+)>
<!ELEMENT student (Name+,st:course+,enrolled+,content+)>
<!ATTLIST student number ID #REQUIRED>
<!ELEMENT Name (#PCDATA)>
<!ATTLIST Name title (Mr. | Ms. | Dr.) #IMPLIED>
<!ELEMENT st:course (#PCDATA)>
<!ATTLIST st:course xmlns CDATA #FIXED "http://www.student_courses/data/st/ns"> 
<!ATTLIST st:course section CDATA #IMPLIED>
<!ELEMENT enrolled (#PCDATA)>
<!ELEMENT content (level+, comments*, book?)>
<!ELEMENT level (#PCDATA)>
<!ATTLIST level class (Intro | Adv.) #IMPLIED>
<!ELEMENT comments (#PCDATA)> 
<!ELEMENT book (#PCDATA)>
<!ATTLIST book isbn CDATA #REQUIRED>
<!ELEMENT courses (cr:course+)>
<!ELEMENT cr:course (name+,offered+,pre_reqs*)>
<!ATTLIST cr:course xmlns:cr CDATA #FIXED "http://www.student_courses/data/cr/ns"> 
<!ATTLIST cr:course id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT offered (#PCDATA)>
<!ELEMENT pre_reqs (#PCDATA)>

谢谢大家,爱这个地方!!

-K

1 个答案:

答案 0 :(得分:0)

你的问题是:

  1. 在DTD更改&lt;!ATTLIST st:course xmlns CDATA #FIXED“http://www.student_courses/data/st/ns”&gt; to&lt;!ATTLIST st:course xmlns:st CDATA #FIXED“http://www.student_courses/data/st/ns”&gt;

  2. 在XML中评论您的&lt; courses&gt; ...&lt; / courses&gt;元素,因为它根据您的DTD无效。您的DTD已经&lt; student&gt;在&lt; students&gt;下仅

  3. 随处更改XML&lt; st:course xmlns =“http://www.student_courses/data/st/ns”...&gt; to&lt; st:course xmlns:st =“http://www.student_courses/data/st/ns”...&gt;

  4. 在XML更改开始时&lt;?xml-namespace ns =“http://www.student_courses/data/st/ns"prefix =”st“?&gt; to&lt;?xml-namespace st =“http://www.student_courses/data/st/ns”prefix =“st”?&gt;

  5. 第一个和第三个是相互核心的。如果你想把你的'course'元素保持为'st:course'(带名称空间前缀),那么你必须将它保存到你的DTD中。

    第二和第三是自我解释。