xml - Uniqueness in XSD or DTD with namespaces -
i need create rules in .xsd or .dtd these cases:
- the name of songs can't repeated. (i not sure if did in code, please check)
- the elements comments , genre can optional, rest of them required. (i not sure if did in code, please check)
- there 3 types of values element genre, pop, rock , jazz. (i sure did in code, please check)
this code, getting errors http://www.xmlvalidation.com/ in xml document:
7: 60 attribute "xmlns" must declared element type "catalog". 7: 60 attribute "xmlns:xsi" must declared element type "catalog". 7: 60 attribute "xsi:schemalocation" must declared 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 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 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 1 love</name2> <artist>air supply</artist> <album>the 1 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>
- the name of songs cant repeated. (i not sure if did in code, please check)
yes, because maxoccurs
defaults 1
. (the default values minoccurs
, maxoccurs
1.)
update: force contents of name2
unique within catalog
, use xs:unique
shown in xsd below. then, if, there 2 name2
elements "bed of roses" content, you'll receive error such following:
[error] catalog.xml:17:32: cvc-identity-constraint.4.1: duplicate unique value [bed of roses] declared identity constraint "catalog-song-name2-unique" of element "catalog".
next question:
- the elements
comments
,genre
can optional, rest of them required. (i not sure if did in code, please check)
yes, because comments
, genre
have minoccurs="0"
, rest have minoccurs="1"
.
- there 3 types of values element genre, pop, rock , jazz.(i sure did in code, please check)
correct.
now, regarding errors:
dtds not compatible xml namespaces without unnatural contortions.
recommend drop dtd , go xsd.
then, xml without doctype
line , minor syntactical correction (delete space in 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 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 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 1 love</name2> <artist>air supply</artist> <album>the 1 love</album> <year>1981</year> <genre>pop</genre> <comments>good song</comments> <path>c://music/air supply</path> </song> </catalog>
will valid against xsd (again, minor syntactical correction: add space before type
in declaration of name2
):
update: demonstrates 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>
Comments
Post a Comment