CSE 190: Internet ECommerce - PowerPoint PPT Presentation

About This Presentation
Title:

CSE 190: Internet ECommerce

Description:

final makeup='yes' 14 /final /student student id='A02222222' exam1 15 /exam1 ... final makeup= no' 15 /final /student /studentgrades XML Syntax ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 20
Provided by: anandbala
Learn more at: https://cseweb.ucsd.edu
Category:

less

Transcript and Presenter's Notes

Title: CSE 190: Internet ECommerce


1
CSE 190 Internet E-Commerce
  • Lecture 17 XML, XSL

2
Problem Communicating data
  • Consider A merchant, like Amazon, wants to
    publish its catalog of books on sale to a third
    party (e.g. a book club), so that party can
    create a newsletter using the data.
  • Question What format does Amazon put that data
    in? How do we communicate that format to the
    third party?

3
Some Approaches
  • Define binary format based upon some structure.
    Distribute a Word document describing the layout
    of the fields.struct Book char isbn 40
    char authorFirstName 60 char
    authorLastName 60 float salePrice
    enum SHIPPING_AVAILABILITY IN_STOCK, SOLD_OUT
    Limitation Adding a new field means
    the parsing code must change takes weeks for
    programmers to read the specification and write
    the parser future applications like this require
    equal amounts of work.

4
Some Approaches
  • 2. Output the data in HTML, send a Word document
    describing the layout of the data. Ex
  • lthtmlgtltheadgtlt/headgtltbodygt
  • lttablegt
  • lttheadgtlttrgtltthgtTitlelt/thgtltthgtISBNlt/thgtltthgtAuthorlt/
    thgtltthgtPricelt/thgtlt/trgt
  • lttrgtlttdgtFast Food Nationlt/tdgtlttdgt234234234lt/tdgtlttd
    gtEric Schlosserlt/tdgtlttdgt17.50lt/tdgtlt/trgt
  • Evaluation
  • At least we can use the HTML parser to get the
    data
  • Still have to parse the resulting HTML tree to
    get data
  • Adding new data fields still implies writing more
    parse code

5
Approach Common syntax with user defined tags
  • Use a strict syntax for tags, but allow any tag
    name to be used
  • ltbooklistgt
  • ltbook isbn0395977894gt
  • lttitlegtFast Food Nationlt/titlegt
  • ltauthorgtEric Schlosserlt/authorgt
  • ltPricegt17.50lt/Pricegt
  • ltbook isbn1406066985gt .
  • Evaluation
  • Directly expresses the data we wanted to
    communicate
  • May re-use the same parser for many different
    applications
  • Easy to ignore unfamiliar tags
  • Call it XML Exchange Markup Language

6
What is XML?
  • XML Used to communicate data to machines.
  • HTML Used to communicate documents to people.
  • XML An application of SGML (a document type
    description language)
  • Unlike HTML, you may invent your own tags
  • Uses a strict syntax so that XML parsers may be
    reused for many applications
  • Unknown tags and attributes are ignored
  • May be used to separate content from presentation
    by applying a transformation to the data to
    create a readable document.
  • Written in plain-text files human-readable, not
    binary

7
XML Example
  • Show me the list of books on sale.
  • lt?xml version1.0 ?gt
  • ltbooklistgt
  • ltbook isbn234234234gt
  • lttitlegtFast Food Nationlt/titlegt
  • ltauthorgtEric Schlosserlt/authorgt
  • ltpricegt17.50lt/pricegt
  • ltin_stock /gt
  • lt/bookgt
  • ltbook isbn456465465gt
  • lttitlegtReligion Explainedlt/titlegt
  • ltauthorgtPascal Boyerlt/authorgt
  • ltpricegt24.99lt/pricegt
  • ltout_of_stock /gt
  • lt/bookgt
  • lt/booklistgt

8
XML Example
  • Show me the grades of all students.
  • lt?xml version1.0gt
  • ltstudentgradesgt
  • ltstudent idA02111111gt
  • ltexam1gt15lt/exam1gt
  • ltexam2gt15lt/exam2gt
  • ltprojectgt16lt/projectgt
  • ltfinal makeupyesgt14lt/finalgt lt/studentgt
  • ltstudent idA02222222gt
  • ltexam1gt15lt/exam1gt
  • ltexam2gt15lt/exam2gt
  • ltprojectgt16lt/projectgt
  • ltfinal makeupnogt15lt/finalgt lt/studentgt
  • lt/studentgradesgt

9
XML Syntax
  • Written in plain text (using notepad, e.g.)
  • All elements must have a closing tag
  • Ex ltpgtText text textlt/pgt
  • Forbidden ltpgtLine OneltpgtLine Two
  • Single element tags close themselves
  • Ex ltbr /gt
  • Not ltbrgttext text or ltbr /gttext textlt/brgt
  • Should start with an XML document indicator
  • Ex lt?xml version1.0?gt
  • Has one root element, the first in the document
  • All attribute values must be single or double
    quoted
  • Ex ltboat_ride price15.00 /gt
  • Not ltboat_ride price15.00 /gt
  • Not ltimage_size width100 /gt
  • Tags are case sensitive
  • Ex ltauthorgtEric Schlosserlt/authorgt
  • Not ltauthorgtEric Schlosserlt/Authorgt
  • Mime Type text/xml

