我在以下XML文件的atlist
声明行中收到了标记声明错误:
<?xml encoding="UTF-8"?>
<!ELEMENT catalog (title,(plant)+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT plant ((name)+,(climate)+,(height)+,(usage)+,(image)+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT climate (#PCDATA)>
<!ELEMENT height (#PCDATA)>
<!ELEMENT usage (#PCDATA)>
<!ELEMENT image (#PCDATA)>
<!ATLIST plant id CDATA #REQUIRED>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "plantdtd.dtd">
<catalog>
<title>Flowers of the week</title>
<plant id="A1">
<name>Aloe vera</name>
<climate>tropical</climate>
<height>60-100cm</height>
<usage>medicinal</usage>
<image>aloevera.jpg</image>
</plant>
<plant id="A2">
<name>Orchidaceae</name>
<height>8-12in</height>
<usage>medicinal</usage>
<usage>decoration</usage>
<image>Orchidaceae.jpg</image>
</plant>
</catalog>
我的XML文档有什么问题?
答案 0 :(得分:1)
您的XML文档同时包含well-formedness and validity problems ...
阻止您的XML文档格式正确的问题,包括:
阻止您的XML文档生效的问题:
A2
plant
必须至少有一个climate
子元素。以下XML已更正为格式正确且有效:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (title,(plant)+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT plant ((name)+,(climate)+,(height)+,(usage)+,(image)+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT climate (#PCDATA)>
<!ELEMENT height (#PCDATA)>
<!ELEMENT usage (#PCDATA)>
<!ELEMENT image (#PCDATA)>
<!ATTLIST plant id CDATA #REQUIRED>
]>
<catalog>
<title>Flowers of the week</title>
<plant id="A1">
<name>Aloe vera</name>
<climate>tropical</climate>
<height>60-100cm</height>
<usage>medicinal</usage>
<image>aloevera.jpg</image>
</plant>
<plant id="A2">
<name>Orchidaceae</name>
<climate/>
<height>8-12in</height>
<usage>medicinal</usage>
<usage>decoration</usage>
<image>Orchidaceae.jpg</image>
</plant>
</catalog>
答案 1 :(得分:0)
此错误是由DTD文件无效引起的。您已创建包含以下内容的DTD文件:
<!DOCTYPE catalog [
…
]>
只需删除显示DTD数据的第一行和最后一行,因为只有在XML文件中包含DTD时才会使用这些数据。应该有吗?因为在植物A2中气候不存在。
`
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT catalog (title,plant+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT plant (name,climate?,height,usage+,image)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT climate (#PCDATA)>
<!ELEMENT height (#PCDATA)>
<!ELEMENT usage (#PCDATA)>
<!ELEMENT image (#PCDATA)>
<!ATTLIST plant id CDATA #REQUIRED>
`