Reputation: 23
XML (from https://github.com/SMAPPNYU/ProgrammerGroup/blob/master/LargeDataSets/sample-xml.xml)
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>
DTD
<!ELEMENT catalog (product)>
<!ELEMENT product (catalog_item+)>
<!ELEMENT catalog_item (item_number,price,size+)>
<!ELEMENT size (color_swatch+)>
<!ELEMENT item_number (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT color_swatch (#PCDATA)>
<!ATTLIST product description "Cardigan Sweater" CDATA>
<!ATTLIST product product_image "cardigan.jpg" CDATA>
<!ATTLIST catalog_item gender ("Mens"|"Womens") CDATA>
<!ATTLIST size description ("Small"|"Medium"|"Large"|"Extra Large") CDATA>
<!ATTLIST color_swatch image ("red_cardigan.jpg"|"burgundy_cardigan.jpg"|"navy_cardigan.jpg"|"black_cardigan.jpg") CDATA>
For the XML code above I have tried DTD as below but I couldn't validate it. Please check what is wrong with my DTD. What changes need to be made in the DTD so the XML code can be validated without any error?
Upvotes: 2
Views: 140
Reputation: 52888
Your DTD is invalid because of the ATTLIST declarations for gender
, description
(product
element), and image
. The values in the enumerations shouldn't be quoted.
Further more, the values should be valid NMTOKEN (name token) types which means they can't contain spaces (so Extra Large
would need to become ExtraLarge
, Extra_Large
, etc. in both the DTD and the XML instance).
So to modify the DTD to make the XML valid those ATTLIST declarations would need to change to:
<!ATTLIST catalog_item gender (Mens|Womens) #REQUIRED>
<!ATTLIST size description (Small|Medium|Large|ExtraLarge) #REQUIRED>
<!ATTLIST color_swatch image (red_cardigan.jpg|burgundy_cardigan.jpg|navy_cardigan.jpg|black_cardigan.jpg) #REQUIRED>
Note that I made the attributes required (#REQUIRED
) but you could make them #IMPLIED
(optional) or give them a default value (wrap the value in quotes). See https://www.w3.org/TR/REC-xml/#sec-attr-defaults for more info.
You'd also need to change the description
attribute value from "Extra Large".
Another thing; in your XML you use "Men's" and "Women's" as values for the gender
attribute. You'd need to change those to "Mens" and "Womens".
I also noticed that you use default values in the attributes for the product
element. This doesn't make sense. What happens when you have a second product that isn't a "Cardigan Sweater"?
I think you should change those declarations to:
<!ATTLIST product
description CDATA #REQUIRED
product_image CDATA #REQUIRED>
Notice that I combined both declarations into one and formatted for readability. This is not necessary, but does make it easier to read in my opinion.
One last thing; having an enumeration for image
on color_swatch
doesn't make sense either. You'd have to have every image for every product listed in it. Maybe change it to:
<!ATTLIST color_swatch image CDATA #REQUIRED>
Updated DTD (also moved ATTLIST declarations under corresponding ELEMENT declarations. this is just personal preference.):
<!ELEMENT catalog (product)>
<!ELEMENT product (catalog_item+)>
<!ATTLIST product
description CDATA #REQUIRED
product_image CDATA #REQUIRED>
<!ELEMENT catalog_item (item_number,price,size+)>
<!ATTLIST catalog_item gender (Mens|Womens) #REQUIRED>
<!ELEMENT size (color_swatch+)>
<!ATTLIST size description (Small|Medium|Large|ExtraLarge) #REQUIRED>
<!ELEMENT item_number (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT color_swatch (#PCDATA)>
<!ATTLIST color_swatch image CDATA #REQUIRED>
Updated XML
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Mens">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Womens">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="ExtraLarge">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>
Upvotes: 1