IS 373—Web Standards Todd Will Intro to XML IS 373—Web Standards Todd Will
CIS 373---Web Standards-XML The Program Intro to XML How XML Is Used XML Syntax Elements Validation Browser Support Real Life Example CIS 373---Web Standards-XML
CIS 373---Web Standards-XML XML Introduction XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to describe data XML tags are not predefined. You must define your own tags XML uses a Document Type Definition (DTD) or an XML Schema to describe the data XML with a DTD or XML Schema is designed to be self-descriptive XML is a W3C Recommendation (a standard setting body) CIS 373---Web Standards-XML
CIS 373---Web Standards-XML XML IS NOT HTML XML was designed to carry data. XML is not a replacement for HTML. XML and HTML were designed with different goals: XML was designed to describe data and to focus on what data is. HTML was designed to display data and to focus on how data looks. HTML is about displaying information, while XML is about describing information. CIS 373---Web Standards-XML
XML Does not “Do” Anything XML was not designed to DO anything. XML was structures, stores, and sends information. The following example is a note to Tove from Jani, stored as XML: <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> You can see this has a body, to, from, and a subject, but you need another program to read and display this data CIS 373---Web Standards-XML
XML Is Free and Extensible No predefined tags HTML uses predefined tags (like <p>, <h1>, etc.). XML allows the author to define his own tags and his own document structure Tags are invented by the author of the document and describe the data contained VERY FLEXIBLE! CIS 373---Web Standards-XML
CIS 373---Web Standards-XML XML Uses XML can Separate Data from HTML XML can store required data outside of the html page Allows the use of xml to store data and html to display data More elegant than putting both on the same page XML can exchange data Data can be moved between two incompatible systems Data can be exchanged over the Internet Data can be read by many different applications (i.e. word, PDF, PowerPoint) Can be applied to Business to Business since data does not need to be reentered Time and cost savings CIS 373---Web Standards-XML
CIS 373---Web Standards-XML XML Uses (cont) XML Can share data XML stored in plain text Hardware and software independent Data can be used between old and new programs alike easily Data can be read and displayed in several different browsers XML can store data Data can be stored in plain text files or databases Generic applications can display the data (no need for proprietary software Data can be easily formatted or changed to enable people with disabilities to access the system Can be understood by voice readers Can easily change the formatting of the page to better suit blind people CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Syntax Example XML document <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> CIS 373---Web Standards-XML
CIS 373---Web Standards-XML XML Syntax Broken Down <?xml version="1.0" encoding="ISO-8859-1"?> XML declaration Defines XML version (1.0) Defines character encoding (ISO-8859-1) Latin-1/West European character set All xml documents will have this declaration at the top of the document CIS 373---Web Standards-XML
CIS 373---Web Standards-XML XML Syntax Broken Down Root element <note> </note> This syntax describes the root element of the data in the document Should describe the information contained in the document In this case, the document is a note CIS 373---Web Standards-XML
CIS 373---Web Standards-XML XML Syntax Broken Down Child elements <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> All child elements should pertain to the root node XML is very descriptive Anyone should be able to look at the document and be able to understand it CIS 373---Web Standards-XML
XML Syntax – Closing Tags Illegal to omit closing tag when declaring an open tag In HTML, not all tags require a closing tag <p> this is a paragraph is legal in html XML syntax: <name>Todd is not legal <name>Todd</name> is legal XML root elements do not have a closing tag You should not have </xml> in a document The document can still be opened and read Root element is NOT an XML element Microsoft IE does require closed tags CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Elements <book> <title>My First XML</title> <prod id="33-657" media="paper"></prod> <chapter>Introduction to XML <para>What is HTML</para> <para>What is XML</para></chapter> <chapter>XML Syntax <para>Elements must have a closing tag</para> <para>Elements must be properly nested</para> </chapter> </book> Book is the root element Title, Prod, Chapter are child elements of book Title, Prod, Chapter are sister (or sibling elements) CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Element Naming Rules Names can contain letters, numbers, and other characters Names must not start with a number or punctuation character Names must not start with the letters xml (or XML, or Xml, etc) Names cannot contain spaces Element names should be descriptive but not overly long CIS 373---Web Standards-XML
Are these valid element names? 1Book Book_Title Book Title Element names can also contain special and non-English characters but only can be read if the software supports them An XML document is often created from a database (good practice to use database field names for xml element names) CIS 373---Web Standards-XML
Elements can also contain attributes <person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname> </person> Is The Same As <person> <sex>female</sex> Rule of thumb is to put data into elements, not as attributes CIS 373---Web Standards-XML
Avoid using attributes? Why you shouldn’t: Attributes cannot contain multiple values (child elements can) Attributes are not easily expandable (for future changes) Cannot easily add new data elements Attributes cannot describe structures (child elements can) Attributes are more difficult to manipulate through code Difficult to compare to a Document Type Definition (DTD) - which is used to define the legal elements of an XML document If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data. CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Do NOT Do This! <note day="12" month="11" year="2002" to="Tove" from="Jani" heading="Reminder" body="Don't forget me this weekend!"> </note> CIS 373---Web Standards-XML
Well Formed XML Documents A "Well Formed" XML document has correct XML syntax. A "Well Formed" XML document must conform to the rules as previously discussed XML documents must have a root element XML elements must have a closing tag XML tags are case sensitive XML elements must be properly nested XML attribute values must always be quoted <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Conform to a DTD XML documents should conform to a previously agreed upon Document Type Definition (DTD) <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE note SYSTEM "InternalNote.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> InternalNote.dtd is the Definition CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Validate XML Document If software encounters an error in the document, the processing should stop Rationale is that all documents should be compatible Example – Internet Explorer throws an error if the document is not well formed In HTML, the browser can still understand the tags and can still look right even with several errors CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Browser Support Most major browsers support XML Mozilla Firefox Version 1.0.2 support for XML (and CSS). Mozilla Expat for XML parsing and displays XML + CSS. Mozilla also has some support for Namespaces. Netscape As of version 8, Netscape uses the Mozilla engine, and therefore it has the same XML support as Mozilla. Opera As of version 9, Opera has support for XML (and CSS). Version 8 supports only XML + CSS. Internet Explorer As of version 6, Internet Explorer supports XML, Namespaces, CSS, and XPath. CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Real Life XML XML News XML based news exchange Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Example XML News <?xml version="1.0" encoding="ISO-8859-1"?> <nitf> <head> <title>Colombia Earthquake</title> </head> <body> <headline> <hl1>143 Dead in Colombia Earthquake</hl1> </headline> <byline> <bytag>By Jared Kotler, Associated Press Writer</bytag> </byline> <dateline> <location>Bogota, Colombia</location> <date>Monday January 25 1999 7:28 ET</date> </dateline> </body> </nitf> CIS 373---Web Standards-XML
CIS 373---Web Standards-XML Conclusion XML is very useful to carry data Cannot be used to display data Should be well formed (all open tags should have a close tag) Most programs today store some data in XML format Do a search on your hard drive for *.xml CIS 373---Web Standards-XML
CIS 373---Web Standards-XML For Next Week XML homework assignment (see class site for details) Can start work on this now if you want Any problems, please contact me Read Zeldman chapter 8 CIS 373---Web Standards-XML