1 / 32

Review

Review. Writing XML S tyle Common errors. Week 1 Review: 1. Style. 1. Style issues. Be consistent: <title>The Title</title> <Description>The description</Description> (same for camelCasing vs under_scores ). Week 1 Review: 1. Style. 2 . Common errors. Be sure to group related items.

osanna
Download Presentation

Review

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. Review Writing XML Style Common errors XML Technologies - 2012 - David Raponi

  2. Week 1 Review: 1. Style 1. Style issues Be consistent: <title>The Title</title> <Description>The description</Description> (same for camelCasingvsunder_scores) XML Technologies - 2012 - David Raponi

  3. Week 1 Review: 1. Style 2. Common errors Be sure to group related items <book>The book</book> <ISBN>123142</ISBN> <book>Another book</book> <ISBN>1412314124</ISBN> <book> <name>The book</name> <ISBN>123142</ISBN> </book> <book> <name>Another book</name> <ISBN>1412314124</ISBN> </book> XML Technologies - 2012 - David Raponi

  4. Week 1 Review: 1. Style 2. Common errors Avoid repeated names. Examine the following: • <band> • <name></name> • <member> • <name> • <first></first> • <last></last> • </name> • </member> • </band> • Something that holds text • Something that holds more elements Eek! We’re using “name” to define two different structural things. XML Technologies - 2012 - David Raponi

  5. Week 1 Review: 1. Style 2. Common errors No need to increment elements! • <library> • <book1></book1> • <book2></book2> • </library> Hey! What’s the name of that book2 you’re reading? (That doesn’t make sense.) Tag names should be thought of as pure nouns. In this case, just <book>, forget the numbers. XML Technologies - 2012 - David Raponi

  6. XML Technologies Week 2: Document Type Definitions (DTD) What is a DTD? How to write a DTD? XML Technologies - 2012 - David Raponi

  7. Week 2: DTDs > 1. What is a DTD? 1. What is a DTD? Recall: XML is a language that can make new markup languages This involves two steps: Creating own tags and structure (last week) Defining that structure (DTDs, this week) XML Technologies - 2012 - David Raponi

  8. Week 2: DTDs > 1. What is a DTD? Define the blocks W3C: “The purpose of a DTD is to define the legal building blocks of an XML document.” A successful check of an XML file against a DTD makes it “valid” for that DTD (not just well-formed) Note: It still doesn’t DO anything  Sorry. … so why bother? XML Technologies - 2012 - David Raponi

  9. Week 2: DTDs > 1. What is a DTD? So why bother? Tons of data being passed around + Tons of keeners coming up with their own way to manipulate the data = The Apocalypse XML Technologies - 2012 - David Raponi

  10. Week 2: DTDs > 1. What is a DTD? Standards: ensuring predictability With a DTD, you: restrict the data content and how that content is organized This is important because if several people are passing along updated versions of an XML file, and each is trying to be clever by adding/changing things, then the file is no longer “predictable”, which would make future processing of that XML file cumbersome. The more predictable the structure of an XML file, the easier it is to work with it XML Technologies - 2012 - David Raponi

  11. Week 2: DTDs > 1. What is a DTD? HTML goes XHTML • Compare: • <!DOCTYPE html public “-//W3C//DTD HTML 4.01 Strict//EN” “http://www.w3.org/TR/html4/strict.dtd”> • <!DOCTYPE html public “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> • Q: What are some differences? XML Technologies - 2012 - David Raponi

  12. Week 2: DTDs > 2. How to write a DTD? 2. How to write a DTD? • To write a DTD, ask yourself these questions: • What elements are there? • How many times are those elements allowed to appear? • What is inside those elements • What attributes are there? XML Technologies - 2012 - David Raponi

  13. Week 2: DTDs > 2. How to write a DTD? Declaring an element <!ELEMENT name contents> <!ELEMENT name (further subelements)> • Empty elements (like hr and br tags) • <!ELEMENT hr EMPTY> • Parsed Character Data (your text) • <!ELEMENT title (#PCDATA)> • Any Data • <!ELEMENT title ANY> • Elements with subelements • <!ELEMENT div(h1, p)> XML Technologies - 2012 - David Raponi

  14. Week 2: DTDs > 2. How to write a DTD? Declaring a subelement’s recurrence • How many times does the subelement appear? • Once  (do nothing, default) • Once or more  elem+ • Zero or more  elem* • Zero or once  elem? • Either / or  (elem1|elem2) XML Technologies - 2012 - David Raponi

  15. Week 2: DTDs > 2. How to write a DTD? Declaring a subelement’s recurrence • Recurrence examples: • <!ELEMENT book (title)> • <!ELEMENT person (child*)> • <!ELEMENT book (chapter+)> • <!ELEMENT band (record_label?)> • <!ELEMENT gnathostome (fins+|legs+)> *gnatho-what?! Dude, it’s any animal that has a jaw. Doesn’t everyone know that? XML Technologies - 2012 - David Raponi

  16. Week 2: DTDs > 2. How to write a DTD? Declaring an attribute <!ATTLIST elem-name attr-name typedefault-value> • Type: • CDATA (attribute values are not parsed) • Enumerated (this|that|another) – specific values • Value: • #REQUIRED (must be present, and can’t be “ “) • #IMPLIED (attr may/may not be there) • #FIXED “value” (attr must always have this value) • “value” (sets a default, but it can be changed) XML Technologies - 2012 - David Raponi

  17. Week 2: DTDs > 2. How to write a DTD? Declaring an attribute <!ATTLIST elem-name attr-name typedefault-value> Enumeration Examples: <! ATTLIST person status (single|married) #REQUIRED> <! ATTLIST person status (single|married) #IMPLIED> CDATA Examples: <! ATTLIST person name CDATA #REQUIRED> <! ATTLIST person status CDATA #IMPLIED> <! ATTLIST person gender CDATA #FIXED “female”> <! ATTLIST person gender CDATA “female”> XML Technologies - 2012 - David Raponi

  18. Week 2: DTDs > 2. How to write a DTD? Declaring an attribute: special note! • Spaces • Recall: Spaces are NOT allowed in tag names • Recall: Spaces ARE allowed in attribute values • BUT: not allowed in DTD enumerated values! • Ex: <!ATTLIST name attr (word|two words) #IMPLIED>(this is bad) • In other words: You can have well-formed XML docs with spaces in attributes, but it may not validate with a DTD. • In general: Avoid spaces in attributes! • Instead, use _, -, or if you need to. XML Technologies - 2012 - David Raponi

  19. Week 2: DTDs > 2. How to write a DTD? Putting it together (program_v1.xml) Please open up program_v1.xml and look at it as we go through these slides… XML Technologies - 2012 - David Raponi

  20. Week 2: DTDs > 2. How to write a DTD? Putting it together (program_v1.xml) • Begin with a doctype declaration: • <!DOCTYPE root_element [elements]> <!DOCTYPE program [ …(other elements to follow) ]> XML Technologies - 2012 - David Raponi

  21. Week 2: DTDs > 2. How to write a DTD? DTD for program_v1.xml • Now list the next element (don’t forget the root itself!) and any sub elements it may contain • <!ELEMENT elem (subelem, subelem…)> <!DOCTYPE program [ <!ELEMENT program (title, semester)> ]> XML Technologies - 2012 - David Raponi

  22. Week 2: DTDs > 2. How to write a DTD? DTD for program_v1.xml Add recurrences:(how many times the subelements appear): <!DOCTYPE program [ <!ELEMENT program (title, semester+)> ]> XML Technologies - 2012 - David Raponi

  23. Week 2: DTDs > 2. How to write a DTD? DTD for program_v1.xml • To move on, state the contents of the elements in the order you listed them. The contents are one of three options: • Text • Another element (the cycle continues) • Both (a “mixed element”) XML Technologies - 2012 - David Raponi

  24. Week 2: DTDs > 2. How to write a DTD? DTD for program_v1.xml Updated: <!DOCTYPE program [ <!ELEMENT program (title, semester+)> <!ELEMENT title (#PCDATA)> <!ELEMENT semester (course*)> ]> XML Technologies - 2012 - David Raponi

  25. Week 2: DTDs > 2. How to write a DTD? DTD for program_v1.xml Now keep cycling through until the elements are all accounted for: Updated: <!DOCTYPE program [ <!ELEMENT program (title, semester+)> <!ELEMENT title (#PCDATA)> <!ELEMENT semester (course*)> <!ELEMENT course (#PCDATA)> ]> XML Technologies - 2012 - David Raponi

  26. Week 2: DTDs > 2. How to write a DTD? DTD for program_v1.xml Then finish up with attributes: Updated: <!DOCTYPE program [ <!ELEMENT program (title, semester+)> <!ELEMENT title (#PCDATA)> <!ELEMENT semester (course*)> <!ELEMENT course (#PCDATA)> <!ATTLIST semester number CDATA #REQUIRED> ]> XML Technologies - 2012 - David Raponi

  27. Week 2: DTDs > 2. How to write a DTD? To sum up: • Doctypes are (basically) written as follows: • Wrap everything in the <!DOCTYPE… • Then list the elements with recurrences • List what’s inside those elements • Text • More elements? • Repeat steps 2 and 3 until all elements are accounted for • Declare attributes XML Technologies - 2012 - David Raponi

  28. Week 2: DTDs > 2. How to write a DTD? Attaching a DTD to an XML file • Internal • Just place the whole DTD right after your <?xml version=“1.0”?> declaration • External • Remove the initial <!DOCTYPE declaration • Save your DTD as a .dtd file • Add the following to your .xml file • <!DOCTYPE root_elem SYSTEM “location-of-dtd-file”> XML Technologies - 2012 - David Raponi

  29. Week 2: DTDs > 2. How to write a DTD? Other notes CAPITALIZATION Note that <!ELEMENT and <!ATTLIST must be in CAPS Indenting The convention is to NOT indent your DTD items XML Technologies - 2012 - David Raponi

  30. Week 2: DTDs > 2. How to write a DTD? Other notes CDATA Character Data (XML engine will NOT parse the info) because the value is just text (no markup) and so it simply spits it out to screen #PCDATA Parsed Character Data (XML engine will try to interpret the contents before spitting it out) to check if there’s actually another node <elem></elem> or special characters.  What you need to know: CDATA for attributes, #PCDATA for elements with just text XML Technologies - 2012 - David Raponi

  31. Week 2: DTDs > 2. How to write a DTD? Other notes Mixed Data <elem> Some text <subelem>More text</subelem> </elem> If you do this, let me know so I can come over and yell at you. But if you MUST, then you’d declare it like so: <!ELEMENT elem ANY> … or … <!ELEMENT elem (#PCDATA|subelem)*> <!ELEMENT subelem (#PCDATA)> XML Technologies - 2012 - David Raponi

  32. Week 2: DTDs > 2. How to write a DTD? Other notes • XML docs are made to follow DTDs, NOT the other way around: • <?xml version=“1.0” encoding=“utf-8”?> • <!DOCTYPE band [ • <!ELEMENT band (tour)> • <!ELEMENT tour EMPTY> • ]> • <band> • <tour></tour> • </band> • Q: Why is this technically correct, but conceptually wrong? XML Technologies - 2012 - David Raponi

More Related