CIS 275Web App Dev I XML Introduction to
CIS 275—Web App Dev I XML
Introduction to XML n n n e. Xtensible Markup Language XML stands for ____________. HTML was designed to display data. describe data. XML was designed to _____ tags With XML, you define your own _______. Sample 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> 2
How can XML be Used? n n n With XML, data can be stored separately from the HTML code. ______ text format. With XML, data are stored in plain _____ With XML, data can be exchanged between incompatible systems. ______ WML are written in XML. Important languages like _____ B 2 B applications. XML is important for _____ 3
XML Document Structure n Sample 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> n The XML document above consists of n n n declaration an XML ______ root element One ______ child elements Four _______ 4
XML Syntax n n n closing tag. All XML elements must have a _____ case sensitive. XML tags are ______ XML elements must be properly nested. <b><i>This text is bold and italic</i></b> n n root element. All XML documents must have a ______ quoted Attribute values must be _____. <note date="12/11/2002"> n Comments in XML look like this: <!-- This is a comment --> 5
XML Elements n n n extensible New elements can XML documents are _____. be added without crashing an existing application. relationships (there are XML elements have ______ parent, child, and sibling elements). Elements have content n n Some have only element content (e. g. , the root element) text content Some have simple (_____) <title>My First XML</title> n n Some have mixed content (element and simple) Some have no content <prod id="33 -657" media="paper"></prod> n attributes Elements may have ______ 6
Element Content In this example, <book> <? xml version="1. 0" element content, has _____ encoding="ISO-8859 -1"? > because it contains other <book> elements. <title>My First XML</title> mixed n <chapter> has _______ <prod id="33 -657" media="paper"></prod> content because it contains <chapter>Introduction to XML both text and other <para>What is HTML</para> elements. <para>What is XML</para> simple n <para> has ____ </chapter> content (or text content) <chapter>XML Syntax because it contains only text. <para>Elements must have a closing tag</para> n <prod> has _______ <para>Elements must be properly content, because empty it carries nested</para> no information. </chapter> n </book> 7
XML Attributes n Element attributes are not part of the data, but contain information about the data. <file type="gif">computer. gif</file> n n You should try to avoid using attributes, especially if the information feels like basic data. identification One good use of attributes is for _______. <messages> <note id="p 501"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> … 8
XML Validation n n syntax A “well formed” XML document has correct _______. A “valid” XML document is both well formed and Document Type Definition). conforms to a DTD (_____ <? xml version="1. 0" encoding="ISO-8859 -1"? > <!DOCTYPE note SYSTEM "Internal. Note. dtd"> n n n A DTD defines document structure and legal elements ______. Schema is the W 3 C alternative to DTD. An XML _____ Invalid XML documents will not execute properly. See http: //www. w 3. org/XML/Schema. html for details. XML validators: n http: //www. w 3 schools. com/dom_validate. asp 9
Internal DOCTYPE Declaration <? xml version="1. 0" encoding="ISO-8859 -1"? > <!DOCTYPE note [ <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note> n parsed character data. ” #PCDATA means “_______ 10
External DOCTYPE Declaration n XML (note. xml) <? xml version="1. 0"? > <!DOCTYPE note SYSTEM "note. dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</headin g> <body>Don't forget me this weekend</body> </note> n DTD (note. dtd) <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> 11
Why use a DTD? n n With DTD, your XML files can carry a description of format with it. its own _____ With a DTD, independent groups of people can interchanging data. agree to use a common DTD for ______ standard DTD to verify Your application can use a _____ that the data you receive from the outside world is valid. verify your own data. You can also use a DTD to _______ 12
Viewing XML Files n n An XML document can be viewed in IE 5. 0+ by typing the URL or double-clicking the icon. Look at this XML file: note. xml. Invalid XML files will not display: note_error. xml. Other Examples n n n An XML CD catalog An XML plant catalog A Simple Food Menu 13
Displaying XML with CSS n n n You can view an XML with the help of a CSS, but this is not the future of XML. Take a look at this pure XML file: The CD Catalog Then look at this style sheet: The CSS file Finally, view the CD Catalog formatted with the CSS file You can write a web page using XML, but this is not recommended: A homepage written in XML. XHTML ______ (HTML defined as XML) is recommended for creating web pages. 14
Displaying XML with XSL n n n e. Xtensible Style Language) is the preferred XSL (_____ style sheet language of XML. XSL is far more sophisticated than CSS. If you have Netscape 6 or IE 5 or higher you can view the XML file and the XSL style sheet. View the result in IE 6 View the result in IE 5 15
XML Embedded in HTML n <xml> tag is used to embed XML The unofficial ______ data within HTML. <xml id="note"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </xml> n You can embed a separate XML file. <xml id="note" src="note. xml"> </xml> 16
Data Binding n Example of binding a data island to an HTML <table> element (in the following case, _______): <html> <body> <xml id="cdcat" src="cd_catalog. xml"></xml> <table border="1" datasrc="#cdcat"> <tr> <td><span datafld="ARTIST"></span></td> <td><span datafld="TITLE"></span></td> </tr> </table> </body> </html> n n Here is the result. Here is the external XML file. 17
- Slides: 17