XML, Schemas - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

XML, Schemas

Description:

title Romeo & Juliet /title description A tragic romance /description /book ... published date= '1968'/ Root Element the top-level element ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 23
Provided by: chris520
Category:
Tags: xml | and | juliet | romeo | schemas

less

Transcript and Presenter's Notes

Title: XML, Schemas


1
XML, Schemas Transforms
  • An introduction to
  • XML
  • XML Schema checking XML
  • You have successfully learnt this if you can
  • Explain XML and XML schema
  • Explain the uses of XML and schemas
  • Why is XML increasingly popular?
  • Why are alternatives challenging XML?

2
What is XML?
  • A text-based language that mixes structural
    information (Mark-up) with data.
  • Definitions
  • XML eXtensible Mark-up Language
  • XSL eXtensible Stylesheet Language
  • XSLT eXtensible Stylesheet Language
    Transformation

3
Why is XML important?
  • Portable language for data exchange
  • Many parsers available
  • Transform to multiple display formats
  • Can define permitted document structure schemas
  • Automatically check that a document is valid
  • Use XSLT to transform XML
  • Produce different HTML (or other) format from XML

4
XML Example
Prolog
  • lt?xml version"1.0"?gt
  • ltcataloggt
  • ltbook id"bk101"gt
  • ltauthor name"Shakespeare"/gt
  • lttitlegtRomeo amp Julietlt/titlegt
  • ltdescriptiongtA tragic romancelt/descriptiongt
  • lt/bookgt
  • ltbook id"bk102"gt
  • ltauthor name"X. Emmel"/gt
  • lttitlegtXML characterslt/titlegt
  • ltdescriptiongtXML symbols lt!CDATAlt, gt
    gtlt/descriptiongt
  • lt/bookgt
  • lt/cataloggt

Start tag
End tag
Element
Entity
Root Element
Attribute
Empty element
escaped information
Character data
5
Notes on Key Features
  • Prolog
  • Information about the whole document e.g. XML
    version
  • Elements
  • Start and end tags
  • Tags without content end tags
  • Attributes
  • Entities amp lt
  • CDATA terminated by gt
  • What text can a CDATA section not contain?
  • XML is case-sensitive
  • In XML whitespace is significant

6
Definitions 1
  • URI Uniform Resource Identifier
  • URL Uniform Resource Locator
  • Character Data information content
  • Mark-up Text describes document structure
  • Tag text enclosed in ltgt - mark-up text
  • Entity reference special characters lt (lt)

7
Definitions 2
  • Element marked by start end tags or empty tag
  • ltbook type paperbackgtSquadron Airbornelt/bookgt
  • ltpublished date 1968/gt
  • Root Element the top-level element
  • Attribute name-value pair
  • giving information about an element
  • Namespace a unique name for a group of tags
  • to avoid clashes

8
Definitions 3
A language for describing syntax
  • DTD define valid document structure (in EBNF)
  • Document Type Definition
  • Schema define document structure (in XML)
  • Well-formed XML properly matching tags
  • Valid XML Well-formed satisfies a Schema
  • Parser
  • checks the syntax of a document
  • creates a representation of its contents.
  • DOM Document object model
  • tree-structured object-based representation of
    the document components

9
Advantages of XML Format
  • Structure defined by readable mark-up in text
  • XML formats are text-based
  • more readable, easier to document, and sometimes
    easier to debug.
  • simplify complex information exchange
  • Can create new structures for storing
    information.
  • Parsers widely available
  • XML is portable
  • Can check structure data types if schemas are
    used.
  • Uses Unicode supports international documents.
  • Can be transformed into different formats (XSLT)

10
XML Disadvantages
  • Longer than many binary formats
  • (use complex bit patterns to represent data)
  • E.g. use binary not characters to represent
    integers
  • More network bandwidth
  • More storage space
  • More processor time for compression
  • Slower to parse than specialist binary formats

11
XML Schema
  • Describe valid document structure
  • Written in XML
  • You only need to understand 1 language
  • The following is one possible document
  • The schema will describe the general structure of
    this type of document

12
Sample Document
  • lt?xml version"1.0" ?gt
  • lt?xml-stylesheet type"text/xsl"
    href"smalltransform.xsl"?gt
  • ltmarks xmlns"x-schemasmallmarksSchema.xml"gt
  • ltstudentgt
  • ltnamegtAblelt/namegt
  • ltassignment number"1" mark"11" /gt
  • ltassignment number"2" mark"12" /gt
  • lt/studentgt
  • ltstudentgt
  • ltnamegtBakerlt/namegt
  • ltassignment number"1" mark"21" /gt
  • ltassignment number"2" mark"22" /gt
  • lt/studentgt
  • lt/marksgt

