对于给定的 XML 文件,此 DTD 是否错误
Is this DTD wrong for the given XML file
XML(来自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>
对于上面的 XML 代码,我尝试了如下的 DTD,但我无法验证它。请检查我的 DTD 有什么问题。需要在 DTD 中进行哪些更改才能验证 XML 代码而不会出现任何错误?
由于 gender
、description
(product
元素)和 image
的 ATTLIST 声明,您的 DTD 无效。 enumerations 中的值不应被引用。
此外,这些值应该是有效的 NMTOKEN(名称标记)类型,这意味着它们不能包含空格(因此 Extra Large
需要变为 ExtraLarge
、Extra_Large
, 等等在 DTD 和 XML 实例中).
因此,要修改 DTD 以使 XML 有效,这些 ATTLIST 声明需要更改为:
<!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>
请注意,我将属性设置为必需 (#REQUIRED
),但您可以将它们设置为 #IMPLIED
(可选)或给它们一个默认值(将值括在引号中)。有关详细信息,请参阅 https://www.w3.org/TR/REC-xml/#sec-attr-defaults。
您还需要将 description
属性值从 "Extra Large" 更改。
另一件事;在您的 XML 中,您使用 "Men's" 和 "Women's" 作为 gender
属性的值。您需要将它们更改为 "Mens" 和 "Womens".
我还注意到您在 product
元素的属性中使用默认值。这没有意义。当您有第二个产品不是 "Cardigan Sweater" 时会发生什么?
我认为您应该将这些声明更改为:
<!ATTLIST product
description CDATA #REQUIRED
product_image CDATA #REQUIRED>
请注意,我将两个声明合二为一并进行了格式化以便于阅读。这不是必需的,但在我看来确实更容易阅读。
最后一件事;在 color_swatch
上枚举 image
也没有意义。您必须拥有其中列出的每个产品的每个图像。也许将其更改为:
<!ATTLIST color_swatch image CDATA #REQUIRED>
更新的 DTD(还将 ATTLIST 声明移动到相应的 ELEMENT 声明下。这只是个人偏好。):
<!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>
已更新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>
XML(来自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>
对于上面的 XML 代码,我尝试了如下的 DTD,但我无法验证它。请检查我的 DTD 有什么问题。需要在 DTD 中进行哪些更改才能验证 XML 代码而不会出现任何错误?
由于 gender
、description
(product
元素)和 image
的 ATTLIST 声明,您的 DTD 无效。 enumerations 中的值不应被引用。
此外,这些值应该是有效的 NMTOKEN(名称标记)类型,这意味着它们不能包含空格(因此 Extra Large
需要变为 ExtraLarge
、Extra_Large
, 等等在 DTD 和 XML 实例中).
因此,要修改 DTD 以使 XML 有效,这些 ATTLIST 声明需要更改为:
<!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>
请注意,我将属性设置为必需 (#REQUIRED
),但您可以将它们设置为 #IMPLIED
(可选)或给它们一个默认值(将值括在引号中)。有关详细信息,请参阅 https://www.w3.org/TR/REC-xml/#sec-attr-defaults。
您还需要将 description
属性值从 "Extra Large" 更改。
另一件事;在您的 XML 中,您使用 "Men's" 和 "Women's" 作为 gender
属性的值。您需要将它们更改为 "Mens" 和 "Womens".
我还注意到您在 product
元素的属性中使用默认值。这没有意义。当您有第二个产品不是 "Cardigan Sweater" 时会发生什么?
我认为您应该将这些声明更改为:
<!ATTLIST product
description CDATA #REQUIRED
product_image CDATA #REQUIRED>
请注意,我将两个声明合二为一并进行了格式化以便于阅读。这不是必需的,但在我看来确实更容易阅读。
最后一件事;在 color_swatch
上枚举 image
也没有意义。您必须拥有其中列出的每个产品的每个图像。也许将其更改为:
<!ATTLIST color_swatch image CDATA #REQUIRED>
更新的 DTD(还将 ATTLIST 声明移动到相应的 ELEMENT 声明下。这只是个人偏好。):
<!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>
已更新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>