我差不多为我的XML文件创建了一个DTD,但我不知道如何完成最后一部分。这是XML文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<authors>
<author aid="a1">Bill Evjen</author>
<author aid="a2">Michael Kay</author>
<author aid="a3">Kevin Goldberg</author>
<author aid="a4">Michael Morrison</author>
</authors>
<books>
<book bookID="b001" author="a2">
<title>XSLT 2.0 and XPath 2.0 Programmer's Reference</title>
<stock>4</stock>
<publisher>John Wiley</publisher>
<year>2009</year>
<use type="advanced" />
<use type="reference" />
</book>
<book bookID="b002" author="a1 a2">
<title>Professional XML (Programmer to Programmer) </title>
<stock>2</stock>
<publisher>John Wiley</publisher>
<year>2007</year>
<use type="professional" />
<use type="advanced" />
<use type="reference" />
</book>
<book bookID="b003" author="a3">
<title>XML: Visual QuickStart Guide</title>
<stock>3</stock>
<publisher>Peachpit Press</publisher>
<year>2008</year>
<use type="introductory" />
<use type="reference" />
</book>
<book bookID="b004" author="a4">
<title>Sams Teach Yourself XML in 24 Hours</title>
<stock>5</stock>
<publisher>SAMS</publisher>
<year>2005</year>
</book>
</books>
</library>
这是我的DTD,我评论了我无法弄清楚的一句话:
<!-- DTD for the scot ski example -->
<!ELEMENT library (authors | books)*>
<!ELEMENT authors (author)*>
<!ELEMENT author (#PCDATA) >
<!ATTLIST author aid CDATA #REQUIRED>
<!ELEMENT books (book)*>
<!ELEMENT book (title, stock, publisher, year, use*)>
<!ATTLIST book bookID ID #REQUIRED>
<!ATTLIST book author (authors) #REQUIRED ><!-- How to do this correctly -->
<!ELEMENT title (#PCDATA) >
<!ELEMENT stock (#PCDATA) >
<!ELEMENT publisher (#PCDATA) >
<!ELEMENT year (#PCDATA) >
<!ELEMENT use (#PCDATA) >
<!ATTLIST use type ( advanced | reference | introductory | professional) #REQUIRED >
我已经尝试了一段时间,并尝试从在线示例中学习,但我陷入了困境,我无法看到如何正确地做到这一点。
答案 0 :(得分:1)
“使用Element子元素作为另一个元素中的属性”听起来有点奇怪。您可以将引用到属性中的其他元素。这就是我想你想要的:
<!ELEMENT library (authors | books)*>
<!ELEMENT authors (author)*>
<!ELEMENT author (#PCDATA) >
<!ATTLIST author aid ID #REQUIRED> <!-- 'aid' is of type ID -->
<!ELEMENT books (book)*>
<!ELEMENT book (title, stock, publisher, year, use*)>
<!ATTLIST book bookID ID #REQUIRED
author IDREFS #REQUIRED > <!-- 'author' is of type IDREFS -->
<!ELEMENT title (#PCDATA) >
<!ELEMENT stock (#PCDATA) >
<!ELEMENT publisher (#PCDATA) >
<!ELEMENT year (#PCDATA) >
<!ELEMENT use (#PCDATA) >
<!ATTLIST use type ( advanced | reference | introductory | professional) #REQUIRED >
类型IDREFS的属性用于引用文档中其他元素的ID值。