Namespace used to group names to avoid clashes.
e.g. Game namespace Drinks namespace might
contain a Sprite name.
13
Example XML Schema
  • lt?xml version"1.0" ?gt
  • ltSchema xmlns"urnschemas-microsoft-comxml-data"
  • xmlnsdt"urnschemas-microsoft-comdatatypes"gt
  • ltElementType name'name' content'textOnly'/gt
  • ltAttributeType name"number" dttype'int'/gt
  • ltAttributeType name"mark" dttype'int' /gt
  • ltElementType name"assignment" content"empty"
    model'closed'gt
  • ltattribute type"number" required'yes'/gt
  • ltattribute type"mark" required'yes'/gt
  • lt/ElementTypegt

14
Example XML Schema 2
  • ltElementType name'marks' content'eltOnly'
    model'closed' order'seq'gt
  • ltelement type'student' maxOccurs''/gt
  • lt/ElementTypegt
  • ltElementType name'student' content'eltOnly'
    model'closed' order'seq'gt
  • ltelement type'name' minOccurs'1'
    maxOccurs'1'/gt
  • ltelement type'assignment' maxOccurs''/gt
  • lt/ElementTypegt
  • lt/Schemagt

15
XSLT
  • XSLT a programming language for transforming XML
    documents.
  • An XSLT style sheet is an XML document. This
    means that XSLT instructions are expressed as XML
    elements, and an XSLT style sheet can transform
    other XSLT style sheets, or even itself.
  • Uses a rule-based, pattern-matching approach
  • ltpattern / template1gt -gt ltresponse1gt
  • lttemplate2gt -gt ltresponse2gt

16
Using XML Web Services
  • What is a Web Service?
  • Application providing a Services over the Web!
  • Enable any platform to intercommunicate
  • Based on a core set of standards
  • HTTP to transfer requests responses
  • SOAP to encode method, parameters results
  • Simple Object Access Protocol
  • WSDL to describe the capabilities of a Web
    service.
  • Web Services Description Language
  • SOAP WSDL use XML

17
Directory Services
  • UDDI
  • Universal Description, Discovery, Integration
  • A standard for a directory for Web services
  • Describing and locating a service
  • WSDL
  • Detailed technical information
  • Service group of methods
  • Method parameters, results

18
Web Services
UDDI Directory
Proxy object automatically created from WDSL
calls a method
19
Using XML AJAX 1
  • Standard Web
  • Reload page for each user request
  • High latency
  • AJAX
  • Control page with JavaScript
  • User can interact with the page
  • E.g. drag and drop
  • Request information from server
  • Update part of page
  • Can modify HTML using document object model

20
Using XML AJAX 2
  • Standards-based UI with XHTML and CSS
  • Dynamic display using DOM to modify page
  • Document Object Model
  • Data interchange and manipulation
  • Using XML and XSLT
  • Asynchronous data retrieval
  • Using XMLHttpRequest JavaScript method
  • JavaScript binding everything together.

21
AJAX in Action
Browser
Browser
User Interface
HTML CSS
JavaScript Call
User Interface
AJAX engine
HTML CSS
HTTP request
HTML CSS
HTTP request
Server
Server
Database
Database
Standard Approach
AJAX Approach
22
Summary
  • XML is used for creating data representation
  • Tags, elements, attributes, rules for their use
  • XML schema defines valid representation
  • What elements, data types, nesting, counts
  • Web Services
  • Similar to RMI but using HTTP, XML
  • Directories, remote method calls
  • AJAX
  • Client-side programming of UI, fewer page loads
  • Program communicates to server using XML
  • But other formats becoming popular

23
Features
  • Built-in data types
  • Boolean, number, string, node-set, external
    objects.
  • Pattern definition
  • ltxsltemplategt with XPath language for
    expressions
  • Programming flow-control
  • ltxslifgt, ltxslfor-eachgt, ltxslchoosegt, and so
    on.
  • Operations
  • ltxslapply-templatesgt, ltxslsortgt, ltxsloutputgt,
  • ltxslimportgt (load new XSLT files)

24
XSLT Example 1
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform"
  • xmlnsb"x-schemasmallmarksSchema.xml"
    version"1.0"gt
  • ltxsloutput method"html"/gt
  • ltxsltemplate match"/" gt
  • ltHTMLgtltHEADgtltTITLEgtMarkslt/TITLEgtlt/HEADgt
  • ltBODYgt
  • lth1gtDatalt/h1gt
  • lttable border"1"gt
  • lttrgtltthgtStudent lt/thgt
  • ltxslfor-each select"//bdata/bstudent1/
    bassignment"gt
  • ltthgtAssignmentltxslvalue-of
    select"_at_number"/gtlt/thgt
  • lt/xslfor-eachgt
  • lt/trgt

25
XSLT Example 2
  • ltxslfor-each select"//bdata/bstudent"gt
  • lttr gt
  • ltth style"text-alignleft"gt
  • ltxslvalue-of select"bname"/gtlt/thgt
  • ltxslfor-each select"bassignment"gt
  • lttdgt
  • ltxslvalue-of select"format-number(_at_m
    arks,',')"/gt
  • lt/tdgt
  • lt/xslfor-eachgt
  • lt/trgt
  • lt/xslfor-eachgt
  • lt/tablegt
  • lt/BODYgtlt/HTMLgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

