Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Fundamentals.

Similar presentations


Presentation on theme: "Chapter 1 Fundamentals."— Presentation transcript:

1 Chapter 1 Fundamentals

2 1.1 A Brief Intro to the Internet
- Origins - ARPAnet - late 1960s and early 1970s - Network reliability - For ARPA-funded research organizations - BITnet, CSnet - late 1970s & early 1980s - and file transfer for other institutions - NSFnet - Originally for non-DOD funded places - Initially connected five supercomputer centers - By 1990, it had replaced ARPAnet for non- military uses - Soon became the network for all (by the early 1990s) - NSFnet eventually became known as the Internet © 2006 Pearson Addison-Wesley. All rights reserved.

3 - A world-wide network of computer networks
What the Internet is: - A world-wide network of computer networks - At the lowest level, since 1982, all connections use TCP/IP - TCP/IP hides the differences among devices connected to the Internet © 2006 Pearson Addison-Wesley. All rights reserved.

4 1.1 A Brief Intro to the Internet (continued)
- Internet Protocol (IP) Addresses - Every node has a unique numeric address - Form: 32-bit binary number - New standard, IPv6, has 128 bits (1998) - Organizations are assigned groups of IPs for their computers © 2006 Pearson Addison-Wesley. All rights reserved.

5 - Form: host-name.domain-names
- First domain is the smallest; last is the largest - Last domain specifies the type of organization - Fully qualified domain name - the host name and all of the domain names - DNS servers - convert fully qualified domain names to IPs - Problem: By the mid-1980s, several different protocols had been invented and were being used on the Internet, all with different user interfaces (Telnet, FTP, Usenet, mailto © 2006 Pearson Addison-Wesley. All rights reserved.

6 1.2 The World-Wide Web - A possible solution to the proliferation of different protocols being used on the Internet - Origins - Tim Berners-Lee at CERN proposed the Web in 1989 - Purpose: to allow scientists to have access to many databases of scientific work through their own computers - Document form: hypertext - Pages? Documents? Resources? - We’ll call them documents - Hypermedia – more than just text – images, sound, etc. - Web or Internet? - The Web uses one of the protocols, http, that runs on the Internet--there are several others (telnet, mailto, etc.) © 2006 Pearson Addison-Wesley. All rights reserved.

7 1.3 Web Browsers - Mosaic - NCSA (Univ. of Illinois), in early 1993
- First to use a GUI, led to explosion of Web use - Initially for X-Windows, under UNIX, but was ported to other platforms by late 1993 - Browsers are clients - always initiate, servers react (although sometimes servers require responses) - Most requests are for existing documents, using HyperText Transfer Protocol (HTTP) - But some requests are for program execution, with the output being returned as a document © 2006 Pearson Addison-Wesley. All rights reserved.

8 - Provide responses to browser requests, either
1.4 Web Servers - Provide responses to browser requests, either existing documents or dynamically built documents - Browser-server connection is now maintained through more than one request-response cycle © 2006 Pearson Addison-Wesley. All rights reserved.

9 1.4 Web Servers (continued)
- All communications between browsers and servers use Hypertext Transfer Protocol (HTTP) - Web servers run as background processes in the operating system - Monitor a communications port on the host, accepting HTTP messages when they appear - All current Web servers came from either 1. The original from CERN 2. The second one, from NCSA - © 2006 Pearson Addison-Wesley. All rights reserved.

10 Web servers have two main directories:
1. Document root (servable documents) 2. Server root (server system software) - Document root is accessed indirectly by clients - Its actual location is set by the server configuration file - Requests are mapped to the actual location - Virtual document trees - Virtual hosts © 2006 Pearson Addison-Wesley. All rights reserved.

11 1.4 Web Servers (continued)
- Proxy servers - Web servers now support other Internet protocols - Apache (open source, fast, reliable) - Directives (operation control): ServerName ServerRoot ServerAdmin DocumentRoot Alias Redirect DirectoryIndex UserDir - IIS - Operation is maintained through a program with a GUI interface © 2006 Pearson Addison-Wesley. All rights reserved.

12 1.5 URLs - General form: scheme:object-address
- The scheme is often a communications protocol, such as telnet or ftp - For the http protocol, the object-address is: fully qualified domain name/doc path - For the file protocol, only the doc path is needed - Host name may include a port number, as in zeppo:80 (80 is the default, so this is silly) © 2006 Pearson Addison-Wesley. All rights reserved.

13 URLs cannot include spaces or any of a collection
of other special characters (semicolons, colons, ...) - The doc path may be abbreviated as a partial path - The rest is furnished by the server configuration - If the doc path ends with a slash, it means it is a directory © 2006 Pearson Addison-Wesley. All rights reserved.

14 1.6 Multipurpose Internet Mail Extensions (MIME)
- Originally developed for - Used to specify to the browser the form of a file returned by the server (attached by the server to the beginning of the document) - Type specifications - Form: type/subtype - Examples: text/plain, text/html, image/gif, image/jpeg - Server gets type from the requested file name’s suffix (.html implies text/html) - Browser gets the type explicitly from the server © 2006 Pearson Addison-Wesley. All rights reserved.

15 - Subtype begins with x- e.g., video/x-msvideo
Experimental types - Subtype begins with x- e.g., video/x-msvideo - Experimental types require the server to send a helper application or plug-in so the browser can deal with the file © 2006 Pearson Addison-Wesley. All rights reserved.

16 1.7 The HyperText Transfer Protocol
- The protocol used by ALL Web communications - Request Phase - Form: HTTP method domain part of URL HTTP ver. Header fields blank line Message body - An example of the first line of a request: GET /cs.uccp.edu/degrees.html HTTP/1.1 © 2006 Pearson Addison-Wesley. All rights reserved.

17 - Most commonly used methods: GET - Fetch a document
POST - Execute the document, using the data in body HEAD - Fetch just the header of the document PUT - Store a new document on the server DELETE - Remove a document from the server © 2006 Pearson Addison-Wesley. All rights reserved.

18 1.7 The HyperText Transfer Protocol (continued)
- Four categories of header fields: General, request, response, & entity - Common request fields: Accept: text/plain Accept: text/* If-Modified_since: date © 2006 Pearson Addison-Wesley. All rights reserved.

19 - Common response fields: Content-length: 488 Content-type: text/html
Can communicate with HTTP without a browser > telnet blanca.uccs.edu http GET /respond.html HTTP/1.1 Host: blanca.uccs.edu © 2006 Pearson Addison-Wesley. All rights reserved.

20 1.7 The HyperText Transfer Protocol (continued)
- Response Phase - Form: Status line Response header fields blank line Response body - Status line format: HTTP version status code explanation - Example: HTTP/ OK (Current version is 1.1) © 2006 Pearson Addison-Wesley. All rights reserved.

21 - Status code is a three-digit number; first digit
specifies the general status 1 => Informational 2 => Success 3 => Redirection 4 => Client error 5 => Server error - The header field, Content-type, is required © 2006 Pearson Addison-Wesley. All rights reserved.

22 1.7 The HyperText Transfer Protocol (continued)
- An example of a complete response header: HTTP/ OK Date: Tues, 18 May :45:13 GMT Server: Apache (Red-Hat/Linux) Last-modified: Tues, 18 May :38:38 GMT Etag: "841fb-4b-3d1a0179" Accept-ranges: bytes Content-length: 364 Connection: close Content-type: text/html, charset=ISO - Both request headers and response headers must be followed by a blank line © 2006 Pearson Addison-Wesley. All rights reserved.

23 1.8 The Web Programmer’s Toolbox
- XHTML - To describe the general form and layout of documents - An XHTML document is a mix of content and controls - Controls are tags and their attributes - Tags often delimit content and specify something about how the content should be arranged in the document - Attributes provide additional information about the content of a tag © 2006 Pearson Addison-Wesley. All rights reserved.

24 - Tools for creating XHTML documents
- XHTML editors - make document creation easier - Shortcuts to typing tag names, spell-checker, - WYSIWYG XHTML editors - Need not know XHTML to create XHTML documents - Plug ins - Integrated into tools like word processors, effectively converting them to WYSIWYG XHTML editors © 2006 Pearson Addison-Wesley. All rights reserved.

25 1.8 The Web Programmer’s Toolbox (continued)
- Filters - Convert documents in other formats to XHTML - Advantages of both filters and plug-ins: - Existing documents produced with other tools can be converted to XHTML documents - Use a tool you already know to produce XHTML - Disadvantages of both filters and plug-ins: - XHTML output of both is not perfect - must be fine tuned - XHTML may be non-standard - You have two versions of the document, which are difficult to synchronize © 2006 Pearson Addison-Wesley. All rights reserved.

26 1.8 The Web Programmer’s Toolbox (continued)
- XML - A meta-markup language - Used to create a new markup language for a particular purpose or area - Because the tags are designed for a specific area, they can be meaningful - No presentation details - A simple and universal way of representing data of any textual kind © 2006 Pearson Addison-Wesley. All rights reserved.

27 - A client-side HTML-embedded scripting language
- JavaScript - A client-side HTML-embedded scripting language - Only related to Java through syntax - Dynamically typed and not object-oriented - Provides a way to access elements of HTML documents and dynamically change them - Java - General purpose object-oriented programming - Based on C++, but simpler and safer - Our focus is on applets, servlets, and JSP © 2006 Pearson Addison-Wesley. All rights reserved.

28 1.8 The Web Programmer’s Toolbox (continued)
- Perl - Provides server-side computation for HTML documents, through CGI - Perl is good for CGI programming because: - Direct access to operating systems functions - Powerful character string pattern-matching operations - Access to database systems - Perl is highly platform independent, and has been ported to all common platforms - Perl is not just for CGI © 2006 Pearson Addison-Wesley. All rights reserved.

29 1.8 The Web Programmer’s Toolbox (continued)
- PHP - A server-side scripting language - An alternative to CGI - Similar to JavaScript - Great for form processing and database access through the Web © 2006 Pearson Addison-Wesley. All rights reserved.

30 Chapter 2 Introduction to XHTML

31 2.1 Origins and Evolution of HTML
- HTML was defined with SGML - Original intent of HTML: General layout of documents that could be displayed by a wide variety of computers - Recent versions: - HTML 4.0 – 1997 - Introduced many new features and deprecated many older features - HTML A cleanup of 4.0 - XHTML - Just 4.01 defined using XML, instead of SGML - XHTML 1.1 – 2001 - Modularized 1.0, and drops frames - We’ll stick to 1.1, except for frames

32 2.1 Origins and Evolution of HTML
(continued) - Reasons to use XHTML, rather than HTML: 1. HTML has lax syntax rules, leading to sloppy and sometime ambiguous documents - XHTML syntax is much more strict, leading to clean and clear documents in a standard form 2. HTML processors do not even enforce the few syntax rule that do exist in HTML 3. The syntactic correctness of XHTML documents can be validated

33 2.2 Basic Syntax - Elements are defined by tags (markers) - Tag format: - Opening tag: <name> - Closing tag: </name> - The opening tag and its closing tag together specify a container for the content they enclose

34 2.2 Basic Syntax (continued)
- Not all tags have content - If a tag has no content, its form is <name /> - The container and its content together are called an element - If a tag has attributes, they appear between its name and the right bracket of the opening tag - Comment form: <!-- … --> - Browsers ignore comments, unrecognizable tags, line breaks, multiple spaces, and tabs - Tags are suggestions to the browser, even if they are recognized by the browser

35 2.3 Standard HTML Document Structure
- Every XHTML document must begin with: <?xml version = ″1.0″?> <!DOCTYPE html PUBLIC ″-//w3c//DTD XHTML 1.1//EN″ - <html>, <head>, <title>, and <body> are required in every document - The whole document must have <html> as its root - html must have the xmlns attribute: <html xmlns = ″ - A document consists of a head and a body - The <title> tag is used to give the document a title, which is normally displayed in the browser’s window title bar (at the top of the display) - Prior to XHTML 1.1, a document could have either a body or a frameset

36 2.4 Basic Text Markup - Text is normally placed in paragraph elements
- The <p> tag breaks the current line and inserts a blank line - the new line gets the beginning of the content of the paragraph - The browser puts as many words of the paragraph’s content as will fit in each line

37 <?xml version = ″1.0″?>
<!DOCTYPE html PUBLIC ″-//w3c//DTD XHTML 1.1//EN″ <!-- greet.hmtl A trivial document --> <html xmlns = ″ <head> <title> Our first document </title> </head> <body> <p> Greetings from your Webmaster! </p> </body> </html>

38 2.4 Basic Text Markup (continued)
- W3C HTML Validation Service - Line breaks - The effect of the <br /> tag is the same as that of <p>, except for the blank line - No closing tag!

39 - Example of paragraphs and line breaks
On the plains of hesitation <p> bleach the bones of countless millions </p> <br /> who, at the dawn of victory <br /> sat down to wait, and waiting, died. - Typical display of this text: On the plains of hesitation bleach the bones of countless millions who, at the dawn of victory sat down to wait, and waiting, died.

40 2.4 Basic Text Markup (continued)
- Headings - Six sizes, 1 - 6, specified with <h1> to <h6> - 1, 2, and 3 use font sizes that are larger than the default font size - 4 uses the default size - 5 and 6 use smaller font sizes

41 <!-- headings.html An example to illustrate headings --> <html xmlns = ″ <head> <title> Headings </title> </head> <body> <h1> Aidan’s Airplanes (h1) </h1> <h2> The best in used airplanes (h2) </h2> <h3> "We’ve got them by the hangarful" (h3) </h3> <h4> We’re the guys to see for a good used airplane (h4) </h4> <h5> We offer great prices on great planes (h5) </h5> <h6> No returns, no guarantees, no refunds, all sales are final (h6) </h6> </body> </html>

42 - Blockquotes - Content of <blockquote> - To set a block of text off from the normal flow and appearance of text - Browsers often indent, and sometimes italicize

43 lies completely in the street
- Font Styles and Sizes (can be nested) - Boldface - <b> - Italics - <i> - Larger - <big> - Smaller - <small> - Monospace - <tt> The <big> sleet <big> in <big> <i> Crete </i><br /> lies </big> completely </big> in </big> the street The sleet in Crete lies completely in the street

44 - These tags are not affected if they appear in the
content of a <blockquote>, unless there is a conflict (e.g., italics) - Superscripts and subscripts - Subscripts with <sub> - Superscripts with <sup> Example: x<sub>2</sub><sup>3</sup> Display: x23 - Inline versus block elements - Block elements CANNOT be nested in inline elements

45 2.4 Basic Text Markup (continued)
- All of this font size and font style stuff can be done with style sheets, but these tags are not yet deprecated - Character Entities Char. Entity Meaning & & Ampersand < < Less than > > Greater than " " Double quote ' &apos; Single quote ¼ ¼ One quarter ½ ½ One half ¾ ¾ Three quarters  ° Degree (space)   Non-breaking space

46 - Horizontal rules - <hr /> draws a line across the display, after a line break - The meta element (for search engines) Used to provide additional information about a document, with attributes, not content

47 2.5 Images - GIF (Graphic Interchange Format)
- 8-bit color (256 different colors) - JPEG (Joint Photographic Experts Group) - 24-bit color (16 million different colors) - Both use compression, but JPEG compression is better - Images are inserted into a document with the <img /> tag with the src attribute - The alt attribute is required by XHTML - Purposes: 1. Non-graphical browsers 2. Browsers with images turned off <img src = "comets.jpg" alt = "Picture of comets" /> - The <img> tag has 30 different attributes, including width and height (in pixels)

48 - Portable Network Graphics (PNG) - Relatively new
- Should eventually replace both gif and jpeg <!-- image.html An example to illustrate an image --> <html xmlns = ″ <head> <title> Images </title> </head> <body> <h1> Aidan's Airplanes </h1> <h2> The best in used airplanes </h2> <h3> "We've got them by the hangarful" </h3> <h2> Special of the month </h2> <p> 1960 Cessna 210 <br /> 577 hours since major engine overhaul <br /> 1022 hours since prop overhaul <br /><br /> <img src = "c210new.jpg" alt = "Picture of a Cessna 210"/> <br /> Buy this fine airplane today at a remarkably low price <br /> Call today! </p> </body> </html>

49 2.5 Images (continued)

50 2.6 Hypertext Links - Hypertext is the essence of the Web!
- A link is specified with the href (hypertext reference) attribute of <a> (the anchor tag) - The content of <a> is the visual link in the document - If the target is a whole document (not the one in which the link appears), the target need not be specified in the target document as being the target - Note: Relative addressing of targets is easier to maintain and more portable than absolute addressing

51 2.6 Hypertext Links (continued)
<!-- link.html An example to illustrate a link --> <html xmlns = ″ <head> <title> Links </title> </head> <body> <h1> Aidan's Airplanes </h1> <h2> The best in used airplanes </h2> <h3> "We've got them by the hangarful" </h3> <h2> Special of the month </h2> <p> 1960 Cessna 210 <br /> <a href = "C210data.html"> Information on the Cessna 210 </a> </p> </body> </html>

52 2.6 Hypertext Links (continued)

53 If the target is not at the beginning of the document, the target spot
must be marked - Target labels can be defined in many different tags with the id attribute, as in <h1 id = "baskets"> Baskets </h1> - The link to an id must be preceded by a pound sign (#); If the id is in the same document, this target could be <a href = "#baskets"> What about baskets? </a> - If the target is in a different document, the document reference must be included <a href = "myAd.html#baskets”> Baskets </a> Style note: a link should blend in with the surrounding text, so reading it without taking the link should not be made less pleasant - Links can have images: <a href = "c210data.html“> <img src = "smallplane.jpg" alt = "Small picture of an airplane " /> Info on C210 </a>

54 2.7 Lists - Unordered lists
- The list is the content of the <ul> tag - List elements are the content of the <li> tag <h3> Some Common Single-Engine Aircraft </h3> <ul> <li> Cessna Skyhawk </li> <li> Beechcraft Bonanza </li> <li> Piper Cherokee </li> </ul> - Ordered lists - The list is the content of the <ol> tag - Each item in the display is preceded by a sequence value

55 2.7 Lists (continued) - Nested lists
<h3> Cessna 210 Engine Starting Instructions </h3> <ol> <li> Set mixture to rich </li> <li> Set propeller to high RPM </li> <li> Set ignition switch to "BOTH" </li> <li> Set auxiliary fuel pump switch to "LOW PRIME" </li> <li> When fuel pressure reaches 2 to 2.5 PSI, push starter button </li> </ol> - Nested lists - Any type list can be nested inside any type list - The nested list must be in a list item

56 2.7 Lists (continued) - Definition lists (for glossaries, etc.)
- List is the content of the <dl> tag - Terms being defined are the content of the <dt> tag - The definitions themselves are the content of the <dd> tag <h3> Single-Engine Cessna Airplanes </h3> <dl > <dt> 152 </dt> <dd> Two-place trainer </dd> <dt> 172 </dt> <dd> Smaller four-place airplane </dd> <dt> 182 </dt> <dd> Larger four-place airplane </dd> <dt> 210 </dt> <dd> Six-place airplane - high performance </dd> </dl>

57 2.8 Tables - A table is a matrix of cells, each possibly having content - The cells can include almost any element - Some cells have row or column labels and some have data - A table is specified as the content of a <table> tag - A border attribute in the <table> tag specifies a border between the cells - If border is set to "border", the browser’s default width border is used - The border attribute can be set to a number, which will be the border width - Without the border attribute, the table will have no lines! - Tables are given titles with the <caption> tag, which can immediately follow <table>

58 - Each row of a table is specified as the content of a <tr> tag
- The row headings are specified as the content of a <th> tag - The contents of a data cell is specified as the content of a <td> tag <table border = "border"> <caption> Fruit Juice Drinks </caption> <tr> <th> </th> <th> Apple </th> <th> Orange </th> <th> Screwdriver </th> </tr> <th> Breakfast </th> <td> 0 </td> <td> 1 </td> <th> Lunch </th> </table>

59 2.8 Tables (continued) - A table can have two levels of column labels
- If so, the colspan attribute must be set in the <th> tag to specify that the label must span some number of columns <tr> <th colspan = "3"> Fruit Juice Drinks </th> </tr> <th> Orange </th> <th> Apple </th> <th> Screwdriver </th>

60 2.8 Tables (continued) - If the rows have labels and there is a spanning column label, the upper left corner must be made larger, using rowspan <table border = "border"> <tr> <td rowspan = "2"> </td> <th colspan = "3"> Fruit Juice Drinks </th> </tr> <th> Apple </th> <th> Orange </th> <th> Screwdriver </th> </table>

61 - The align attribute controls the horizontal
placement of the contents in a table cell - Values are left, right, and center (default) - align is an attribute of <tr>, <th>, and <td> elements - The valign attribute controls the vertical placement of the contents of a table cell - Values are top, bottom, and center (default) - valign is an attribute of <th> and <td> elements  SHOW cell_align.html and display it - The cellspacing attribute of <table> is used to specify the distance between cells in a table - The cellpadding attribute of <table> is used to specify the spacing between the content of a cell and the inner walls of the cell

62 - Header, body, and footer, which are the elements:
<table cellspacing = "50"> <tr> <td> Colorado is a state of … </td> <td> South Dakota is somewhat… </tr> </table> - Table Sections - Header, body, and footer, which are the elements: thead, tbody, and tfoot - If a document has multiple tbody elements, they are separated by thicker horizontal lines

63 2.9 Forms - A form is the usual way information is gotten from
a browser to a server - HTML has tags to create a collection of objects that implement this information gathering - The objects are called widgets (e.g., radio buttons and checkboxes) - When the Submit button of a form is clicked, the form’s values are sent to the server - All of the widgets, or components of a form are defined in the content of a <form> tag

64 - The only required attribute of <form> is action,
which specifies the URL of the application that is to be called when the Submit button is clicked action = " - If the form has no action, the value of action is the empty string - The method attribute of <form> specifies one of the two possible techniques of transferring the form data to the server, get and post - get and post are discussed in Chapter 10

65 - Widgets - Many are created with the <input> tag - The type attribute of <input> specifies the kind of widget being created 1. Text - Creates a horizontal box for text input - Default size is 20; it can be changed with the size attribute - If more characters are entered than will fit, the box is scrolled (shifted) left

66 - If you don’t want to allow the user to type more
characters than will fit, set maxlength, which causes excess input to be ignored <input type = "text" name = "Phone" size = "12" > 2. Checkboxes - to collect multiple choice input - Every checkbox requires a value attribute, which is the widget’s value in the form data when the checkbox is ‘checked’ - A checkbox that is not ‘checked’ contributes no value to the form data - By default, no checkbox is initially ‘checked’ - To initialize a checkbox to ‘checked’, the checked attribute must be set to "checked"

67 2.9 Forms (continued) - Widgets (continued) Grocery Checklist
<form action = ""> <p> <input type = "checkbox" name ="groceries" value = "milk" checked = "checked"> Milk value = "bread"> Bread <input type = "checkbox" name = "groceries" value= "eggs"> Eggs </p> </form>

68 3. Radio Buttons - collections of checkboxes in
which only one button can be ‘checked’ at a time - Every button in a radio button group MUST have the same name - If no button in a radio button group is ‘pressed’, the browser often ‘presses’ the first one Age Category <form action = ""> <p> <input type = "radio" name = "age" value = "under20" checked = "checked"> 0-19 value = "20-35"> 20-35 value = "36-50"> 36-50 value = "over50"> Over 50 </p> </form>

69 4. Menus - created with <select> tags
- There are two kinds of menus, those that behave like checkboxes and those that behave like radio buttons (the default) - Menus that behave like checkboxes are specified by including the multiple attribute, which must be set to "multiple" - The name attribute of <select> is required - The size attribute of <select> can be included to specify the number of menu items to be displayed (the default is 1) - If size is set to > 1 or if multiple is specified, the menu is displayed as a pop-up menu

70 - Each item of a menu is specified with an <option> tag, whose pure text content (no tags) is the value of the item - An <option> tag can include the selected attribute, which when assigned "selected" specifies that the item is preselected Grocery Menu - milk, bread, eggs, cheese <form action = ""> <p> With size = 1 (the default) <select name = "groceries"> <option> milk </option> <option> bread </option> <option> eggs </option> <option> cheese </option> </select> </p> </form>

71 2.9 Forms (continued) - Widgets (continued) - After clicking the menu:
- After changing size to 2:

72 5. Text areas - created with <textarea>
- Usually include the rows and cols attributes to specify the size of the text area - Default text can be included as the content of <textarea> - Scrolling is implicit if the area is overfilled Please provide your employment aspirations <form action = ""> <p> <textarea name = "aspirations" rows = "3” cols = "40"> (Be brief and concise) </textarea> </p> </form>

73 6. Reset and Submit buttons - Both are created with <input>
<input type = "reset" value = "Reset Form"> <input type = "submit” value = "Submit Form"> - Submit has two actions: 1. Encode the data of the form 2. Request that the server execute the server-resident program specified as the value of the action attribute of <form> - A Submit button is required in every form --> SHOW popcorn.html and display it

74 2.10 Frames - Frames are rectangular sections of the display
window, each of which can display a different document - Because frames are no longer part of XHTML, you cannot validate a document that includes frames - The <frameset> tag specifies the number of frames and their layout in the window - <frameset> takes the place of <body> - Cannot have both! - <frameset> must have either a rows attribute or a cols attribute, or both (usually the case) - Default is 1

75 - The possible values for rows and cols are
numbers, percentages, and asterisks - A number value specifies the row height in pixels - Not terribly useful! - A percentage specifies the percentage of total window height for the row - Very useful! - An asterisk after some other specification gives the remainder of the height of the window - Examples: <frameset rows = "150, 200, 300"> <frameset rows = "25%, 50%, 25%"> <frameset rows = "50%, 20%, *" > <frameset rows = "50%, 25%, 25%" cols = "40%, *">

76 - The <frame> tag specifies the content of a frame
- The first <frame> tag in a <frameset> specifies the content of the first frame, etc. - Row-major order is used - Frame content is specified with the src attribute - Without a src attribute, the frame will be empty (such a frame CANNOT be filled later) - If <frameset> has fewer <frame> tags than frames, the extra frames are empty

77 - Scrollbars are implicitly included if needed (they
are needed if the specified document will not fit) - If a name attribute is included, the content of the frame can be changed later (by selection of a link in some other frame) SHOW frames.html - Note: the Frameset standard must be specified in the DOCTYPE declaration

78 <!-- contents.html The contents of the first frame of frames.html, which is the table of contents for the second frame --> <html xmlns = ″ <head> <title> Table of Contents Frame </title> </head> <body> <h4> Fruits </h4> <ul> <li> <a href = "apples.html" target = "descriptions"> apples </a> <li> <a href = "bananas.html" bananas </a> <li> <a href = "oranges.html" oranges </a> </ul> </body> </html>

79 2.11 Syntactic Differences between HTML & XHTML
- Nested frames - to divide the screen in more interesting ways  SHOW nested_frames.html 2.11 Syntactic Differences between HTML & XHTML - Case sensitivity - Closing tags - Quoted attribute values - Explicit attribute values - id and name attributes - Element nesting

80 Cascading Style Sheets
Chapter 3 Cascading Style Sheets

81 3.1 Introduction The CSS1 specification was developed in 1996
CSS2 was released in 1998 CSS3 is on its way CSSs provide the means to control and change presentation of HTML documents CSS is not technically HTML, but can be embedded in HTML documents Style sheets allow you to impose a standard style on a whole document, or even a whole collection of documents Style is specified for a tag by the values of its properties

82 3.2 Levels of Style Sheets There are three levels of style sheets
Inline - specified for a specific occurrence of a tag and apply only to that tag This is fine-grain style, which defeats the purpose of style sheets - uniform style Document-level style sheets - apply to the whole document in which they appear External style sheets - can be applied to any number of documents When more than one style sheet applies to a specific tag in a document, the lowest level style sheet has precedence In a sense, the browser searches for a style property spec, starting with inline, until it finds one (or there isn’t one)

83 3.2 Levels of Style Sheets (continued)
Inline style sheets appear in the tag itself Document-level style sheets appear in the head of the document External style sheets are in separate files, potentially on any server on the Internet Written as text files with the MIME type text/css A <link> tag is used to specify that the browser is to fetch and use an external style sheet file <link rel = "stylesheet" type = "text/css" href = " </link> External style sheets can be validated

84 3.3 Style Specification Formats
Format depends on the level of the style sheet Inline: Style sheet appears as the value of the style attribute General form: style = "property_1: value_1; property_2: value_2; property_n: value_n;“ Final ; s optional but preferred

85 - Document-level: - Style sheet appears as a list of rules that are the content of a <style> tag - The <style> tag must include the type attribute, set to "text/css" - The list of rules must be placed in an HTML comment, because it is not HTML - Comments in the rule list must have a different form - use C comments (/*…*/)

