我需要在.xsd或.dtd中为这些情况创建一些规则:
这是我的代码,但我从XML文档中的http://www.xmlvalidation.com/收到此错误:
7: 60 Attribute "xmlns" must be declared for element type "catalog".
7: 60 Attribute "xmlns:xsi" must be declared for element type "catalog".
7: 60 Attribute "xsi:schemaLocation" must be declared for element type "catalog".
的catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLoc ation="http://www.w3schools.com catalog.xsd">
<song>
<name2>Bed of Roses</name2>
<artist>Bon Jovi</artist>
<album>Cross Road</album>
<year>1995</year>
<genre>rock</genre>
<comments>Good song</comments>
<path>C://music/bon jovi</path>
</song>
<song>
<name2>Fly Away from here</name2>
<artist>Aerosmith</artist>
<album>Just Push Play</album>
<year>2001</year>
<genre>rock</genre>
<comments>Good song</comments>
<path>C://music/aerosmith</path>
</song>
<song>
<name2>Jossie</name2>
<artist>Blink 182</artist>
<album>Blink 182</album>
<year>2001</year>
<genre>pop</genre>
<comments>Good song</comments>
<path>C://music/blink 182</path>
</song>
<song>
<name2>Want you bad</name2>
<artist>The Offspring</artist>
<album>Conspiracy of One</album>
<year>2000</year>
<genre>pop</genre>
<comments>Good song</comments>
<path>C://music/the offspring</path>
</song>
<song>
<name2>The One that you love</name2>
<artist>Air Supply</artist>
<album>The One that you love</album>
<year>1981</year>
<genre>pop</genre>
<comments>Good song</comments>
<path>C://music/air supply</path>
</song>
</catalog>
catalog.DTD
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT catalog (song+)>
<!ELEMENT song (name2,artist,album,year,genre,comments,path)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT name2 (#PCDATA)>
<!ELEMENT artist (#PCDATA)>
<!ELEMENT album (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT genre (#PCDATA)>
<!ELEMENT comments (#PCDATA)>
<!ELEMENT path (#PCDATA)>
catalog.XSD
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="song" maxOccurs="5">
<xs:complexType>
<xs:sequence>
<xs:element name="name2" minOccurs="1"type="xs:string"/>
<xs:element name="artist" minOccurs="1" type="xs:string"/>
<xs:element name="album" minOccurs="1" type="xs:string"/>
<xs:element name="year" minOccurs="1" type="xs:integer"/>
<xs:element name="genre" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pop"/>
<xs:enumeration value="rock"/>
<xs:enumeration value="jazz"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="comments" minOccurs="0" type="xs:string"/>
<xs:element name="path" minOccurs="1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
答案 0 :(得分:1)
- 歌曲的名称不能重复。 (我不确定我是否这样做过 在我的代码中,请检查)
醇>
是的,因为maxOccurs
默认为1
。 (minOccurs
和maxOccurs
的默认值为1.)
更新:要强制name2
的内容在catalog
内保持唯一,请使用xs:unique
,如下面的XSD所示。然后,如果,有两个name2
元素与&#34;玫瑰花床&#34;内容,您将收到如下错误:
[错误] catalog.xml:17:32:cvc-identity-constraint.4.1:重复 为身份限制声明的独特价值[玫瑰之床] &#34;目录-歌曲名2-独特&#34;元素&#34;目录&#34;。
下一个问题:
- 元素
醇>comments
和genre
可以 可选,其余的都是必需的。 (我不确定我是否这样做过 在我的代码中,请检查)
是的,因为comments
和genre
有minOccurs="0"
,其余的有minOccurs="1"
。
- 只有3种类型的值 元素类型,流行,摇滚和爵士乐。(我相信我在我的代码中做到了, 请检查)
醇>
正确。
现在,关于错误:
如果没有非自然的扭曲,DTD与XML名称空间不兼容。
建议您放弃DTD并使用XSD。
然后,你的XML没有DOCTYPE
行,并且有一个小的语法修正(删除xsi:schemaLoc ation
中的空格),
<?xml version="1.0" encoding="UTF-8"?>
<catalog
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com catalog.xsd">
<song>
<name2>Bed of Roses</name2>
<artist>Bon Jovi</artist>
<album>Cross Road</album>
<year>1995</year>
<genre>rock</genre>
<comments>Good song</comments>
<path>C://music/bon jovi</path>
</song>
<song>
<name2>Fly Away from here</name2>
<artist>Aerosmith</artist>
<album>Just Push Play</album>
<year>2001</year>
<genre>rock</genre>
<comments>Good song</comments>
<path>C://music/aerosmith</path>
</song>
<song>
<name2>Jossie</name2>
<artist>Blink 182</artist>
<album>Blink 182</album>
<year>2001</year>
<genre>pop</genre>
<comments>Good song</comments>
<path>C://music/blink 182</path>
</song>
<song>
<name2>Want you bad</name2>
<artist>The Offspring</artist>
<album>Conspiracy of One</album>
<year>2000</year>
<genre>pop</genre>
<comments>Good song</comments>
<path>C://music/the offspring</path>
</song>
<song>
<name2>The One that you love</name2>
<artist>Air Supply</artist>
<album>The One that you love</album>
<year>1981</year>
<genre>pop</genre>
<comments>Good song</comments>
<path>C://music/air supply</path>
</song>
</catalog>
对你的XSD有效(同样,通过一个小的语法修正:在type
的声明中在name2
之前添加一个空格):
更新:现在为xs:unique
演示name2
:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns:w3="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="song" maxOccurs="5">
<xs:complexType>
<xs:sequence>
<xs:element name="name2" minOccurs="1" type="xs:string"/>
<xs:element name="artist" minOccurs="1" type="xs:string"/>
<xs:element name="album" minOccurs="1" type="xs:string"/>
<xs:element name="year" minOccurs="1" type="xs:integer"/>
<xs:element name="genre" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pop"/>
<xs:enumeration value="rock"/>
<xs:enumeration value="jazz"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="comments" minOccurs="0" type="xs:string"/>
<xs:element name="path" minOccurs="1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="catalog-song-name2-unique">
<xs:selector xpath="w3:song"/>
<xs:field xpath="w3:name2"/>
</xs:unique>
</xs:element>
</xs:schema>