Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 190: Internet E-Commerce Lecture 17: XML, XSL.

Similar presentations


Presentation on theme: "CSE 190: Internet E-Commerce Lecture 17: XML, XSL."— Presentation transcript:

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 1.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: Title ISBN Author Price Fast Food Nation 234234234 Eric Schlosser 17.50 … 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: Fast Food Nation Eric Schlosser 17.50 …. 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. Fast Food Nation Eric Schlosser 17.50 Religion Explained Pascal Boyer 24.99

8 XML Example Show me the grades of all students. 15 16 14 15 16 15

9 XML Syntax Written in plain text (using notepad, e.g.) All elements must have a closing tag –Ex: Text text text –Forbidden: Line One Line Two Single element tags close themselves –Ex: –Not: text text or text text Should start with an XML document indicator –Ex: Has one root element, the first in the document All attribute values must be single or double quoted –Ex: –Not: Tags are case sensitive –Ex: Eric Schlosser –Not: Eric Schlosser 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) Manny Janni Reminder Don't forget me this weekend!

11 DTDs DTDs: Describe the allowable tags and attributes used in an XML document <!DOCTYPE TVSCHEDULE [ ]> 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: Eric Schlosser Food, muckraking 17.50

13 Viewing XML XML documents may be viewed natively in IE, Netscape –Ex: testdoc.xml What if we’d 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 Title Artist

17 XSL Elements xsl:template –Replaces all elements matching the Xpath expression (e.g. “/” matches all elements) with the specified content xsl:for-each –For each element matching the select Xpath expression, the element is replaced with the specified content xsl:value-of –Is replaced with the text value of the element specified by select’s Xpath expression Xpath: A pattern language for XML –/ -> matches all elements –a/b -> 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 doesn’t 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/http://www.w3schools.com/xml/ URL: http://www.w3schools.com/xsl/http://www.w3schools.com/xsl/


Download ppt "CSE 190: Internet E-Commerce Lecture 17: XML, XSL."

Similar presentations


Ads by Google