Answers to assignment#2

advertisement
Assignment #2
Assignment #2
Due: Nov. 06, 2014
1. (20) The following is a DTD for books. Please produce an XML document
conforming to the DTD. <!DOCTYPE AuthorBook [
<!ELEMENT AuthorBook
(Author*, Book*)>
<!ELEMENT Author
(Name, Address+)>
<!ATTLIST Author
AuthorId
ID
#REQUIRED
writing
INREFS
#IMPLIED
>
<!ELEMENT Name
(#PCDATA)>
<!ELEMENT Address
(Street, City)>
<!ELEMNT Street
(#PCDATA)>
<!ELEMENT City
(#PCDATA)>
<!ELEMENT Book
(Title, Publisher, Year)>
<!ATTLIST
Book
BookIn
ID
#REQUIRED
authorOf
IDREFS
#REQUIRED
>
<!ELEMENT Title
(#PCDATA)>
<!ELEMENT Publisher
(#PCDATA)>
<!ELEMENT Year (#PCDATA)>
]>
Sept. 2014
Yangjun Chen
ACS-7102
1
Assignment #2
<? Xml version = “1.0” encoding = “utf-8” standalone = “yes” ?>
<authorBook>
<Author authorID = “cf” writing = “sw”>
<Name>Carrie Fishes</Name>
<Address>
<Street>123 Maple St.</Street><City>Hollywood</City>
</Address>
<Address>
<Street>5 Locust Ln.</Street><City>Malibu</City>
<Address>
</Author>
<Author authorID = “mh” starredIn = “sw”>
<Name>Mark Hamill</Name><Street>456 Oak Rd.</Street>
<City>Brentwood</City>
</Author>
<Book bookID = “sw” authorOf = “cf mh”>
<Title>Star Wars</title><Publisher>The MIT Press</Publisher><Year>1977</Year>
</Book>
</authorBook>
Sept. 2014
Yangjun Chen
ACS-7102
2
Assignment #2
2. (20) Define an XML-schema which is equivalent to the DTD shown in
Question 1.
<? Xml version = “1.0” encoding = “utf-8” ?>
<xs: schema xmlns: xs = “http://www.w3.org/2001/XMLSchema”>
<xs:complexType name = “authorType”>
<xs: sequence>
<xs: attribute name = “AuthorID” type = “xs: string” />
</xs: attribute name = “Writing” type = “xs: string” />
</xs: sequence>
</xs: complexType>
<xs:complexType name = “bookType”>
<xs: sequence>
<xs: attribute name = “BookID” type = “xs: string” />
</xs: attribute name = “authorOf” type = “xs: string” />
</xs: sequence>
</xs: complexType>
Sept. 2014
Yangjun Chen
ACS-7102
3
Assignment #2
<xs:complexType name = “addressType”>
<xs: sequence>
<xs: element name = “street” type = “xs: string” />
</xs: element name = “city” type = “xs: string” />
</xs: sequence>
</xs: complexType>
<xs: element name = “AuthorBook”>
<xs: complexTyp>
<xs: sequence>
<xs: element name = “Author” type = “authorType”
minOccurs = “0” maxOcurs = “unbouned” />
<xs: complexType>
<xs: sequence>
<xs: element name = “name” type = “xs: string” />
</xs: element name = “Address” type = “addressType”
minOccurs = “1” maxOcurs = “unbouned” />
</xs:sequence>
</xs: complexType>
</xs:element>
Sept. 2014
Yangjun Chen
ACS-7102
4
Assignment #2
<xs: element name = “Book” type = “bookType”
minOccurs = “0” maxOcurs = “unbouned” />
<xs: complexType>
<xs: sequence>
<xs: element name = “title” type = “xs: string” />
<xs: element name = “Publisher” type = “xs: string” />
<xs: element name = “year” type = “xs: string” />
</xs:sequence>
</xs: complexType>
</xs:element>
</xs: sequence>
</xs: complexTyp>
</xs: element>
</xs: schema>
Sept. 2014
Yangjun Chen
ACS-7102
5
Assignment #2
3. (25) Write an algorithm to transform a simplified XPath expression (in which
the subexpressions in any condition can be connected only with , e.g.,
/StarMovieData/Star[//City = “Malibu” and //Street = “123 Maple St.”]/Name) to
a tree structure.
/StarMovieData/Star[//City = “Malibu”]/Name
/a/b[//c = “Malibu” and //d = 5]/e
Assume that an XPath is stored in a character array A:
/ a / b [ / / c = “ M a l i b “
a n d
…
When we scan A, we will meet the following different cases:
‘/’ followed by a character,
‘//’,
‘[’
Comparison symbols: ‘=’, ‘<’, ‘>’, ‘<>’, ‘≠’
Sept. 2014
Yangjun Chen
ACS-7102
6
Assignment #2
Algorithm tree-generation(A):
r := a node labeled with nil;
(* this node is called a virtual root.*)
Scan array A;
If A[i] is ‘/’ followed by a character, create a node
P labeled with the string A[i+1..j] such that A[j+1]
is ‘/’,‘[‘, or a comparison symbol.
i := j + 1.
Make p a /-child of r.
r := p.
If A[i..i+1] = ‘//’, create a node
P labeled with the string A[i+2..j] such that A[j+1]
is ‘/’,‘[‘, or a comparison symbol.
i := j + 1.
Make p a //-child of r.
r := p.
Sept. 2014
Yangjun Chen
ACS-7102
7
Assignment #2
If A[i] is a comparison symbol, create a node p for
the value. Make p a child of r. Label the edge with
the symbol.
If A[i] = ‘[’, recognize the conditions C between the
‘[‘ and the matching ‘]’ following A[i]. Assume that it
is A[j]. Let C = path1 and path2 and … and pathk.
Do the following:
for a = 1 to k do
{ call tree-generation(patha);
assume that Ta be the subtree created by the
recursive call; make it a subtree of r;
}
i := j + 1.
If i = n + 1, remove the virtual node and return the
tree.
Sept. 2014
Yangjun Chen
ACS-7102
8
Assignment #2
4. (20) The following is a DTD for a set of documents on books.
(a) Write an FLWR expression to find all the books authored by D.
Knuth.
(b) Write an FLWR expression to find all books published by
<!DOCTYPE AuthorBook [
Addison Wesley Longman in 2007.
<!ELEMENT AuthorBook
Let $ab = /AuthorBook
For $a in $ab/Author
For $b in $ab/Book
Where $a/Name = ‘D.Knuth’ and
$a/@AuthorID = $b/@authorOf
Return $b/Title
(A
<!ELEMENT Author (Name, Address+
<!ATTLIST Author
AuthorId
ID
writing
IN
>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Address
(S
<!ELEMNT Street (#PCDATA)>
<!ELEMENT City (#PCDATA)>
<!ELEMENT Book (Title, Publisher,
<!ATTLIST
Bo
BookIn
ID
authorOf
ID
>
<!ELEMENT Title
(#P
<!ELEMENT Publisher
(#P
<!ELEMENT Year (#PCDATA)>]>
b)
Let $AuthorBook:= doc(AuthorBook.xml)
For $a in $AuthorBook/AuthorBook/Book
Where $a/Publisher = “Addison Wesley Longman” and $a/Year = “2007”
Return $a/Title
Sept. 2014
Yangjun Chen
ACS-7102
9
Assignment #2
5. (15) According to the above DTD, construct an XPath expression to
find the author’s name who published a book by the publisher
Addision Wesley Longman in 1972.
/authorBook/author/[/@AuthorId = ../book/@authorOf
and
../book/publisher = “Addision Wesley Longman”
and
../book/year = “2007”]/name
Sept. 2014
Yangjun Chen
ACS-7102
10
Download