290 likes | 685 Vues
XML Example. <?xml version="1.0"?> <inventory> <drink> <lemonade item-code="drink-1023"> <price>$2.50</price><amount>20</amount> </lemonade> <pop item-code="drink-1034“> <price>$1.50</price><amount>10</amount> </pop> </drink> <snack>
E N D
XML Example <?xml version="1.0"?> <inventory> <drink> <lemonade item-code="drink-1023"> <price>$2.50</price><amount>20</amount> </lemonade> <pop item-code="drink-1034“> <price>$1.50</price><amount>10</amount> </pop> </drink> <snack> <chips item-code="snack-3473"> <price>$4.50</price><amount>60</amount> </chips> </snack> </inventory>
XML Namespace <?xml version="1.0"?> <inventory> <drink> <lemonade item-code="drink-1023"> <price>$2.50</price> <amount>20</amount> </lemonade> </drink> </inventory>
XML Namespace <?xml version="1.0"?> <inventory> <drink xmlns=“http://oak.cs.ucla.edu/cs144/”> <lemonade item-code="drink-1023"> <price>$2.50</price> <amount>20</amount> </lemonade> </drink> </inventory>
Q: What namespace does element lemonade belong to? • Q: What namespace does element inventory belong to? • Q: What namespace does attribute item-code belong to?
Multiple Namespaces? <?xml version="1.0"?> <inventory> <drink> <lemonade item-code="drink-1023"> <price>$2.50</price> <amount>20</amount> </lemonade> </drink> </inventory> • drink, lemonade: http://oak.cs.ucla.edu/cs144/ • price, amount, item-code: http://xml.com/shopping/?
Multiple Namespaces <?xml version="1.0"?> <inventory> <drink xmlns=“http://oak.cs.ucla.edu/cs144/” xmlns:e=“http://xml.com/shopping”> <lemonade e:item-code="drink-1023"> <e:price>$2.50</e:price> <e:amount>20</e:amount> </lemonade> </drink> </inventory>
<a:E1 xmlns:a=“http://a.com/”> <b:E2 xmlns:b=“http://a.com/”> • Q: Do E1 and E2 belong to the same namespace?
Notes on XML Namespace • Namespace specification is valid for the node and all its descendants • Namespace URL does not have to point to anything. Just an identifier • Both elements and attributes can have a namespace • Default namespace is valid only for elements (not for attributes)
<?xml version="1.0"?> <Bookstore> <Book ISBN="0130353000" Price="$65" Ed="2nd"> <Title>First Course in Database Systems</Title> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> </Book> <Book ISBN="0130319953" Price="$75"> <Title>Database Systems: Complete Book</Title> <Author>Hector Garcia-Molina</Author> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> <Remark>It's a great deal!</Remark> </Book> </Bookstore>
ISBN ISBN Price Price Ed Tree Representation Bookstore Book Book Title Author Title Author Author Remark FC FN LN DS HGM FN LN buy… J U J U
DTD <!ELEMENT Bookstore (Book*)> <!ELEMENT Book (Title, Author+, Remark?)> <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Ed CDATA #IMPLIED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA| (First_Name, Last_Name))> <!ELEMENT Remark (#PCDATA)> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)>
Ed ISBN ISBN Price Price Authors Alternative Representation? Bookstore Book Book Author Author Authors Title Title Remark HGM FN LN FC DS buy… J U
Add reference to the authors (IDREF type attribute) Assign a unique “key” to each author (ID type attribute) <?xml version="1.0"?> <Bookstore> <Book ISBN="0130353000" Price="$65" Ed="2nd"> <Title>A First Course in Database Systems</Title> </Book> <Book ISBN="0130319953" Price="$75"> <Title>Database Systems: Complete Book</Title> <Remark>It's a great deal!</Remark> </Book> <Author>Hector Garcia-Molina</Author> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> </Bookstore>
<Author>Hector Garcia-Molina</Author> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> Adding Keys
Adding Keys <Author Ident=“HG”>Hector Garcia-Molina</Author> <Author Ident=“JU”> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author>
Adding References <Book ISBN="0130353000" Price="$65” Ed="2nd"> <Title>A First Course in Database Systems</Title> </Book> <Book ISBN="0130319953" Price="$75"> <Title>Database Systems: The Complete Book</Title> <Remark>It's a great deal!</Remark> </Book>
Adding References <Book ISBN="0130353000" Price="$65“ Ed="2nd“ Authors=“JU”> <Title>A First Course in Database Systems</Title> </Book> <Book ISBN="0130319953" Price="$75" Authors=“HG JU”> <Title>Database Systems: The Complete Book</Title> <Remark>It's a great deal!</Remark> </Book>
Specifying Keys and References in DTD <!ELEMENT Bookstore (Book*, Author*)> <!ELEMENT Book (Title, Remark?)> <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Edition CDATA #IMPLIED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Remark (#PCDATA)> <!ELEMENT Author (#PCDATA| (First_Name, Last_Name))> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)>
Specifying Keys and References in DTD <!ELEMENT Bookstore (Book*, Author*)> <!ELEMENT Book (Title, Remark?)> <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Edition CDATA #IMPLIED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Remark (#PCDATA)> <!ELEMENT Author (#PCDATA| (First_Name, Last_Name))> <!ATTLIST Author Ident ID #REQUIRED> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)>
Specifying Keys and References in DTD <!ELEMENT Bookstore (Book*, Author*)> <!ELEMENT Book (Title, Remark?)> <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Edition CDATA #IMPLIED Authors IDREFS #REQUIRED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Remark (#PCDATA)> <!ELEMENT Author (#PCDATA| (First_Name, Last_Name))> <!ATTLIST Author Ident ID #REQUIRED> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)>
DTD Syntax Summary <!ELEMENT elm-name (elm-content)> <!ATTLIST elm-name attr-name attr-type default> • attr-type: CDATA, “0”|”1”|”2”, ID, IDREF(S), … • ID values should be unique within an XML document • default: value, #REQUIRED, #IMPLIED(= optional) … (#P)CDATA: for text data • CDATA for attribute • #PCDATA for element content • DTD specification for a XML data • <!DOCTYPE root-element [element declaration]> or • <!DOCTYPE root-element SYSTEM “example.dtd”> • Q: Attribute vs element?
XML Schema • “XML way” of defining a schema • XML schema specification itself is an XML document
Example DTD <!ELEMENT Book (Title, Author+, Remark?)> <!ATTLIST Book ISBN CDATA #REQUIRED Edition CDATA #IMPLIED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Remark (#PCDATA)> <!ELEMENT Author (#PCDATA)>
targetNamespace=“http://oak.cs.ucla.edu/cs144”> <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"> </schema> <element name="Book"> </element> <complexType> </complexType> <sequence> </sequence> <element name="Title" type="string"/> <element name="Author" type="string” minOccurs="1" maxOccurs="unbounded"/> <element name="Remark" type="string” minOccurs="0" maxOccurs="1"/> <attribute name="ISBN" type="string" use="required"/> <attribute name="Edition" type="string"/> Q: What namespace does “Book” belong to?
XML Schema Summary • Enclosed in <schema> … </schema> under namespace http://www.w3.org/2001/XMLSchema • targetNamespace for the namespace of defined elements • If no attributes or subelements: <element name=“…” type=“…”/> • Otherwise: <element name=“…”> <complexType> <element name=“…” type=“…”/> … <attribute name=“…” type=“…”/> </complexType> </element> • Type can be integer, double, string, … or user defined types • ID and IDREF(S) possible for attribute types • Key/keyref should be preferred
Q: DTD vs XML Schema. Which one is more expressive? • Q: Can everything specified in DTD be specified in XML Schema? • Q: Can everything specified in XML Schema be specified in DTD? • Q: Why use DTD (not XML schema)?
XPath Example <AAA> <BBB aaa="111" bbb="222"> <CCC/> <CCC xxx="555" yyy="666" zzz="777"/> </BBB> <BBB aaa="999"> <CCC xxx=“ww"/> <DDD xxx="ww"> <CCC>35</CCC> <EEE/> </DDD> </BBB> <BBB/> </AAA>
XML to Relation (1) <!ELEMENT Book (Title, Author, Remark)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (First_Name, Last_Name)> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)> <!ELEMENT Remark (#PCDATA)> <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED>
XML to Relation (2) <!ELEMENT Book (Title, Author+, Remark?)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (First_Name, Last_Name, Bio)> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)> <!ELEMENT Bio (#PCDATA)> <!ELEMENT Remark (#PCDATA)> <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #OPTIONAL>