10
Parsing
  • An XML parser may parse any XML document
  • Well-formed XML document follows the XML syntax
    rules
  • Valid XML document conforms to its specified DTD
    (Document Type Definition)
  • lt?xml version"1.0 ?gt
  • lt!DOCTYPE note SYSTEM "note.dtd"gt
  • ltnotegt
  • lttogtMannylt/togt
  • ltfromgtJannilt/fromgt
  • ltheadinggtReminderlt/headinggt
  • ltbodygtDon't forget me this weekend!lt/bodygt
  • lt/notegt

11
DTDs
  • DTDs Describe the allowable tags and attributes
    used in an XML document
  • lt!DOCTYPE TVSCHEDULE
  • lt!ELEMENT TVSCHEDULE (CHANNEL)gt
  • lt!ELEMENT CHANNEL (BANNER,DAY)gt
  • lt!ELEMENT BANNER (PCDATA)gt
  • lt!ELEMENT DAY (DATE,(HOLIDAYPROGRAMSLOT))gt
  • lt!ELEMENT HOLIDAY (PCDATA)gt lt!ELEMENT DATE
    (PCDATA)gt
  • lt!ELEMENT PROGRAMSLOT (TIME,TITLE,DESCRIPTION?)gt
  • lt!ELEMENT TIME (PCDATA)gt lt!ELEMENT TITLE
    (PCDATA)gt 
  • lt!ELEMENT DESCRIPTION (PCDATA)gt
  • lt!ATTLIST TVSCHEDULE NAME CDATA REQUIREDgt
  • lt!ATTLIST CHANNEL CHAN CDATA REQUIREDgt
  • lt!ATTLIST PROGRAMSLOT VTR CDATA IMPLIEDgt
  • lt!ATTLIST TITLE RATING CDATA IMPLIEDgt
  • lt!ATTLIST TITLE LANGUAGE CDATA IMPLIEDgt
  • gt
  • Written in SGML

12
Namespaces
  • Question How do we create a document using tags
    from two different DTDs?
  • We can just combine the two, using the tags from
    both DTDs
  • Problem What if both DTDs are using the same tag
    name to refer to different items?
  • Solution We can specify what namespace a tag is
    coming from.
  • Namespaces Specify the DTD that a tag name is
    relative to
  • Example
  • lt?xml version1.0?gt
  • ltamznbooklist xmlnsamznhttp//www.amazon.com/d
    eals/BLgt
  • ltamznbook isbn23423422gt
  • ltamznauthorgtEric Schlosserlt/amznauthorgt
  • ltloccategory xmlnslochttp//www.libraryofco
    ngress.org/classifygt Food,
    muckrakinglt/loccategorygt
  • ltamznpricegt17.50lt/amznpricegt
  • lt/amznbookgt
  • lt/amznbooklistgt

13
Viewing XML
  • XML documents may be viewed natively in IE,
    Netscape
  • Ex testdoc.xml
  • What if wed like to present the data to people,
    in a more readable way?
  • Useful for creating data documents, then a
    clearly separated way for viewing the data
  • XML Transformation into a view
  • CSS
  • XSLT

14
CSS
  • CSS Cascading Style Sheets
  • Applied to XML, similar to HTML
  • Example (note the tags in the selector)
  • CATALOG background-color ffffff width 100
  • CD display block margin-bottom 30pt
    margin-left 0
  • TITLE color FF0000 font-size 20pt
  • ARTIST color 0000FF font-size 20pt
  • COUNTRY,PRICE,YEAR,COMPANY display block
    color 000000 margin-left 20pt
  • Limitation Not all browser versions support XML
    CSS display

15
XSL
  • XSL XML Style Language
  • Provides declarative method for transform XML
    document into a viewable format (usually HTML)

16
XSL Example
  • lt?xml version"1.0 ?gt
  • ltxslstylesheet version"1.0 xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • ltxsltemplate match"/"gt
  • lthtmlgt
  • ltbodygt
  • lttable border"1"gt
  • lttrgt
  • ltthgtTitlelt/thgt
  • ltthgtArtistlt/thgt
  • lt/trgt
  • ltxslfor-each select"catalog/cd"gt
  • lttrgt
  • lttdgtltxslvalue-of select"title" /gtlt/tdgt
  • lttdgtltxslvalue-of select"artist" /gtlt/tdgt
  • lt/trgt
  • lt/xslfor-eachgt
  • lt/tablegt
  • lt/bodygt
  • lt/htmlgt

17
XSL Elements
  • xsltemplate
  • Replaces all elements matching the Xpath
    expression (e.g. / matches all elements) with
    the specified content
  • xslfor-each
  • For each element matching the select Xpath
    expression, the element is replaced with the
    specified content
  • xslvalue-of
  • Is replaced with the text value of the element
    specified by selects Xpath expression
  • Xpath A pattern language for XML
  • / -gt matches all elements
  • a/b -gt matches all A elements with children of
    tag B

18
XSL, Server-Side
  • XML file may be made human-friendly by specifying
    style sheet from within the XML document
  • Uses client-side support to transform into HTML
  • What if the browser doesnt support this?
  • The web server may do the transformation on the
    server side, giving the browser the resulting
    HTML.
  • May decide to detect the browser version before
    deciding on what method to use

19
References
  • Book Step by Step XML by Michael Young
  • URL http//www.w3schools.com/xml/
  • URL http//www.w3schools.com/xsl/
Write a Comment
User Comments (0)
About PowerShow.com