1 / 29

XML Example

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>

rafiki
Download Presentation

XML Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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>

  2. XML Namespace <?xml version="1.0"?> <inventory> <drink> <lemonade item-code="drink-1023"> <price>$2.50</price> <amount>20</amount> </lemonade> </drink> </inventory>

  3. 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>

  4. 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?

  5. 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/?

  6. 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>

  7. <a:E1 xmlns:a=“http://a.com/”> <b:E2 xmlns:b=“http://a.com/”> • Q: Do E1 and E2 belong to the same namespace?

  8. 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)

  9. <?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>

  10. 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

  11. 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)>

  12. 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

  13. 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>

  14. <Author>Hector Garcia-Molina</Author> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> Adding Keys

  15. Adding Keys <Author Ident=“HG”>Hector Garcia-Molina</Author> <Author Ident=“JU”> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author>

  16. 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>

  17. 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>

  18. 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)>

  19. 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)>

  20. 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)>

  21. 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?

  22. XML Schema • “XML way” of defining a schema • XML schema specification itself is an XML document

  23. Example DTD <!ELEMENT Book (Title, Author+, Remark?)> <!ATTLIST Book ISBN CDATA #REQUIRED Edition CDATA #IMPLIED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Remark (#PCDATA)> <!ELEMENT Author (#PCDATA)>

  24. 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?

  25. 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

  26. 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)?

  27. 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>

  28. 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>

  29. 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>

More Related