Programming the Web Using Visual Studio NET XML
Programming the Web Using Visual Studio. NET XML
XML BASICS
XML Basics o Benefits n n n Easy to format Describes any type of data Machine-readable Human-readable Data can be distributed Defines the meaning of data
XML o o e. Xtensible Markup Language Not actually a markup language n o Specification for making markup languages XML documents have two fundamental characteristics n n Must be “well-formed” May be associated with a DTD or XML schema
XML o Well-formed n o Must comply with XML syntax rules DTD – Document Type Definition n n Dictates what elements and attributes are permitted and allowed format Example <img src=“eiffel. jpg” alt=“Eiffel Tower”> o <img> element (tag) o src and alt: attributes
XML o Why is XML so popular? n n o It’s flexible It enables you to create your own documents with elements (tags) that you define Non-XML example: <p>This is a paragraph of text</p> o o What does this tell you? What does it tell a program?
XML o What does the browser see? <p> Content the browser posts (copies) but does not process as instructions. Etc. etc. </p>
XML o XML example: <pets> <html> <body> <pet> <p> <type>Dog</type> <img…> <name>Rover</name> <a href=…> <age>12</age> <script…> <owner>Dave</owner> <form…> </body> </pet> </html> </pets>
XML o o o XML tags provide meaning or context Programs can interpret these meanings XML is ideal for sharing data between programs One user can encode data using XML and share it with others A different user can interpret that data
XML o XML documents may have three parts n n n Prolog Body Epilog
XML o Prolog n n n o Comments & processing instructions Version information Reference to a specific XML DTD or schema Body n n n One or more elements Defined by the DTD or schema Forms a hierarchical tree structure o o o One top-level element All others below it in the hierarchy Epilog n Comments & processing instructions
XML o Why is XML important? n n o Forms the basis for web services Use by businesses to exchange information How is XML different from HTML? n HTML is fairly forgiving of errors o n will probably work OK HTML can mix upper and lower case in tags o n <p>This is a paragraph <Tab. Le>…</table> is ok Attribute values don’t have to be enclosed in quotes o <font color = #FF 0000> is ok
XML o Bottom line: n Poorly-written HTML documents o o o XML is not forgiving n o Usually no big deal Usually kind of work (at least close enough) You have to follow the rules What are the rules to remember?
XML – XHML Connection o XHTML is XML n o With rules to implement HTML tags XHTML documents must have a DOCTYPE declaration <!DOCTYPE html PUBLIC ¬ “-//W 3 C//DTD XHTML 1. 0 Transitional//EN” ¬ http: //www/w 3/org/TR/xhtml/11/DTD/xhtml 1 transitional. dtd> n Must occur before the root element
XHTML o Root element n Top of the hierarchy of elements o n n Only one root element allowed No other elements before it o n o May have “processing” instructions however No other elements after the ending tag o n <html> in HTML </html> May have post processing commands Lowercase only (XML is case sensitive)
XHTML o Attribute values must be encased in quotes n n HTML: <img … width=72> XML: <img … width=“ 72”> o Attribute values must not be minimized o That is, the name must have a value n That’s why selected=“selected” Spaces in attribute values will be removed n
XHTML o Only the id attribute is used to uniquely identify an element n o The name attribute for an element is not recognized Each starting tag must have a matching ending tag n n HTML: XML: <br/> o o XML “shorthand” for non-enclosing tags Elements must be nested, not overlapping n HTML will “allow” but may do strange things
XHTML o <script> and <style> types of elements must be marked as CDATA areas <script language=”Javascript”> <![CDATA[ function clickalert() { alert(“VSNET is cool!”) } ]]> </script>
XML DTD
XML Basics o Two types of XML document definitions: n DTD – Document Type Definition o o o n Defines the elements (tags) and attributes (properties) allowed in an XML document Ubiquitous, but “old school” Inherited from SGML XSD – XML Schema Definition o o o More powerful than DTDs Uses XML Schema Based on XML format
XML Basics o Well-formed or Valid? n Well-formed XML documents conform to basic XML syntax o o o n Has exactly one root All elements completely contain elements within them All elements have “ending” tags Valid XML documents conform to a DTD o Conforms to the data format defined in the DTD
XML Basics o DTDs can be referenced n n o Externally (i. e. , in a separate file) via DOCTYPE Internally within the XML document Multiple DTDs can be referenced n n Blended XML document Relies on several sets of valid elements and attributes
XML Basics o Schema n n n Similar to a DTD Written in XML format Richer set of tools for creating elements and attributes o o Easier to specify data types XSD n XML Schema Definition
XML Basics o Using XML allows developers to: n n o Makes it easier for: n n o Define data explicitly Make unique, well-defined data structures to pass data from one application to another Web applications to communicate and work together Applications to communicate data Example …
XML Basics <clients> <client> <name>Oliver Wendell Douglas</name> <phone>510 -555 -1212</phone> </client> <name>Fred Ziffle</name> <phone>510 -555 -3456</phone> </clients>
XML Basics o <clients> What does this tell an <client> <name>Emerson Cod</name> application reading it? <phone>510 -555 -1212</phone> n Has some “clients” o For one “client” n n o </client> </clients> 1 st item is a “name” 2 nd item is a “phone” Without a DTD the receiving application doesn’t “know” anything about name or phone
XML Basics o o With a DTD or a schema, additional information or rules can be conveyed E. g. : n n o A client may have only one name A name must consist of alphabetic characters only Parties sharing data must agree on n n Meaning of each element Action to be taken on each element
XML Basics o XML vocabularies help accomplish this purpose n n n Major players in an industry develop a vocabulary Anyone wishing to use that player’s data must conform to that vocabulary Basically like a protocol o A set of rules commonly agreed upon
XML Basics o Everyone who has adopted a DTD or schema or vocabulary n n Knows what every other party using that DTD/schema/vocabulary means Can program their applications to o Share data Share processing Communicate
XML Basics o XML documents n n May contain all parts in one file May also be composed of separate sections in files distributed across the Internet o o Tags here Data there
XML Basics o o XML document parts are entities Entities n n Have names Contain o o Data or references to data References are in a URL format
XML Basics o XML vocabulary n o Definitions of elements and attributes contained within a DTD or schema Think of a vocabulary as a mini-dictionary n Defines only those words and phrases that are permitted in a certain situation
XML Basics o Names used for XML structures must follow rules: n 1 st character must be a letter, underscore, or colon o o n n Cannot be a number Colons are never used except in namespace aliases 2 nd and subsequent characters can be any Unicode character Characters 1 -3 cannot be “xml” in any combination of cases (x. ML, XMl, xml, etc. )
XML Document Prolog o o Well-formed documents must have a body They may have n n o A prolog at the beginning An epilog at the end Prolog can include an XML declaration specifying n n Version of XML used Encoding & stand-alone attributes
XML Document Prolog o Example: <? xml version=“ 1. 0” encoding=“UTF-8” standalone=“yes” ? > o Version enables applications reading this XML document to be able to know n n Rules to use for each element How to decide if the document is “well-formed” or not
XML Document Prolog o o o Encoding specifies how the characters used in the document were constructed Standalone tells user if any entity declarations are contained in an external DTD XML declaration should be the first line in the prolog
XML Document Prolog You can write your own DTD - or - o Use one that someone else has written and published o By referencing a DTD o n XML processors can check if your document is o o o Well-formed Valid Sharing documents works if everyone is using the same DTD
XML Document Prolog o External DTD <? xml version=“ 1. 0” encoding=“UTF-8” standalone=“yes” ? > <!DOCTYPE WService SYSTEM “http: //www. servata. com/DTD 02”> n n “WService” is the name of the root element “SYSTEM” means a URL is being used to reference the DTD
XML Document Prolog o Internal DTD <? xml version=“ 1. 0” encoding=“UTF-8” standalone=“yes” ? > <!DOCTYPE WService [ <!ENTITY WSNM “Web Service Name”> ]>
XML Document o o XML documents declare the elements and attributes that will be used XML elements that contain content must be written with both a starting and ending tag <my. Element>content goes here</my. Element> o XML elements without content may be terminated with a slash: <my. Element/>
XML Document o o Elements are organized hierarchically Root element is at the “top” n o o Root element can occur only once Child elements can occur many times Elements may contain n Attributes o n n Always written into the starting tag as name-value pairs Other elements Other XML structures
XML Document <city> <street name=“Tryon Street”> <address>101</address> <address>102</address> </street> <street name=“Trade Street”> <address>201</address> <address>202</address> </street> </city>
XML Document o o Both elements and attributes may hold data Attributes may occur only once in an element n o In any order if multiple attributes are present Should you use an element or an attribute? n n No rules that require one over the other Use which way fits the context
XML Document Which is better? <city> <street> <name>Tryon Street</name> <address>201</address> <address>202</address> </street> </city> - or <city> <street name=“Trade Street”> <address>201</address> </street> </city> Assumptions: only one street name allowed, multiple addresses allowed Remember in the “real world” you will only choose one consistent way!
Writing Document Type Definitions o A DTD defines n Elements o n Attributes o n n “Things” the document works with Properties associated with an element Entities Notations
Writing Document Type Definitions o Content is the data in an element n n o May exist between the starting and ending tags May exist in child elements Each element has a specific syntax
Writing Document Type Definitions o Content may be in one of 4 “models” n EMPTY o n ANY o n No restrictions on content MIXED o n Elements that have no content like <br/> May contain child elements, text data, attributes CHILDREN o May have child elements and attributes but no text data
Writing Document Type Definitions o Element syntax n General form: <!ELEMENT n name model Examples > Order shown <!ELEMENT myelement 1 ANY> <!ELEMENT myelement 2 (child 01, child 02)> <!ELEMENT myelement 3 (child 01 | child 02)> One OR the other
Examples o Multiple Children n a+ - One or more occurrences of a n a* - Zero or more occurrences of a <!ELEMENT BOOK (CHAPTER)+> <!ELEMENT List (Object)*> n a? - a specific item or nothing n a, b - a followed by b n a | b - a or b but not both n (expression) - expression treated as a unit <!ELEMENT Table (plate)? > <!ELEMENT SUM (op 1, op 2)> <!ELEMENT POINT (COORDINATES | POLAR)> <!ELEMENT CHAPTER (INTRODUCTION, (P | QUOTE | NOTE)*, DIV*)>
Writing Document Type Definitions o Attributes <!ATTLIST myelement > myatt 1 ID myatt 2. . . CDATA Character data Unique name #REQUIRED #IMPLIED Must appear May appear
Example o DTD n o <!ELEMENT Rectangle EMPTY> <!ATTLIST Rectangle length CDATA "0 px" width CDATA "0 px"> Use n <Rectangle width="80 px" length="40 px"/>
Extra References o o XML DTD by John Punin DEAD Link
XML Schema o In databases, a schema defines n n o Structure Constraints Usually reflects business rules n n Customer SSN must be part of account information Therefore, cannot add customer record without an SSN
XML Schema o o XML schemas function similarly to DB schemas Like a DTD, schemas define items n Allows more control over elements and attributes o o Frequency of occurrence Order of appearance Allowable data types Custom data types
XML Schema o o o An XML document may be connected to more than one schema Possibility of conflict with elements having the same name An XML namespace n n Differentiates between items with the same name From different schemas
XML Schema o o Example: telephone numbers You may have the same number as someone in a different part of the country n n o How do you tell them apart? Add an area code An XML namespace performs the same function
XML Schema o Different authors may use the same XML element or attribute names n n n But the items may have completely different formats and purposes How is an XML document to “understand” which definition to use? By use of a namespace
Closing Quotes o "XML is a giant step in no direction at all" n o "Some people, when confronted with a problem, think 'I know, I’ll use XML. ' Now they have two problems. " n o Erik Naggum dirtsimple. org[6] "XML is like violence - if it isn't working properly, you aren't using enough of it. “ n Anon
Assignment o Preview: Create an XML file and display it on a browser n n Lab 1 – XML 2005 Class exercise in Woodward 335
- Slides: 59