0

好的,所以我需要为 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

4

1 回答 1

0

你的问题是:

  1. 在 DTD 中将 <!ATTLIST st:course xmlns CDATA #FIXED " http://www.student_courses/data/st/ns "> 更改为 <!ATTLIST st:course xmlns:st CDATA #FIXED " http://www.student_courses /数据/st/ns ">

  2. 在 XML 注释中,您的 <courses>... </courses> 元素根据您的 DTD 无效。您的 DTD 仅在 <students> 下有 <student>。

  3. 随处更改 XML <st:course xmlns= " http://www.student_courses/data/st/ns "...> 到 <st:course xmlns:st="http://www.student_courses/data/st /ns"...>

  4. 在 XML 开始时将 <?xml-namespace ns="http://www.student_courses/data/st/ns" prefix="st"?> 更改为 <?xml-namespace st="http://www. student_courses/data/st/ns" 前缀="st"?>

第一个和第三个是相互关联的。如果您想将您的“课程”元素保留为“st:course”(带有命名空间前缀),那么您必须将其添加到您的 DTD 中。

其中第二和第三是不言自明的。

于 2013-04-23T06:38:18.810 回答