86 3.4 Selector Forms 1. Simple Selector Forms
- The selector is a tag name or a list of tag names, separated by commas - Examples: h1, h3 p - Contextual selectors ol ol li

87 2. Class Selectors - Used to allow different occurrences of the same tag to use different style specifications - A style class has a name, which is attached to a tag name - For example, p.narrow {property/value list} p.wide {property/value list}

88 - The class you want on a particular occurrence of
a tag is specified with the class attribute of the tag - For example, <p class = "narrow"> ... </p> <p class = "wide">

89 3. Generic Selectors - A generic class can be defined if you want a style to apply to more than one kind of tag - A generic class must be named, and the name must begin with a period - Example, .really-big { … } - Use it as if it were a normal style class <h1 class = "really-big"> … </h1> ... <p class = "really-big"> … </p>

90 4. id Selectors - An id selector allow the application of a style to one specific element - General form: #specific-id {property-value list} - Example: #section14 {font-size: 20}

91 - Names begin with colons
5. Pseudo Classes - Pseudo classes are styles that apply when something happens, rather than because the target element simply exists - Names begin with colons - hover classes apply when the mouse cursor is over the element - focus classes apply when an element has focus <!-- pseudo.html --> <html xmlns = " <head> <title> Checkboxes </title> <style type = "text/css"> input:hover {color: red;} input:focus {color: green;} </style> </head> <body> <form action = ""> <p> Your name: <input type = "text" /> </p> </form> </body> </html>