26
Notes on the example
  • Namespace
  • A local name b labels a group of elements
  • The local name is put in front of names
  • (e.g. bname)
  • Patterns
  • //bdata/bstudent1/bassignment
  • Identifies the assignment element in the first
    (1) student element within the data element.

27
Processing
  • HTML text is copied to the result template.
  • ltxslfor-eachgt picks out each assignment element
    and the body labels a column for each assignment
    number.
  • _at_ is used to identify names referring to
    attributes

28
The output
Data
  • How can we introduce a space before the
    assignment number in the column header?

29
Rule-based Programs Rules
  • A template rule is an instruction to transform a
    specified source element in a particular way.
  • To transform a set of data, you provide a
    complete set of rules.
  • However, each rule is independent of every other.
  • The order in which rules are declared is not
    significant.

30
Matching
  • Matching starts with a pattern for the root node.
  • If no pattern matches a node (including the
    root), a default is used
  • ltxsltemplate match"/" gt
  • ltxslapply-templates /gt
  • lt/xsltemplategt
  • / - matches the root
  • - matches anything matches no other pattern
  • - means or
  • apply-templates apply all templates which match
    against children of the current element.

31
Template Rules
  • Each ltxsltemplategt element or template rule
    consists of two components a match pattern, and
    the template itself.
  • ltxsltemplate match"region"gt
  • template
  • lt/xsltemplategt

32
Match Patterns
  • The match pattern consists of a match attribute
    and an XML Path Language (XPath) expression,
    which specifies the sections of the input to be
    processed by the template rule.
  • e.g. ltxsltemplate match"/" gt

33
Templates
  • Each time the template locates something in the
    input that matches the match pattern, it outputs
    content.
  • That is, the template rule instantiates the
    template for each match.

34
Special and Literal Elements
  • Elements in the XSLT namespace, such as ltxslifgt
    are interpreted as XSLT code to be processed.
  • Other elements, such as ltHTMLgt, ltBODYgt, ltPgt, and
    ltH2gt, are copied to the output with minimal
    processing.
  • HTML elements in the XSLT file must be closed Be
    sure to include a closing slash, such as ltBR/gt or
    ltPgt . lt/Pgt.

35
For Loops - ltxslfor-eachgt
  • ltxslfor-eachgt sets the context for iteration.
  • The XSLT transformation instructions within the
    for-each loop are applied to each of the nodes
    selected by the select attribute of
    ltxslfor-eachgt.
  • Each source element selected by ltxslfor-eachgt
    becomes a new context in which operations in the
    ltxslfor-eachgt body are performed.

36
Example
  • ltxslfor-each select"weather/forecast"gt
  • lttrgt
  • lttdgtltxslvalue-of select"_at_day"/gtlt/tdgt
  • lttdgtltxslvalue-of select"temperature"/gtlt/tdgt
  • lttdgtltxslvalue-of select"humidity"/gtlt/tdgt
  • lt/trgt
  • lt/xslfor-eachgt

37
Conditional Patterns
  • If the expression in an ltxslifgt, ltxslwhengt, or
    ltxslotherwisegt element evaluates to true, the
    template body inside that element is
    instantiated.
  • ltxslifgt - decide whether to perform an action
  • ltxslchoosegt - select one from many actions.
  • The condition can be a comparison or an XPath
    expression.
  • True is any non-null expression.

38
Example of ltxslifgt
  • ltxslfor-each select"weather/forecast"gtlttrgt
  • lttdgtltxslvalue-of select"_at_day"/gtlt/tdgt
  • lttdgtltxslattribute name"bgcolor"gtlightyellow
    lt/xslattributegt
  • ltxslif test"temperature gt 80"gt
  • ltxslattribute name"bgcolor"gtlightgre
    enlt/xslattributegt
  • lt/xslifgt
  • ltxslvalue-of select"temperature"/gt
  • lt/tdgt
  • lttdgtltxslvalue-of select"humidity"/gtlt/tdgt
  • lt/trgtlt/xslfor-eachgt
  • Note the ltxslattributegt element is used to set
    the element of the enclosed element (i.e. lttdgt)

39
Example of ltxslchoosegt
  • ltxslfor-each select"weather/forecast"gtlttrgt
  • lttdgtltxslvalue-of select"_at_day"/gtlt/tdgtlttdgt
  • ltxslchoosegt
  • ltxslwhen test"temperature gt 80"gt
  • ltxslattribute name"bgcolor"gtlightye
    llowlt/xslattributegt
  • lt/xslwhengt
  • ltxslwhen test"temperature lt 40"gt
  • ltxslattribute name"bgcolor"gtredlt/xs
    lattributegt
  • lt/xslwhengt
  • ltxslotherwisegt
  • ltxslattribute name"bgcolor"gtbluelt/x
    slattributegt
  • lt/xslotherwisegt
  • lt/xslchoosegt
  • ltxslvalue-of select"temperature"/gtlt/tdgt
  • lt/trgtlt/xslfor-eachgt
Write a Comment
User Comments (0)
About PowerShow.com