【问题标题】:XML DTD Namespace not BoundXML DTD 命名空间未绑定
【发布时间】:2013-04-23 06:11:24
【问题描述】:

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

【问题讨论】:

  • 请注意,我在 w3c 官方规范中的任何地方都没有找到“xml-namespace”处理指令。

标签: xml namespaces xml-namespaces dtd


【解决方案1】:

你的问题是:

  1. 在 DTD 中将 http://www.student_courses/data/st/ns"> 更改为 http://www.student_courses/data/st/ns">

    李>
  2. 在 XML 注释中,您的 ... 元素根据您的 DTD 无效。您的 DTD 仅在 下有

  3. XML 随处变化 http://www.student_courses/data/st/ns"...> 到

  4. 在 XML 开始时将 更改为

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

第二个和第三个是不言自明的。

【讨论】:

  • 我不明白你的第二点:课程是另一个元素,我需要 cr 命名空间以避免与课程元素在那里和学生元素中的名称冲突。不过,你拥有的其他一切对我来说都很有意义。
  • 根据您创建的 DTD,您在根元素学生下只有一个子元素学生。但是,如果您想再添加一个子元素 - 'courses',则必须将您的 DTD 从 更改为到<!ELEMENT学生(学生+,课程)>。并在您的 XML 更改 <cr:course xmlns = "student_courses/data/cr/ns"...&gt; 到 <cr:course xmlns:cr = "student_courses/data/cr/ns"...&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多