92 3.5 Property Value Forms - There are 60 different properties in 7 categories: - Fonts - Lists - Alignment of text - Margins - Colors - Backgrounds - Borders - Property Value Forms - Keywords - left, small, … - Not case sensitive

93 - Length - numbers, maybe with decimal points
- Units: px - pixels in - inches cm - centimeters mm - millimeters pt - points pc - picas (12 points) em - height of the letter ‘m’ ex-height - height of the letter ‘x’ - No space is allowed between the number and the unit specification e.g., 1.5 in is illegal!

94 - Percentage - just a number followed
immediately by a percent sign - URL values - url(protocol://server/pathname) - Colors - Color name - rgb(n1, n2, n3) - Numbers can be decimal or percentages - Hex form: #XXXXXX - Property values are inherited by all nested tags, unless overriden

95 3.6 Font Properties - font-family - Value is a list of font names - browser uses the first in the list it has - font-family: Arial, Helvetica, Courier - Generic fonts: serif, sans-serif, cursive, fantasy, and monospace (defined in CSS) - Browser has a specific font for each - If a font name has more than one word, it should be single-quoted

96 - font-size - Possible values: a length numbr or a name, such as smaller, xx-large, etc. - font-style - italic, oblique (useless), normal - font-weight - degrees of boldness - bolder, lighter, bold, normal - Could specify as a multiple of 100 (100 – 900) - font - For specifying a list of font properties font: bolder 14pt Arial Helvetica - Order must be: style, weight, size, name(s)

97 Examples -> SHOW fonts.html and display -> SHOW fonts2.html and display - The text-decoration property - line-through, overline, underline, none - letter-spacing – value is any length property value 3.7 List properties - list-style-type - Unordered lists - Bullet can be a disc (default), a square, or a circle - Set it on either the <ul> or <li> tag - On <ul>, it applies to list items

98

99 <h3> Some Common Single-Engine Aircraft </h3>
<ul style = "list-style-type: square"> <li> Cessna Skyhawk </li> <li> Beechcraft Bonanza </li> <li> Piper Cherokee </li> </ul> - On <li>, list-style-type applies to just that item <ul> <li style = "list-style-type: disc"> Cessna Skyhawk </li> <li style = "list-style-type: square"> Beechcraft Bonanza </li> <li style = "list-style-type: circle"> Piper Cherokee </li>

100 <style type = "text/css"> rule list -->
General form: <style type = "text/css"> <!-- rule list --> </style> Form of the rules: selector {list of property/values} Each property/value pair has the form: property: value Pairs are separated by semicolons, just as in the value of a <style> tag External style sheets Form is a list of style rules, as in the content of a <style> tag for document-level style sheets

101 - Could use an image for the bullets in an unordered list
- Example: <li style = "list-style-image: url(bird.jpg)"> - On ordered lists - list-style-type can be used to change the sequence values Property value Sequence type First four decimal Arabic numerals , 2, 3, 4 upper-alpha Uc letters A, B, C, D lower-alpha Lc letters a, b, c, d upper-roman Uc Roman I, II, III, IV lower-roman Lc Roman i, ii, iii, iv  SHOW sequence_types.html and display - CSS2 has more, like lower-greek and hebrew

102 3.8 Colors - Color is a problem for the Web for two reasons:
1. Monitors vary widely 2. Browsers vary widely - There are three color collections 1. There is a set of 16 colors that are guaranteed to be displayable by all graphical browsers on all color monitors black green silver C0C0C lime FF00 gray olive white FFFFFF yellow FFFF00 maroon navy red FF blue FF purple teal fuchia FF00FF aqua FFFF

103 2. There is a much larger set, the Web Palette
- 216 colors - Use hex color values of 00, 33, 66, 99, CC, and FF - Inside back cover of this book has them!

104 3. Any one of 16 million different colors
___________________________________________ - The color property specifies the foreground color of elements <style type = “text/css”> th.red {color: red} th.orange {color: orange} </style> <table border = "5"> <tr> <th class = "red"> Apple </th> <th class = "orange"> Orange </th> <th class = "orange"> Screwdriver </th> </tr> </table> - The background-color property specifies the background color of elements SHOW back_color.html and display

105 3.9 Alignment of Text - The text-indent property allows indentation
- Takes either a length or a % value - The text-align property has the possible values, left (the default), center, right, or justify - Sometimes we want text to flow around another element - the float property - The float property has the possible values, left, right, and none (the default) - If we have an element we want on the right, with text flowing on its left, we use the default text-align value (left) for the text and the right value for float on the element we want on the right

106 3.9 Alignment of Text (continued)
<img src = "c210.jpg" style = "float: right" /> -- Some text with the default alignment - left

107 3.10 The Box Model - Borders – every element has a border-style property - Controls whether the element has a border and if so, the style of the border - border-style values: none, dotted, dashed, and double - border-width – thin, medium (default), thick, or a length value in pixels - Border width can be specified for any of the four borders (e.g., border-top-width) - border-color – any color - Border color can be specified for any of the four borders (e.g., border-top-color)  SHOW borders.html and display

108 - Margin – the space between the border of an
element and its neighbor element - The margins around an element can be set with margin-left, etc. - just assign them a length value <img src = "c210.jpg " style = "float: right; margin-left: 0.35in; margin-bottom: 0.35in" />

109 - Padding – the distance between the content of an element and its
border - Controlled by padding, padding-left, etc.  SHOW marpads.html and display 3.11 Background Images - The background-image property  SHOW back_image.html and display - Repetition can be controlled - background-repeat property - Possible values: repeat (default), no-repeat, repeat-x, or repeat-y - background-position property - Possible values: top, center, bottom, left, or right

110 3.12 The <span> and <div> tags
One problem with the font properties is that they apply to whole elements, which are often too large - Solution: a new tag to define an element in the content of a larger element - <span> - The default meaning of <span> is to leave the content as it is <p> Now is the <span> best time </span> ever! </p> - Use <span> to apply a document style sheet to its content <style type = "text/css">? bigred {font-size: 24pt; font-family: Ariel; color: red} </style> Now is the <span class = "bigred"> best time </span> ever!

111 3.12 The <span> and <div> tags (continued)
- The <span> tag is similar to other HTML tags, they can be nested and they have id and class attributes - Another tag that is useful for style specifications: <div> - Used to create document sections (or divisions) for which style can be specified - e.g., A section of five paragraphs for which you want some particular style

112 Extra: Positioning Items can be placed in specified positions on the web page absolute position position: absolute; position relative to the current insertion point is default Placement Measure can be any length unit Can specify top or bottom, left or right position top:50px; left:100px; Example Relative placement Can use distance or can “float” from left or right float: left;

113 The Basics of JavaScript
Chapter 4 The Basics of JavaScript

114 4.1 Overview of JavaScript
- Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape and Sun in 1995, renamed JavaScript - Now standardized by the European Computer Manufacturers Association as ECMA-262 (also ISO 16262) - We’ll call collections of JavaScript code scripts, not programs - JavaScript and Java are only related through syntax - JavaScript is dynamically typed - JavaScript’s support for objects is very different © 2006 Pearson Addison-Wesley. All rights reserved.

115 4.1 Overview of JavaScript (continued)
- JavaScript can be used to replace some of what is typically done with applets (except graphics) is done with CGI (but no file operations or networking) - User interactions through forms are easy - The Document Object Model makes it possible to support dynamic HTML documents with JavaScript - Event-Driven Computation (See Chapter 5) - User interactions with HTML documents in JavaScript use the event-driven model of computation - User interactions with form elements can be used to trigger execution of scripts © 2006 Pearson Addison-Wesley. All rights reserved.

116 4.2 Object Orientation and JavaScript
- JavaScript is NOT an object-oriented programming language - Does not support class-based inheritance - Cannot support polymorphism - Has prototype-based inheritance, which is much different - JavaScript Objects: - JavaScript objects are collections of properties, which are like the members of classes in Java and C++ - JavaScript has primitives for simple types - The root object in JavaScript is Object – all objects are derived from Object - All JavaScript objects are accessed through references © 2006 Pearson Addison-Wesley. All rights reserved.

117 4.3 General Syntactic Characteristics
- For this book, all JavaScript scripts will be embedded in HTML documents - Either directly, as in <script type = “text/javaScript"> -- JavaScript script – </script> - Or indirectly, as a file specified in the src attribute of <script>, as in <script type = "text/javaScript" src = "myScript.js"> - Identifier form: begin with a letter or underscore, followed by any number of letters, underscores, and digits - Case sensitive - 25 reserved words, plus future reserved words - Comments: both // and /* … */ © 2006 Pearson Addison-Wesley. All rights reserved.

118 4.3 General Syntactic Characteristics (continued)
- Scripts are usually hidden from browsers that do not include JavaScript interpreters by putting them in special comments <!-- -- JavaScript script – //--> - Semicolons can be a problem - They are “somewhat” optional - Problem: When the end of the line can be the end of a statement – JavaScript puts a semicolon there 4.4 Primitives, Operations, & Expressions - All primitive values have one of the five primitive types: Number, String, Boolean, Undefined, or Null © 2006 Pearson Addison-Wesley. All rights reserved.

119 4.4 Primitives, Operations, & Expressions (continued)
- Number, String, and Boolean have wrapper objects (Number, String, and Boolean) - In the cases of Number and String, primitive values and objects are coerced back and forth so that primitive values can be treated essentially as if they were objects - Numeric literals – just like Java - All numeric values are stored in double-precision floating point - String literals are delimited by either ' or " - Can include escape sequences (e.g., \t) - All String literals are primitive values © 2006 Pearson Addison-Wesley. All rights reserved.

120 4.4 Primitives, Operations, & Expressions (continued)
- Boolean values are true and false - The only Null value is null - The only Undefined value is undefined - JavaScript is dynamically typed – any variable can be used for anything (primitive value or reference to any object) - The interpreter determines the type of a particular occurrence of a variable - Variables can be either implicitly or explicitly declared var sum = 0, today = "Monday", flag = false; © 2006 Pearson Addison-Wesley. All rights reserved.

121 4.4 Primitives, Operations, & Expressions (continued)
- Numeric operators - ++, --, +, -, *, /, % - All operations are in double precision - Same precedence and associativity as Java - The Math Object provides floor, round, max, min, trig functions, etc. e.g., Math.cos(x) - The Number Object - Some useful properties: MAX_VALUE, MIN_VALUE, NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY, PI - e.g., Number.MAX_VALUE - An arithmetic operation that creates overflow returns NaN - NaN is not == to any number, not even itself - Test for it with isNaN(x) - Number object has the method, toString © 2006 Pearson Addison-Wesley. All rights reserved.

122 4.4 Primitives, Operations, & Expressions (continued)
- String catenation operator - + - Coercions - Catenation coerces numbers to strings - Numeric operators (other than +) coerce strings to numbers (if either operand of + is a string, it is assumed to be catenation) - Conversions from strings to numbers that do not work return NaN - Explicit conversions 1. Use the String and Number constructors 2. Use toString method of numbers 3. Use parseInt and parseFloat on strings - String properties & methods: - length e.g., var len = str1.length; (a property) - charAt(position) e.g., str.charAt(3) - indexOf(string) e.g., str.indexOf('B') - substring(from, to) e.g., str.substring(1, 3) - toLowerCase() e.g., str.toLowerCase() © 2006 Pearson Addison-Wesley. All rights reserved.

123 4.4 Primitives, Operations, & Expressions (continued)
- The typeof operator - Returns "number", "string", or "boolean" for primitives; returns "object" for objects and null - Assignment statements – just like C++ and Java - The Date Object - Create one with the Date constructor (no params) - Local time methods of Date: toLocaleString – returns a string of the date getDate – returns the day of the month getMonth – returns the month of the year (0 – 11) getDay – returns the day of the week (0 – 6) getFullYear – returns the year getTime – returns the number of milliseconds since January 1, 1970 getHours – returns the hour (0 – 23) getMinutes – returns the minutes (0 – 59) getMilliseconds – returns the millisecond (0 – 999) © 2006 Pearson Addison-Wesley. All rights reserved.

124 4.5 Screen Output & Keyboard Input
- The JavaScript model for the HTML document is the Document object - The model for the browser display window is the Window object - The Window object has two properties, document and window, which refer to the Document and Window objects, respectively - The Document object has a method, write, which dynamically creates content - The parameter is a string, often catenated from parts, some of which are variables e.g., document.write("Answer: " + result + "<br />"); - The parameter is sent to the browser, so it can be anything that can appear in an HTML document (<br />, but not \n) - The Window object has three methods for creating dialog boxes, alert, confirm, and prompt © 2006 Pearson Addison-Wesley. All rights reserved.

125 4.5 Screen Output (continued)
1. alert("Hej! \n"); - Parameter is plain text, not HTML - Opens a dialog box which displays the parameter string and an OK button - It waits for the user to press the OK button 2. confirm("Do you want to continue?"); - Opens a dialog box and displays the parameter and two buttons, OK and Cancel - Returns a Boolean value, depending on which button was pressed (it waits for one) 3. prompt("What is your name?", ""); - Opens a dialog box and displays its string parameter, along with a text box and two buttons, OK and Cancel - The second parameter is for a default response if the user presses OK without typing a response in the text box (waits for OK)  SHOW roots.html © 2006 Pearson Addison-Wesley. All rights reserved.

126 4.6 Control Statements - Similar to C, Java, and C++
- Compound statements are delimited by braces, but compound statements are not blocks - Control expressions – three kinds 1. Primitive values - If it is a string, it is true unless it is empty or "0" - If it is a number, it is true unless it is zero 2. Relational Expressions - The usual six: ==, !=, <, >, <=, >= - Operands are coerced if necessary - If one is a string and one is a number, it attempts to convert the string to a number - If one is Boolean and the other is not, the Boolean operand is coerced to a number (1 or 0) - The unusual two: === and !== - Same as == and !=, except that no coercions are done (operands must be identical) © 2006 Pearson Addison-Wesley. All rights reserved.

127 4.6 Control Statements (continued)
2. Relational Expressions (continued) - Comparisons of references to objects are not useful (addresses are compared, not values) 3. Compound Expressions - The usual operators: &&, ||, and ! - The Boolean object has a method, toString, to allow them to be printed (true or false) - Selection Statements - The usual if-then-else (clauses can be either single statements or compound statements) © 2006 Pearson Addison-Wesley. All rights reserved.

128 4.6 Control Statements (continued)
- Switch switch (expression) { case value_1: // value_1 statements case value_2: // value_2 statements [default: // default statements] } - The statements can be either statement sequences or compound statements - The control expression can be a number, a string, or a Boolean - Different cases can have values of different types  SHOW borders2.html © 2006 Pearson Addison-Wesley. All rights reserved.

129 4.6 Control Statements (continued)
- Loop statements while (control_expression) statement or cmpnd for (init; control; increment) statement or cmpnd - init can have declarations, but the scope of such variables is the whole script  SHOW date.html do statement or compound while (control_expression) 4.7 Object Creation and Modification - Objects can be created with new - The most basic object is one that uses the Object constructor, as in var myObject = new Object(); - The new object has no properties - a blank object - Properties can be added to an object, any time © 2006 Pearson Addison-Wesley. All rights reserved.

130 4.7 Object Creation and Modification (continued)
var myAirplane = new Object(); myAirplane.make = "Cessna"; myAirplane.model = "Centurian"; - Objects can be nested, so a property could be itself another object, created with new - Properties can be accessed by dot notation or in array notation, as in var property1 = myAirplane["model"]; delete myAirplane.model; - Another Loop Statement - for (identifier in object) statement or compound for (var prop in myAirplane) document.write(myAirplane[prop] + "<br />"); © 2006 Pearson Addison-Wesley. All rights reserved.

131 4.8 Arrays - Objects with some special functionality
- Array elements can be primitive values or references to other objects - Length is dynamic - the length property stores the length - Array objects can be created in two ways, with new, or by assigning an array literal var myList = new Array(24, "bread", true); var myList2 = [24, "bread", true]; var myList3 = new Array(24); - The length of an array is the highest subscript to which an element has been assigned, plus 1 myList[122] = "bitsy"; // length is 123 - Because the length property is writeable, you can set it to make the array any length you like, as in myList.length = 150;  SHOW insert_names.html © 2006 Pearson Addison-Wesley. All rights reserved.

132 4.8 Arrays (continued) - Array methods:
- join – e.g., var listStr = list.join(", "); - reverse - sort – e.g., names.sort(); - Coerces elements to strings and puts them in alphabetical order - concat – e.g., newList = list.concat(47, 26); - slice listPart = list.slice(2, 5); listPart2 = list.slice(2); - toString - Coerce elements to strings, if necessary, and catenate them together, separated by commas (exactly like join(", ")) - push, pop, unshift, and shift  SHOW nested_arrays.html © 2006 Pearson Addison-Wesley. All rights reserved.

133 4.9 Functions - function function_name([formal_parameters]) {
-- body – } - Return value is the parameter of return - If there is no return, or if the end of the function is reached, undefined is returned - If return has no parameter, undefined is returned - Functions are objects, so variables that reference them can be treated as other object references - If fun is the name of a function, ref_fun = fun; ... ref_fun(); /* A call to fun */ - We place all function definitions in the head of the the HTML document - All variables that are either implicitly declared or explicitly declared outside functions are global - Variables explicitly declared in a function are local © 2006 Pearson Addison-Wesley. All rights reserved.

134 4.9 Functions (continued)
- Parameters are passed by value, but when a reference variable is passed, the semantics are pass-by-reference - There is no type checking of parameters, nor is the number of parameters checked (excess actual parameters are ignored, excess formal parameters are set to undefined) - All parameters are sent through a property array, arguments, which has the length property  SHOW parameters.html and output - There is no clean way to send a primitive by reference - One dirty way is to put the value in an array and send the array’s name function by10(a) { a[0] *= 10; } ... var listx = new Array(1); listx[0] = x; by10(listx); x = listx[0]; © 2006 Pearson Addison-Wesley. All rights reserved.

135 4.9 Functions (continued)
- To sort something other than strings into alphabetical order, write a function that performs the comparison and send it to the sort method - The comparison function must return a negative number, zero, or a positive number to indicate whether the order is ok, equal, or not function num_order(a, b) {return a - b;} - Now, we can sort an array named num_list with: num_list.sort(num_order); 4.10 An Example -> SHOW medians.html & output © 2006 Pearson Addison-Wesley. All rights reserved.

136 4.11 Constructors - Used to initialize objects, but actually create the properties function plane(newMake, newModel, newYear){ this.make = newMake; this.model = newModel; this.year = newYear; } myPlane = new plane("Cessna", "Centurnian", "1970"); - Can also have method properties function displayPlane() { document.write("Make: ", this.make, "<br />"); document.write("Model: ", this.model, document.write("Year: ", this.year, - Now add the following to the constructor: this.display = displayPlane; © 2006 Pearson Addison-Wesley. All rights reserved.

137 - JavaScript provides two ways to do pattern
4.12 Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects 2. Using methods on String objects - Simple patterns - Two categories of characters in patterns: a. normal characters (match themselves) b. metacharacters (can have special meanings in patterns--do not match themselves) \ | ( ) [ ] { } ^ $ * + ? . - A metacharacter is treated as a normal character if it is backslashed - period is a special metacharacter - it matches any character except newline © 2006 Pearson Addison-Wesley. All rights reserved.

138 4.12 Pattern Matching (continued)
search(pattern) - Returns the position in the object string of the pattern (position is relative to zero); returns -1 if it fails var str = "Gluckenheimer"; var position = str.search(/n/); /* position is now 6 */ - Character classes - Put a sequence of characters in brackets, and it defines a set of characters, any one of which matches [abcd] - Dashes can be used to specify spans of characters in a class [a-z] - A caret at the left end of a class definition means the opposite [^0-9] © 2006 Pearson Addison-Wesley. All rights reserved.

139 4.12 Pattern Matching (continued)
- Character classes (continued) - Character class abbreviations Abbr. Equiv. Pattern Matches \d [0-9] a digit \D [^0-9] not a digit \w [A-Za-z_0-9] a word character \W [^A-Za-z_0-9] not a word character \s [ \r\t\n\f] a whitespace character \S [^ \r\t\n\f] not a whitespace character - Variables in patterns are interpolated - Quantifiers - Quantifiers in braces Quantifier Meaning {n} exactly n repetitions {m,} at least m repetitions {m, n} at least m but not more than n repetitions © 2006 Pearson Addison-Wesley. All rights reserved.

140 4.12 Pattern Matching (continued)
- Quantifiers (continued) - Other quantifiers (just abbreviations for the most commonly used quantifiers) - * means zero or more repetitions e.g., \d* means zero or more digits - + means one or more repetitions e.g., \d+ means one or more digits - ? Means zero or one e.g., \d? means zero or one digit © 2006 Pearson Addison-Wesley. All rights reserved.

141 4.12 Pattern Matching (continued)
- Anchors - The pattern can be forced to match only at the left end with ^; at the end with $ e.g., /^Lee/ matches "Lee Ann" but not "Mary Lee Ann" /Lee Ann$/ matches "Mary Lee Ann", but not "Mary Lee Ann is nice" - The anchor operators (^ and $) do not match characters in the string--they match positions, at the beginning or end - Pattern modifiers - The i modifier tells the matcher to ignore the case of letters /oak/i matches "OAK" and "Oak" and … © 2006 Pearson Addison-Wesley. All rights reserved.

142 4.12 Pattern Matching (continued)
- Pattern modifiers (continued) - The x modifier tells the matcher to ignore whitespace in the pattern (allows comments in patterns) - Other Pattern Matching Methods of String replace(pattern, string) - Finds a substring that matches the pattern and replaces it with the string (g modifier can be used) var str = "Some rabbits are rabid"; str.replace(/rab/g, "tim"); str is now "Some timbits are timid" $1 and $2 are both set to "rab" © 2006 Pearson Addison-Wesley. All rights reserved.

143 4.12 Pattern Matching (continued)
match(pattern) - The most general pattern-matching method - Returns an array of results of the pattern- matching operation - With the g modifier, it returns an array of the substrings that matched - Without the g modifier, first element of the returned array has the matched substring, the other elements have the values of $1, … var str = "My 3 kings beat your 2 aces"; var matches = str.match(/[ab]/g); - matches is set to ["b", "a", "a"] split(parameter) "," and /,/ both work  SHOW forms_check.html © 2006 Pearson Addison-Wesley. All rights reserved.

144 4.13 Debugging JavaScript - IE6
- Select Internet Options from the Tools menu - Choose the Advanced tab - Uncheck the Disable script debugging box - Check the Display a notification about every script error box - Now, a script error causes a small window to be opened with an explanation of the error - NS7 - Select Tasks, Tools, and JavaScript Console - A small window appears to display script errors - Remember to Clear the console after using an error message – avoids confusion - Select Tools, Web Development, JavaScript Console © 2006 Pearson Addison-Wesley. All rights reserved.

145 JavaScript and HTML Documents
Chapter 5 JavaScript and HTML Documents

146 5.1 JavaScript Execution Environment
- The JavaScript Window object represents the window in which the browser displays documents - The Window object provides the largest enclosing referencing environment for scripts - All global variables are properties of Window - Implicitly defined Window properties: - document - a reference to the Document object that the window displays - frames - an array of references to the frames of the document - Every Document object has: - forms - an array of references to the forms of the document - Each Form object has an elements array, which has references to the form’s elements - Document also has anchors, links, & images © 2006 Pearson Addison-Wesley. All rights reserved.

147 5.2 The Document Object Model
- DOM 0 is supported by all JavaScript-enabled browsers (no written specification) - DOM 1 was released in 1998 - DOM 2 is the latest approved standard - Nearly completely supported by NS7 - IE6’s support is lacking some important things - The DOM is an abstract model that defines the interface between HTML documents and application programs—an API  SHOW document & DOM tree (p. 192) - A language that supports the DOM must have a binding to the DOM constructs - In the JavaScript binding, HTML elements are represented as objects and element attributes are represented as properties e.g., <input type = "text" name = "address"> would be represented as an object with two properties, type and name, with the values "text" and "address" © 2006 Pearson Addison-Wesley. All rights reserved.

148 5.3 Element Access in JavaScript
- There are several ways to do it - Example (a document with just one form and one widget): <form action = ""> <input type = "button" name = "pushMe"> </form> 1. DOM address document.forms[0].element[0] Problem: document changes 2. Element names – requires the element and all of its ancestors (except body) to have name attributes - Example: <form name = "myForm" action = ""> document.myForm.pushMe Problem: XHTML 1.1 spec doesn’t allow the name attribute on form elements © 2006 Pearson Addison-Wesley. All rights reserved.

149 5.3 Element Access in JavaScript
(continued) 3. getElementById Method (defined in DOM 1) - Example: <form action = ""> <input type = "button" id = "pushMe"> </form> document.getElementById("pushMe") - Checkboxes and radio button have an implicit array, which has their name <form id = "topGroup"> <input type = "checkbox" name = "toppings" value = "olives" /> ... value = "tomatoes" /> var numChecked = 0; var dom = document.getElementById("topGroup"); for index = 0; index < dom.toppings.length; index++) if (dom.toppings[index].checked] numChecked++; © 2006 Pearson Addison-Wesley. All rights reserved.

150 5.4 Events and Event Handling
- An event is a notification that something specific has occurred, either with the browser or an action of the browser user - An event handler is a script that is implicitly executed in response to the appearance of an event - The process of connecting an event handler to an event is called registration - Don’t use document.write in an event handler, because the output may go on top of the display Event Tag Attribute blur onblur change onchange click onclick focus onfocus load onload mousedown onmousedown mousemove onmousemove mouseout onmouseout mouseover onmouseover mouseup onmouseup select onselect submit onsubmit unload onunload © 2006 Pearson Addison-Wesley. All rights reserved.

151 5.4 Events and Event Handling
(continued) - The same attribute can appear in several different tags e.g., The onclick attribute can be in <a> and <input> - A text element gets focus in three ways: 1. When the user puts the mouse cursor over it and presses the left button 2. When the user tabs to the element 3. By executing the focus method  SHOW Table 5.2 - Event handlers can be registered in two ways: 1. By assigning the event handler script to an event tag attribute onclick = "alert('Mouse click!');" onclick = "myHandler();" © 2006 Pearson Addison-Wesley. All rights reserved.

152 5.5 Handling Events from Body Elements
- Example: the load event - triggered when the loading of a document is completed SHOW load.html & display 5.6 Handling Events from Button - Plain Buttons – use the onclick property - Radio buttons - If the handler is registered in the markup, the particular button that was clicked can be sent to the handler as a parameter e.g., if planeChoice is the name of the handler and the value of a button is 172, use onclick = ″planeChoice(172)″ - This is another way of choosing the clicked button  SHOW radio_click.html & display © 2006 Pearson Addison-Wesley. All rights reserved.

153 5.6 Handling Events from Button Elements (continued)
- If the handler is registered by assigning it to a property of the JavaScript objects associated with the HTML elements. As in: var dom = document.getElementById(″myForm″) dom.elements[0].onclick = planeChoice; - This registration must follow both the handler function and the XHTML form - If this is done for a radio button group, each element of the array must be assigned - In this case, the checked property of a radio button object is used to determine whether a button is clicked - If the name of the buttons is planeButton var dom = document.getElementById(″myForm″); for (var index = 0; index < dom.planeButton.length; index++) { if (dom.planeButton[index].checked) { plane = dom.planeButton[index].value; break; }  SHOW radio_click2.html © 2006 Pearson Addison-Wesley. All rights reserved.

154 5.6 Handling Events from Button Elements (continued)
- The disadvantage of specifying handlers by assigning them to event properties is that there is no way to use parameters - The advantages of specifying handlers by assigning them to event properties are: 1. It is good to keep HTML and JavaScript separate 2. The handler could be changed during use 5.7 Handling Events from Textbox and Password Elements - The Focus Event - Can be used to detect illicit changes to a text box by blurring the element every time the element acquires focus  SHOW nochange.html © 2006 Pearson Addison-Wesley. All rights reserved.

155 5.7 Handling Events from Textbox and Password Elements (continued)
- Checking Form Input - A good use of JavaScript, because it finds errors in form input before it is sent to the server for processing - So, it saves both: 1. Server time, and 2. Internet time - Things that must be done: 1. Detect the error and produce an alert message 2. Put the element in focus (the focus function) 3. Select the element (the select function) - The focus function puts the element in focus, which puts the cursor in the element document.getElementById("phone").focus(); - The select function highlights the text in the element © 2006 Pearson Addison-Wesley. All rights reserved.

156 5.7 Handling Events from Textbox and Password Elements (continued)
- To keep the form active after the event handler is finished, the handler must return false - Problems: 1. With IE6, focus and select only work if the handler is registered by assigning it to the element event property 2. With NS7, select works, but focus does not - Example – comparing passwords - The form just has two password input boxes to get the passwords and Reset and Submit buttons - The event handler is triggered by the Submit button © 2006 Pearson Addison-Wesley. All rights reserved.

157 5.7 Handling Events from Textbox and Password Elements (continued)
- Handler actions: 1. If no password has been typed in the first box, focus on that box and return false 2. If the two passwords are not the same, focus and select the first box and return false if they are the same, return true --> SHOW pswd_chk.html - Another Example – Checking the format of a name and phone number - The event handler will be triggered by the change event of the text boxes for the name and phone number - If an error is found in either, an alert message is produced and both focus and select are called on the text box element  SHOW validator.html © 2006 Pearson Addison-Wesley. All rights reserved.

158 5.8 The DOM 2 Event Model - Does not include DOM 0 features, but they are still supported by browsers - DOM 2 is modularized—one module is Events, which has two submodules, HTMLEvents and MouseEvents, whose interfaces are Event (blur, change, etc.) and MouseEvent (click, mouseup, etc.) - Event propagation - The node of the document tree where the event is created is called the target node - The capturing phase (the first phase) - Events begin at the root and move toward the target node - Registered and enabled event handlers at nodes along the way are run - The second phase is at the target node - If there are registered handlers there for the event, they are run - The third phase is the bubbling phase - Event goes back to the root; all encountered registered handlers are run © 2006 Pearson Addison-Wesley. All rights reserved.

159 5.8 The DOM 2 Event Model (continued)
- Not all events bubble (e.g., load and unload) - Any handler can stop further event propagation by calling the stopPropagation method of the Event object - DOM 2 model uses the Event object method, preventDefault, to stop default operations, such as submission of a form, if an error has been detected - Event handler registration is done with the addEventListener method - Three parameters: 1. Name of the event, as a string literal 2. The handler function 3. A Boolean value that specifies whether the event is enabled during the capturing phase node.addEventListener( "change", chkName, false); © 2006 Pearson Addison-Wesley. All rights reserved.

160 5.8 The DOM 2 Event Model (continued)
- A temporary handler can be created by registering it and then unregistering it with removeEventListener - The currentTarget property of Event always references the object on which the handler is being executed - The MouseEvent interface (a subinterface of Event) has two properties, clientX and clientY, that have the x and y coordinates of the mouse cursor, relative to the upper left corner of the browser window - An example: A revision of validator, using the DOM 2 event model  SHOW validator2.html - Note: DOM 0 and DOM 2 event handling can be mixed in a document © 2006 Pearson Addison-Wesley. All rights reserved.

161 5.9 The navigator object - Indicates which browser is being used
- Two useful properties 1. The appName property has the browser’s name 2. The appVersion property has the version # - Microsoft has chosen to set the appVersion of IE6 to 4 (?) - Netscape has chosen to set the appVersion of NS7 to 5.0 (?)  SHOW navigate.html © 2006 Pearson Addison-Wesley. All rights reserved.

162 Dynamic Documents with JavaScript
Chapter 6 Dynamic Documents with JavaScript

163 6.1 Introduction 6.2 Element Positioning
Def: A dynamic HTML document is one whose tag attributes, tag contents, or element style properties can be changed after the document has been and is still being displayed by a browser 6.2 Element Positioning - CSS-P was released by W3C in 1997 - Completely supported by IE6 and NS7 - The position of any element is dictated by the three style properties: position, left, and top - The three possible values of position are absolute, relative, and static - Absolute Positioning <p style = "position: absolute; left: 50px; top: 100px;">  SHOW absPos.html © 2006 Pearson Addison-Wesley. All rights reserved.

164 6.2 Element Positioning (continued)
- If an element is nested inside another element and is absolutely positioned, the top and left properties are relative to the enclosing element  SHOW absPos2.html - Relative Positioning - If no top and left properties are specified, the element is placed exactly where it would have been placed if no position property were given - But it can be moved later - If top and left properties are given, they are offsets from where it would have placed without the position property being specified  SHOW relPos.html - Static Positioning - The default value if position is not specified © 2006 Pearson Addison-Wesley. All rights reserved.

165 6.3 Moving Elements 6.4 Element Visibility
- If position is set to either absolute or relative, the element can be moved after it is displayed - Just change the top and left property values with a script --> SHOW mover.html 6.4 Element Visibility - The visibility property of an element controls whether it is displayed - The values are visible and hidden if (dom.visibility == "visible" dom.visibility = "hidden"; else dom.visibility = "visible";  SHOW showHide.html © 2006 Pearson Addison-Wesley. All rights reserved.

166 6.5 Changing Colors and Fonts
- Background color is controlled by the backgroundColor property - Foreground color is controlled by the color property Background color: <input type = "text" size = "10" name = "background" onchange = "setColor('background', this.value)"> - The actual parameter this.value works because at the time of the call, this is a reference to the text box (the element in which the call is made) - So, this.value is the name of the new color  SHOW dynColors.html © 2006 Pearson Addison-Wesley. All rights reserved.

167 6.5 Dynamic Colors and Fonts (continued)
- Changing fonts - We can change the font properties of a link by using the mouseover and mouseout events to trigger a script that makes the changes onmouseover = "this.style.color = 'blue'; this.style.font = 'italic 16pt Times';" onmouseout = "this.style.color = 'black'; this.style.font = 'normal 16pt Times';” SHOW dynLink.html 6.6 Dynamic Content - The content of an HTML element is addressed with the value property of its associated JavaScript object  SHOW dynValue.html © 2006 Pearson Addison-Wesley. All rights reserved.

168 6.7 Stacking Elements - The z-index attribute determines which element
is in front and which are covered by the front element - The JavaScript property associated with the z-index attribute is zIndex - z-index can be changed dynamically (by changing zIndex - An image element can have an onclick attribute, so images can be clicked to trigger event handlers - Anchors can also trigger event handlers when they are clicked - The href attribute can be set to call a JavaScript function by assigning it the call, with 'JAVASCRIPT' attached to the call code <a href = "JAVASCRIPT:fun()"> © 2006 Pearson Addison-Wesley. All rights reserved.

169 6.7. Stacking Elements (continued)
- To change stacking order, the handler function must change the zIndex property value of the element - A call to the function from an element sets the zIndex value of the new top element to 10 and the zIndex value of the old top element to 0 - It also sets the current top to the new top SHOW stacking.html 6.8 Locating the Mouse Cursor - The coordinates of the element that caused an event are available in the clientX and clientY properties of the event object - These are relative to upper left corner of the browser display window - screenX and screenY are relative to the upper left corner of the whole client screen © 2006 Pearson Addison-Wesley. All rights reserved.

170 6.8 Locating the Mouse Cursor (continued)
- If we want to locate the mouse cursor when the mouse button is clicked, we can use the click event  SHOW where.html 6.9 Reacting to a Mouse Click - A mouse click can be used to trigger an action, no matter where the mouse cursor is in the display - Use event handlers for onmousedown and onmouseup that change the visibility attribute of the message --> SHOW anywhere.html 6.10 Slow Movement of Elements - To animate an element, it must be moved by small amounts, many times, in rapid succession - JavaScript has two ways to do this, but we cover just one: setTimeout("fun()", n) © 2006 Pearson Addison-Wesley. All rights reserved.

171 6.10 Slow Movement of Elements
(continued) - Example: move a text element from its initial position (100, 100) to a new position (300, 300) - Use the onload attribute of the body element to initialize the position of the element - Use a move function to change the top and left attributes by one pixel in the direction of the destination - A problem: coordinate properties are stored as strings, which include the units ("150px") - Another problem: We need to use some HTML special characters (‘<‘ and ‘--’) 1. XML parsers may remove all comments 2. Put the script in a CDATA section 3. Put JavaScript in separate file © 2006 Pearson Addison-Wesley. All rights reserved.

172 6.10 Slow Movement of Elements
(continued) - These are problems of validation only - Both IE6 and NS7 deal correctly with comments SHOW moveText.html 6.11 Dragging and Dropping an Element - We can use mousedown, mousemove, and mouseup events to grab, drag, and drop - We know how to move an element - just change its left and top properties - Example: magnetic poetry - The DOM 2 event model is required (the Event object and its property, currentTarget) - We use both DOM 0 and DOM 2 models (DOM 0 to call the mousedown handler, grabber) © 2006 Pearson Addison-Wesley. All rights reserved.

173 6.11 Dragging and Dropping an Element
- Drag and drop requires three processes: 1. Get the dom of the element to be moved when the mouse button is pressed down (onmousedown) while the mouse cursor is over the element to be moved - We can get the DOM of the element on which an event occurred with the currentTarget property of the event object 2. Move the element by changing its top and left properties of the element as the mouse cursor is moved (onmousemove) - Compute the distance of each move as the difference between the current position ( the left and top values) and the mouse click position (clientX and clientY) 3. Dropping the element when the mouse button is released by undefining the dom used to carry out the move --> SHOW dragNDrop.html © 2006 Pearson Addison-Wesley. All rights reserved.

174 Chapter 8 Introduction to XML

175 8.1 Introduction - SGML is a meta-markup language
- Developed in the early 1980s; ISO std. In 1986 - HTML was developed using SGML in the early 1990s - specifically for Web documents - Two problems with HTML: 1. Fixed set of tags and attributes - User cannot define new tags or attributes - So, the given tags must fit every kind of document, and the tags cannot connote any particular meaning 2. There are no restrictions on arrangement or order of tag appearance in a document - One solution to the first of these problems: Let each group of users define their own tags (with implied meanings) (i.e., design their own “HTML”s using SGML) © 2006 Pearson Addison-Wesley. All rights reserved.

176 8.1 Introduction (continued)
- Problem with using SGML: - It’s too large and complex to use, and it is very difficult to build a parser for it - A better solution: Define a lite version of SGML - XML is not a replacement for HTML - HTML is a markup language used to describe the layout of any kind of information - XML is a meta-markup language that can be used to define markup languages that can define the meaning of specific kinds of information - XML is a very simple and universal way of storing and transferring data of any kind - XML does not predefine any tags - XML has no hidden specifications - All documents described with an XML-derived markup language can be parsed with a single parser © 2006 Pearson Addison-Wesley. All rights reserved.

177 8.1 Introduction (continued)
- We will refer to an XML-based markup language as a tag set - Strictly speaking, a tag set is an XML application, but that terminology can be confusing - An XML processor is a program that parses XML documents and provides the parts to an application - Documents that use an XML-based markup language is an XML document - Both IE6 and NS7 support basic XML 8.2 The Syntax of XML - The syntax of XML is in two distinct levels: 1. The general low-level rules that apply to all XML documents 2. For a particular XML tag set, either a document type definition (DTD) or an XML schema © 2006 Pearson Addison-Wesley. All rights reserved.

178 8.2 The Syntax of XML (continued)
- General XML Syntax - XML documents consist of: 1. data elements 2. markup declarations (instructions for the XML parser) 3. processing instructions (for the application program that is processing the data in the document) - All XML documents begin with an XML declaration: <?xml version = "1.0" encoding = "utf-8"?> - XML comments are just like HTML comments - XML names: - Must begin with a letter or an underscore - They can include digits, hyphens, and periods - There is no length limitation - They are case sensitive (unlike HTML names) © 2006 Pearson Addison-Wesley. All rights reserved.

179 8.2 The Syntax of XML (continued)
- Syntax rules for XML: same as those of XHTML - Every XML document defines a single root element, whose opening tag must appear as the first line of the document - An XML document that follows all of these rules is well formed <?xml version = "1.0"> <ad> <year> 1960 </year> <make> Cessna </make> <model> Centurian </model> <color> Yellow with white trim </color> <location> <city> Gulfport </city> <state> Mississippi </state> </location> </ad> © 2006 Pearson Addison-Wesley. All rights reserved.

180 8.2 The Syntax of XML (continued)
- Attributes are not used in XML the way they are in HTML - In XML, you often define a new nested tag to provide more info about the content of a tag - Nested tags are better than attributes, because attributes cannot describe structure and the structural complexity may grow - Attributes should always be used to identify numbers or names of elements (like HTML id and name attributes) © 2006 Pearson Addison-Wesley. All rights reserved.

181 8.2 The Syntax of XML (continued)
<!-- A tag with one attribute --> <patient name = "Maggie Dee Magpie"> ... </patient> <!-- A tag with one nested tag --> <patient> <name> Maggie Dee Magpie </name> <!-- A tag with one nested tag, which contains three nested tags --> <name> <first> Maggie </first> <middle> Dee </middle> <last> Magpie </last> </name> © 2006 Pearson Addison-Wesley. All rights reserved.

182 8.3 XML Document Structure
- An XML document often uses two auxiliary files: - One to specify the structural syntactic rules - One to provide a style specification - An XML document has a single root element, but often consists of one or more entities - Entities range from a single special character to a book chapter - An XML document has one document entity - All other entities are referenced in the document entity - Reasons for entity structure: 1. Large documents are easier to manage 2. Repeated entities need not be literally repeated 3. Binary entities can only be referenced in the document entities (XML is all text!) © 2006 Pearson Addison-Wesley. All rights reserved.

183 8.3 XML Document Structure (continued)
- When the XML parser encounters a reference to a non-binary entity, the entity is merged in - Entity names: - No length limitation - Must begin with a letter, a dash, or a colon - Can include letters, digits, periods, dashes, underscores, or colons - A reference to an entity has the form: &entity_name; - One common use of entities is for special characters that may be used for markup delimiters - These are predefined (as in XHTML): < < > > & & " " ' &apos; - The user can only define entities in a DTD © 2006 Pearson Addison-Wesley. All rights reserved.

184 8.3 XML Document Structure (continued)
- If several predefined entities must appear near each other in a document, it is better to avoid using entity references - Character data section <![CDATA[ content ]]> e.g., instead of Start > > > > HERE < < < < use <![CDATA[Start >>>> HERE <<<<]]> - If the CDATA content has an entity reference, it is taken literally © 2006 Pearson Addison-Wesley. All rights reserved.

185 8.4 Document Type Definitions
- A DTD is a set of structural rules called declarations - These rules specify a set of elements, along with how and where they can appear in a document - Purpose: provide a standard form for a collection of XML documents and define a markup language for them - Not all XML documents have or need a DTD - The DTD for a document can be internal or external - All of the declarations of a DTD are enclosed in the block of a DOCTYPE markup declaration - DTD declarations have the form: <!keyword … > - There are four possible declaration keywords: ELEMENT, ATTLIST, ENTITY, and NOTATION © 2006 Pearson Addison-Wesley. All rights reserved.

186 8.4 Document Type Definitions (continued)
- Declaring Elements - Element declarations are similar to BNF - An element declaration specifies the name of an an element, and the element’s structure - If the element is a leaf node of the document tree, its structure is in terms of characters - If it is an internal node, its structure is a list of children elements (either leaf or internal nodes) - General form: <!ELEMENT element_name (list of child names)> e.g., <!ELEMENT memo (from, to, date, re, body)> memo from to date re body © 2006 Pearson Addison-Wesley. All rights reserved.

187 8.4 Document Type Definitions (continued)
- Declaring Elements (continued) - Child elements can have modifiers, +, *, ? e.g., <!ELEMENT person (parent+, age, spouse?, sibling*)> - Leaf nodes specify data types, most often PCDATA, which is an acronym for parsable character data - Data type could also be EMPTY (no content) and ANY (can have any content) - Example of a leaf declaration: <!ELEMENT name (#PCDATA)> - Declaring Attributes - General form: <!ATTLIST el_name at_name at_type [default]> © 2006 Pearson Addison-Wesley. All rights reserved.

188 8.4 Document Type Definitions (continued)
- Declaring Attributes (continued) - Attribute types: there are ten different types, but we will consider only CDATA - Default values: a value #FIXED value (every element will have this value), #REQUIRED (every instance of the element must have a value specified), or #IMPLIED (no default value and need not specify a value) <!ATTLIST car doors CDATA "4"> <!ATTLIST car engine_type CDATA #REQUIRED> <!ATTLIST car price CDATA #IMPLIED> <!ATTLIST car make CDATA #FIXED "Ford"> <car doors = "2" engine_type = "V8"> ... </car> © 2006 Pearson Addison-Wesley. All rights reserved.

189 8.4 Document Type Definitions (continued)
- Declaring Entities - Two kinds: - A general entity can be referenced anywhere in the content of an XML document - A parameter entity can be referenced only in a markup declaration - General form of declaration: <!ENTITY [%] entity_name "entity_value"> e.g., <!ENTITY jfk "John Fitzgerald Kennedy"> - A reference: &jfk; - If the entity value is longer than a line, define it in a separate file (an external text entity) <!ENTITY entity_name SYSTEM "file_location">  SHOW planes.dtd © 2006 Pearson Addison-Wesley. All rights reserved.

190 8.4 Document Type Definitions (continued)
- XML Parsers - Always check for well formedness - Some check for validity, relative to a given DTD - Called validating XML parsers - You can download a validating XML parser from: - Internal DTDs <!DOCTYPE root_name [ ]> - External DTDs <!DOCTYPE XML_doc_root_name SYSTEM “DTD_file_name”> © 2006 Pearson Addison-Wesley. All rights reserved.

191 8.5 Namespaces - A markup vocabulary is the collection of all of the
element types and attribute names of a markup language (a tag set) - An XML document may define its own tag set and also use those of another tag set - CONFLICTS! - An XML namespace is a collection of names used in XML documents as element types and attribute names - The name of an XML namespace has the form of a URI - A namespace declaration has the form: <element_name xmlns[:prefix] = URI> - The prefix is a short name for the namespace, which is attached to names from the namespace in the XML document <gmcars xmlns:gm = " - In the document, you can use <gm:pontiac> - Purposes of the prefix: 1. Shorthand 2. URI includes characters that are illegal in XML © 2006 Pearson Addison-Wesley. All rights reserved.

192 8.5 Namespaces (continued)
- Can declare two namespaces on one element <gmcars xmlns:gm = " xmlns:html = " - The gmcars element can now use gm names and html names - One namespace can be made the default by leaving the prefix out of the declaration 8.6 XML Schemas - Problems with DTDs: 1. Syntax is different from XML - cannot be parsed with an XML parser 2. It is confusing to deal with two different syntactic forms 3. DTDs do not allow specification of particular kinds of data © 2006 Pearson Addison-Wesley. All rights reserved.

193 8.6 XML Schemas (continued)
- XML Schemas is one of the alternatives to DTD - Two purposes: 1. Specify the structure of its instance XML documents 2. Specify the data type of every element and attribute of its instance XML documents - Schemas are written using a namespace: - Every XML schema has a single root, schema The schema element must specify the namespace for schemas as its xmlns:xsd attribute - Every XML schema itself defines a tag set, which must be named targetNamespace = " © 2006 Pearson Addison-Wesley. All rights reserved.

194 8.6 XML Schemas (continued)
- If we want to include nested elements, we must set the elementFormDefault attribute to qualified - The default namespace must also be specified xmlns = " - A complete example of a schema element: <xsd:schema <!-- Namespace for the schema itself --> xmlns:xsd = " <!-- Namespace where elements defined here will be placed --> targetNamespace = " <!-- Default namespace for this document --> <!-- Next, specify non-top-level elements to be in the target namespace --> elementFormDefault = "qualified"> © 2006 Pearson Addison-Wesley. All rights reserved.

195 8.6 XML Schemas (continued)
- Defining an instance document - The root element must specify the namespaces it uses 1. The default namespace 2. The standard namespace for instances (XMLSchema-instance) 3. The location where the default namespace is defined, using the schemaLocation attribute, which is assigned two values <planes xmlns = " xmlns:xsi = xsi:schemaLocation = " planes.xsd" > - Data Type Categories 1. Simple (strings only, no attributes and no nested elements) 2. Complex (can have attributes and nested elements) © 2006 Pearson Addison-Wesley. All rights reserved.

196 8.6 XML Schemas (continued)
- XMLS defines 44 data types - Primitive: string, Boolean, float, … - Derived: byte, decimal, positiveInteger, … - User-defined (derived) data types – specify constraints on an existing type (the base type) - Constraints are given in terms of facets (totalDigits, maxInclusive, etc.) - Both simple and complex types can be either named or anonymous - DTDs define global elements (context is irrelevant) - With XMLS, context is essential, and elements can be either: 1. Local, which appears inside an element that is a child of schema, or 2. Global, which appears as a child of schema © 2006 Pearson Addison-Wesley. All rights reserved.

197 8.6 XML Schemas (continued)
- Defining a simple type: - Use the element tag and set the name and type attributes <xsd:element name = "bird" type = "xsd:string" /> - An instance could have: <bird> Yellow-bellied sap sucker </bird> - Element values can be constant, specified with the fixed attribute fixed = "three-toed" - User-Defined Types - Defined in a simpleType element, using facets specified in the content of a restriction element - Facet values are specified with the value attribute © 2006 Pearson Addison-Wesley. All rights reserved.

198 8.6 XML Schemas (continued)
<xsd:simpleType name = "middleName" > <xsd:restriction base = "xsd:string" > <xsd:maxLength value = "20" /> </xsd:restriction> </xsd:simpleType> - There are several categories of complex types, but we discuss just one, element-only elements - Element-only elements are defined with the complexType element - Use the sequence tag for nested elements that must be in a particular order - Use the all tag if the order is not important © 2006 Pearson Addison-Wesley. All rights reserved.

199 8.6 XML Schemas (continued)
<xsd:complexType name = "sports_car" > <xsd:sequence> <xsd:element name = "make" type = "xsd:string" /> <xsd:element name = "model " <xsd:element name = "engine" <xsd:element name = "year" </xsd:sequence> </xsd:complexType> - Nested elements can include attributes that give the allowed number of occurrences (minOccurs, maxOccurs, unbounded)  SHOW planes.xsd and planes1.xml - We can define nested elements elsewhere <xsd:element name = "year" > <xsd:simpleType> <xsd:restriction base = "xsd:decimal" > <xsd:minInclusive value = "1990" /> <xsd:maxInclusive value = "2003" /> </xsd:restriction> </xsd:simpleType> </xsd:element> © 2006 Pearson Addison-Wesley. All rights reserved.

200 8.6 XML Schemas (continued)
- The global element can be referenced in the complex type with the ref attribute <xsd:element ref = "year" /> - Validating Instances of XML Schemas - Can be done with several different tools - One of them is xsv, which is available from: - Note: If the schema is incorrect (bad format), xsv reports that it cannot find the schema 8.7 Displaying Raw XML Documents - There is no presentation information in an XML document - An XML browser should have a default style sheet for an XML document that does not specify one - You get a stylized listing of the XML  SHOW planes.xml © 2006 Pearson Addison-Wesley. All rights reserved.

201 8.8 Displaying XML Documents with CSS
- A CSS style sheet for an XML document is just a list of its tags and associated styles - The connection of an XML document and its style sheet is made through an xml-stylesheet processing instruction <?xml-stylesheet type = "text/css" href = "mydoc.css"?> --> SHOW planes.css and run planes.xml 8.9 XSLT Style Sheets - XSL began as a standard for presentations of XML documents - Split into three parts: - XSLT – Transformations - XPATH - XML Path Language - XSL-FO - Formatting objects - XSLT uses style sheets to specify transformations © 2006 Pearson Addison-Wesley. All rights reserved.

202 8.9 XSLT Style Sheets (continued)
- An XSLT processor merges an XML document into an XSLT style sheet - This merging is a template-driven process - An XSLT style sheet can specify page layout, page orientation, writing direction, margins, page numbering, etc. - The processing instruction we used for connecting a XSLT style sheet to an XML document is used to connect an XSLT style sheet to an XML document <?xml-stylesheet type = "text/xsl" href = "XSLT style sheet"?> - An example: <?xml version = "1.0"?> <!-- xslplane.xml --> href = "xslplane.xsl" ?> <plane> <year> 1977 </year> <make> Cessna </make> <model> Skyhawk </model> <color> Light blue and white </color> </plane> © 2006 Pearson Addison-Wesley. All rights reserved.

203 8.9 XSLT Style Sheets (continued)
- An XSLT style sheet is an XML document with a single element, stylesheet, which defines namespaces <xsl:stylesheet xmlns:xsl = " xmlns = " - If a style sheet matches the root element of the XML document, it is matched with the template: <xsl:template match = "/"> - A template can match any element, just by naming it (in place of /) - XSLT elements include two different kinds of elements, those with content and those for which the content will be merged from the XML doc - Elements with content often represent HTML elements <span style = "font-size: 14"> Happy Easter! </span> © 2006 Pearson Addison-Wesley. All rights reserved.

204 8.9 XML Transformations and Style Sheets (continued)
- XSLT elements that represent HTML elements are simply copied to the merged document - The XSLT value-of element - Has no content - Uses a select attribute to specify part of the XML data to be merged into the new document <xsl:value-of select = ”CAR/ENGINE" /> - The value of select can be any branch of the document tree --> SHOW xslplane.xsl and display xslplane.xml - The XSLT for-each element - Used when an XML document has a sequence of the same elements --> SHOW xslplanes.xml --> SHOW xslplanes.xsl & display © 2006 Pearson Addison-Wesley. All rights reserved.

205 8.10 XML Processors - Purposes:
1. Check the syntax of a document for well- formedness 2. Replace all references to entities by their definitions 3. Copy default values (from DTDs or schemas) into the document 4. If a DTD or schema is specified and the processor includes a validating parser, the structure of the document is validated - Two ways to check well-formedness: 1. A browser with an XML parser 2. A stand-alone XML parser - There are two different approaches to designing XML processors: - SAX and the DOM approach © 2006 Pearson Addison-Wesley. All rights reserved.

206 8.10 XML Processors (continued)
- The SAX (Simple API for XML) Approach: - Widely accepted and supported - Based on the concept of event processing: - Every time a syntactic structure (e.g., a tag, an attribute, etc.) is recognized, the processor raises an event - The application defines event handlers to respond to the syntactic structures - The DOM Approach - The DOM processor builds a DOM tree structure of the document (Similar to the processing by a browser of an XHTML document) - When the tree is complete, it can be traversed and processed © 2006 Pearson Addison-Wesley. All rights reserved.

207 8.10 XML Processors (continued)
- Advantages of the DOM approach: 1. Good if any part of the document must be accessed more than once 2. If any rearrangement of the document must be done, it is facilitated by having a representation of the whole document in memory 3. Random access to any part of the document is possible 4. Because the whole document is parsed before any processing takes place, processing of an invalid document is avoided - Disadvantages of the DOM approach: 1. Large documents require a large memory 2. The DOM approach is slower - Note: Most DOM processors use a SAX front end © 2006 Pearson Addison-Wesley. All rights reserved.

208 8.10 Web Services - The ultimate goal of Web services:
- Allow different software in different places, written in different languages and resident on different platforms, to connect and interoperate - The Web began as provider of markup documents, served through the HTTP methods, GET and POST - An information service system - A Web service is closely related to an information service - Rather than having a server provide documents, the server provides services, through server- resident software - The same Web server can provide both documents and services - The original Web services were provided via Remote Procedure Call (RPC), through two technologies, DCOM and CORBA - DCOM and CORBA use different protocols, which defeats the goal of universal component interoperability © 2006 Pearson Addison-Wesley. All rights reserved.

209 8.10 Web Services (continued)
- There are three roles required to provide and use Web services: 1. Service providers 2. Service requestors 3. A service registry - Web Serviced Definition Language (WSDL) - Used to describe available services, as well as of message protocols for their use - Such descriptions reside on the Web server - Universal Description, Discovery, and Integration Service (UDDI) - Used to create Web services registry, and also methods that allow a remote system to determine which services are available - Standard Object Access Protocol (SOAP) - An XML-based specification that defines the forms of messages and RPCs - Supports the exchange of information among distributed systems © 2006 Pearson Addison-Wesley. All rights reserved.


Download ppt "Chapter 1 Fundamentals."

Similar presentations


Ads by Google