XML代码混淆。继续得到" '('要求启动ATTLIST枚举"不确定如何修复

时间:2016-11-12 04:27:16

标签: xml debugging dtd

我是大学生,我遇到了这个代码文件的问题。我会问老师或其他同学,但似乎没有人回应我在BlackBoard上的代码问题。所以我希望这里有人可以提供帮助!

指令说明&#34;对于versionDate元素,包含调查日期的可选surveyDate属性&#34;。我不完全确定这意味着什么,但我尝试了#34; <!ATTLIST versionDate surveyDate #IMPLIED>&#34;,但上面的错误代码出现了。唯一一行包含&#34; surveyDate&#34;在这是这个:

<versionDate surveyDate=">1849"<1968/versionDate>

我尝试删除该行以查看它是否是我写的方式,但后来我收到了错误代码&#34; ATTLIST: no name for Attribute&#34;,我相信我的所有代码都有一个名字对于。有人可以帮忙吗?

如果它有助于整个元素和attlist代码如下:

<!DOCTYPE products
[
    <!ELEMENT product (product+)>

    <!ELEMENT product (name, versionDate, desc, isbn?, price?)>

    <!ELEMENT name (#PCDATA)>
    <!ATTLIST name pid ID #REQUIRED>
    <!ATTLIST name category (historical |state |parks ) #REQUIRED>


    <!ELEMENT versionDate (#PCDATA)>


    <!ELEMENT desc (#PCDATA)>
    <!ELEMENT isbn (#PCDATA)>

    <!ELEMENT price (#PCDATA)>
    <!ATTLIST price format (flat |raised ) "flat" #IMPLIED> <!-- format default is flat. -->
    <!ATTLIST price media (paper | electronic)>
]>

1 个答案:

答案 0 :(得分:3)

I see 3 issues:

  1. You've declared product twice. Based on the doctype declaration, the first element declaration should be for products (plural).
  2. In the attribute declaration for the format attribute of the price element, you have an enumeration with the values "flat" and "raised" and a default value of "flat". You also have #IMPLIED which is invalid if you have a default value. You should remove #IMPLIED.
  3. In the attribute declaration for the media attribute of the price element, you have an enumeration but don't have a default value, #IMPLIED, or #REQUIRED. Do one of these:
    • Add a default value (that matches a value in the enumeration)
    • Add #IMPLIED
    • Add #REQUIRED

Also, you can combine the ATTLIST declarations for an element. It makes it a little easier to read.

One last thing, if this an external DTD, you probably don't need the doctype declaration in the DTD file. The doctype should be in the XML instance that is referencing the DTD. If the XML and DTD are all in one file, ignore this comment. (This section of the spec might help understanding the doctype declaration and DTD (prolog).)

Example...

<!DOCTYPE products [
<!ELEMENT products (product+)>

<!ELEMENT product (name, versionDate, desc, isbn?, price?)>

<!ELEMENT name (#PCDATA)>
<!ATTLIST name 
    pid ID #REQUIRED
    category (historical|state|parks) #REQUIRED>

<!ELEMENT versionDate (#PCDATA)>
<!ATTLIST versionDate 
    surveyDate CDATA #IMPLIED>

<!ELEMENT desc (#PCDATA)>
<!ELEMENT isbn (#PCDATA)>

<!ELEMENT price (#PCDATA)>
<!ATTLIST price 
    format (flat|raised) "flat"
    media  (paper|electronic) #REQUIRED>
]>

EDIT: I also added the attribute declaration for the surveyDate attribute on the versionDate element. This was missing the attribute type. (See here for more information.)

Your XML example of the versionDate element is also incorrect; it's not well-formed. It should look like this:

<versionDate surveyDate="1849">1968</versionDate>