Week 7: More on XML XML Document Structure - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Week 7: More on XML XML Document Structure

Description:

var parser=new DOMParser(); var doc=parser.parseFromString(txt,'text/xml'); /* now access ... 18. Parsing JSON in Javascript. var p = eval('(' JSON_text ... – PowerPoint PPT presentation

Number of Views:154
Avg rating:3.0/5.0
Slides: 20
Provided by: eelabU
Category:
Tags: xml | document | more | structure | var | week

less

Transcript and Presenter's Notes

Title: Week 7: More on XML XML Document Structure


1
Week 7 More on XMLXML Document Structure
  • XML Syntax rules define the low level structure
    of XML
  • A Schema defines the higher level structure
  • What tag names
  • What each tag can contain
  • XML Schema languages
  • Document Type Definition (DTD)
  • W3C XML Schema
  • RelaxNG
  • Schematron

2
Document Type Definition (DTD)
  • DTD defines
  • What elements can be in an XML document.
  • What attributes an element can have and
    restrictions on their values.
  • What the content model for an element is what
    elements can occur within it and in what pattern.
  • Where text can occur
  • PCDATA -- parsed character data, entity
    references will be replaced
  • CDATA -- character data, can contain special
    characters
  • Why use a DTD
  • To enforce a standard that many people can rely
    on (for example, XHTML).
  • To provide structure for document authors to work
    within (for example, Docbook).
  • To enable validation of documents.

3
XML example
  • lt?xml version'1.0'?gt lt!DOCTYPE slides SYSTEM
    "slides.dtd"gt ltslidesgt ltfoilgt lttitlegtXML
    Basicslt/titlegt
  • ltitemizedlistgt ltlistitemgt XML is a standard
    for defining markup languages. lt/listitemgt
    ltlistitemgt XML defines the ltemphasisgt syntax
    lt/emphasisgt of markup. lt/listitemgt
    lt/itemizedlistgt lt/foilgt lt/slidesgt

4
DTD example
  • lt!ELEMENT slides (foilsection)gt
  • lt!ATTLIST slides "label CDATA IMPLIED
    status CDATA IMPLIED id ID IMPLIEDrevisionfl
    ag (changed added deleted off) IMPLIED"gt
  • lt!ELEMENT section (title,foil)gt
  • lt!ELEMENT foil (title,(itemizedlistpara))gt
  • lt!ELEMENT itemizedlist (listitem)gt
  • lt!ELEMENT listitem PCDATAgt
  • lt!ELEMENT para PCDATAgt
  • lt!ENTITY elec3609 ELEC3609(Web Technology)"gt
  • lt!ENTITY copy "x00A9"gt

5
Declaring Elements
  • The form of an element declaration
  • lt!ELEMENT element_name (names of child elements)gt
  • Example
  • lt!ELEMENT slides (foilsection)gt
  • Modifiers can be added to child element
    specifications
  • one or more occurrences
  • zero or more occurrences
  • ? zero or one occurrence.
  • If an element just contains text then its content
    model is just PCDATA

6
Declaring Attributes
  • The form of an attribute declaration
  • lt!ATTLIST element_name attribute_name
    attribute_type default_valuegt
  • For example
  • lt!ATTLIST slides label CDATA IMPLIEDgt
  • Possible default values for attributes are
  • a value - the value used, if none is specified in
    an element
  • FIXED - the value, which every element will have
    and which cannot be changed
  • REQUIRED - no default value is given every
    instance of the element must specify a value
  • IMPLIED - no default value is given the value
    may or may not be specified in an element.

7
Declaring Entities
  • Entities can be defined so that they can be
    referenced anywhere in the content.
  • The form of an entity declaration
  • lt!ENTITY entity_name "entity_value"gt
  • You can define an entity to represent a string
  • lt!ENTITY elec3609 ELEC3609 (Web Technology)"gt
  • In an XML document the string can be referenced
    by
  • I'm studying elec3609

8
What DTDs Can't Do
  • Can't say anything about the textual content of
    an element.
  • XML Schema proposals attempt to do this (XML
    Schema, RELAX-NG).
  • Equivalent to providing a (programming language)
    type system for XML.
  • RSS Really Simple Syndication
  • RSS is a standard format for web site summaries.
  • An RSS summary, at a minimum, is a document
    describing a "channel" consisting of
    URL-retrievable items. Each item consists of a
    title, link, and brief description. (RSS
    Specification) http//cyber.law.harvard.edu/rss/rs
    s.html
  • RSS defines a set of standard metadata elements
    for describing web resources (in particular, news
    feeds).
  • Recently Atom has been proposed as a successor to
    RSS http//www.atomenabled.org/

9
RSS example
  • ltxml version"1.0" encoding"ISO-8859-1"?gt ltrss
    version"0.91"gt
  • ltchannelgt
  • lttitlegtAnnouncements for ELEC3609lt/titlegt
  • ltlinkgthttp//www.eelab.usyd.edu.au/ELEC3609/cgi-b
    in/newslt/linkgt
  • ltdescriptiongt
  • This is an news service for ELEC3609 unit.
  • lt/descriptiongt
  • ltlanguagegten-uslt/languagegt
  • ltitemgt
  • lttitlegtAssignment 3lt/titlegt
  • ltlinkgthttp//www.eelab.usyd.edu.au/ELEC3609/cgi-bi
    n/ass3.txtlt/linkgt
  • ltdescriptiongt Assignment 3 is now available.
    lt/descriptiongt
  • lt/itemgt
  • lt/channelgt
  • lt/rssgt

10
XML Bookmark Exchange Language
  • Web browsers store bookmarks in a variety of
    formats, some provide tools to import bookmarks
    from other browsers, but often you can't go back
    again.
  • XBEL acts as an intermediate format between the
    Internet Explorer, Netscape and other formats.
  • Bookmarks organised into folders.
  • Bookmark entries and folders can have various
    properties encoded as sub-elements title,
    description, info, metadata.
  • Bookmark url is stored as an attribute

11
XSBEL example
  • lt?xml version"1.0"?gt
  • lt!DOCTYPE xbel PUBLIC "//IDN python.org//DTD XML
    Bookmark Exchange Language 1.0//EN//XML"
    "http//www.python.org/topics/xml/dtds/xbel-1.0.dt
    d"gt
  • ltxbelgt
  • ltinfogt
  • ltmetadata owner"Norman Walsh ltndw_at_nwalsh.comgt"
    /gt
  • lt/infogt
  • ltfolder id"perlbookmarks"gt
  • lttitlegtPerllt/titlegt
  • ltbookmark href"http//www.perl.com/"gt
  • lttitlegtPerl.comlt/titlegt lt/bookmarkgt
  • ltbookmark href"http//www.activestate.com/"gt
  • lttitlegtActiveState Tool Corp. (Perl for
    Win32)lt/titlegt lt/bookmarkgt
  • lt/foldergt
  • lt/xbelgt

12
VoiceXMLhttp//www.w3.org/TR/voicexml20/
  • VoiceXML is an XML-based markup language for the
    development of voice applications.
  • Using only a traditional Web infrastructure, you
    can create applications that are accessible over
    the telephone.
  • VoiceXML is designed for creating audio dialogs
    that feature synthesized speech, digitized audio,
    recognition of spoken and DTMF key input,
    recording of spoken input, telephony, and mixed
    initiative conversations

13
VoiceXML example
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltvxml version"2.0" lang"en"gt
  • ltformgt
  • ltfield name"city"gt ltpromptgt Where do you want
    to travel to? lt/promptgt ltoptiongt Edinburgh
    lt/optiongt ltoptiongt New York lt/optiongt ltoptiongt
    London lt/optiongt ltoptiongt Paris lt/optiongt
    ltoptiongt Stockholm lt/optiongt lt/fieldgt
  • ltfield name"travellers" type"number"gt
  • ltpromptgt How many are travelling to ltvalue
    expr"city"/gt? lt/promptgt lt/fieldgt
  • ltblockgt ltsubmit next"http//localhost/handler"
    namelist"city travellers"/gt lt/blockgt
  • lt/formgt
  • lt/vxmlgt

14
XML on the Web - AJAX
  • AJAX - Asynchronous Javascript and XML
  • Javascript makes a request to the server from
    within a page
  • Recieves an XML response, updates the page using
    the contents
  • XML is a data exchange language, easily parsed by
    Javascript
  • But, is there a better alternative?
  • JSON
  • Javascript Object Notation http//www.json.org/
  • Use Javascript as a data exchange language
  • No need to parse it, just evaluate and you have
    the data JSON

15
XML vs. JSON
  • JSON is purely for data interchange
  • XML is more general a markup langauge
  • JSON needs no parser - unless you want to be
    secure
  • XML allows for validation independent of the
    application
  • JSON is easier to write code for in Javascript
  • XML is better when there might be more than one
    kind of consumer for the data

16
JSON
  • "Name" "John Smith", "address"
    "streetAddress" "21 2nd Street", "city" "New
    York", , "phoneNumbers" "212 555-1234", "646
    555-4567"

17
Parsing XML in Javascripthttp//www.w3schools.com
/Xml/xml_parser.asp
  • var parsernew DOMParser()
  • var docparser.parseFromString(txt,"text/xml")
    / now access elements using the DOM API / to
    doc.getElementsByTagName("to")0.childNodes0.no
    deVal

18
Parsing JSON in Javascript
  • var p eval("(" JSON_text ")")
  • / or using a parser (safer) /
  • var p json_parse(JSON_text)
  • / now access the data elements /
  • to p.to

19
Summary
  • XML is a simple markup language with hierarchical
    structure.
  • Documents can be shown to be well formed or
    valid.
  • DTD allows standard document types to be defined
    (eg. XHMTL).
  • JSON is an alternative data format for sending
    data structures to web browsers.
  • Parsers process the XML or JSON to give programs
    access to the data
Write a Comment
User Comments (0)
About PowerShow.com