XML, DTD, XML Schema, and XSLT - PowerPoint PPT Presentation

About This Presentation
Title:

XML, DTD, XML Schema, and XSLT

Description:

Pentax.xsd. From Costello. 14 ?xml ... xmlns:pentax='http://www.pentax.com' ... namespace='http://www.pentax.com' schemaLocation='Pentax.xsd' ... – PowerPoint PPT presentation

Number of Views:938
Avg rating:3.0/5.0
Slides: 61
Provided by: jiang79
Category:
Tags: dtd | xml | xslt | pentax | schema

less

Transcript and Presenter's Notes

Title: XML, DTD, XML Schema, and XSLT


1
XML, DTD, XML Schema, and XSLT
  • Jianguo Lu
  • University of Windsor

2
Where we are
  • XML
  • DTD
  • XML Schema
  • XML Namespace
  • XPath
  • DOM Tree
  • XSLT

3
Name Conflict
lttablegt lttrgt lttdgtAppleslt/tdgt lttdgtBananaslt/tdgt
lt/trgtlt/tablegt
lttablegt ltnamegtAfrican Coffee Table lt/namegt ltwid
thgt80lt/widthgt ltlengthgt120lt/lengthgtlt/tablegt
  • Solution add prefix to the tag names

lthtablegt lthtrgt lthtdgtAppleslt/htdgt lthtdgtBana
naslt/htdgt lt/htrgtlt/htablegt
ltftablegt ltfnamegtAfrican Coffee Table
lt/fnamegt ltfwidthgt80lt/fwidthgt ltflengthgt120lt/f
lengthgtlt/ftablegt
4
Name spaces
HTML name space
Furniture name space
table
table
td
width
html
name
tr
price
body
th
length
height
5
XML namespace
  • An XML document may use more than one schema
  • Since each structuring document was developed
    independently, name clashes may appear
  • The solution is to use a different prefix for
    each schema
  • prefixname
  • ltprodproduct xmlnsprodhttp//example.org/prodgt
  • ltprodnumbergt 557 lt/prodnumbergt
  • ltprodsize systemUS-DRESSgt 10
    lt/prodsizegt
  • lt/prodproductgt

6
Namespace names
  • Namespace names are URIs
  • Many namespace names are in the form of HTTP URI.
  • The purpose of a name space is not to point to a
    location where a resource resides.
  • It is intended to provide a unique name that can
    be associated with a particular organization.
  • The URI MAY point to a schema.
  • ltprodproduct xmlnsprodhttp//example.org/prodgt
  • ltprodnumbergt 557 lt/prodnumbergt
  • ltprodsize systemUS-DRESSgt 10
    lt/prodsizegt
  • lt/prodproductgt

7
Namespace declaration
  • A namespace is declared using an attribute starts
    with xmlns.
  • You can declare multiple namespaces in one
    instance.
  • ltordorder xmlnsordhttp//example.org/ord
  • xmlnsprodhttp//example.org/prod
    gt
  • ltordnumbergt 123ABC123lt/ordnumbergt
  • ltprodproductgt
  • ltprodnumbergt 557 lt/prodnumbergt
  • ltprodsize systemUS-DRESSgt 10
    lt/prodsizegt
  • lt/prodproductgt
  • lt/ordordergt

8
Default namespace declaration
  • Default namespace maps unprefixed element type
    name to a namespace.
  • ltorder xmlnshttp//example.org/ord
  • xmlnsprodhttp//example.org/prod gt
  • ltnumbergt 123ABC123 lt/numbergt
  • ltprodproductgt
  • ltprodnumbergt 557 lt/prodnumbergt
  • ltprodsize systemUS-DRESSgt 10
    lt/prodsizegt
  • lt/prodproductgt
  • lt/ordergt

9
Scope of namespace declaration
  • Namespace declaration can appear in any start
    tag.
  • The scope is in the element where it is declared.
  • ltorder xmlnshttp//example.org/ordgt
  • ltnumbergt 123ABC123 lt/numbergt
  • ltprodproduct xmlnsprodhttp//example.org/pro
    dgt
  • ltprodnumbergt 557 lt/prodnumbergt
  • ltprodsize systemUS-DRESSgt 10
    lt/prodsizegt
  • lt/prodproductgt
  • lt/ordergt

10
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.book
s.org" xmlns"http//www.bo
oks.org"
elementFormDefault"qualified"gt ltxsdelement
name"BookStore"gt ltxsdcomplexTypegt
ltxsdsequencegt ltxsdelement
ref"Book" minOccurs"1" maxOccurs"unbounded"/gt
lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdelementgt
ltxsdelement name"Book"gt
ltxsdcomplexTypegt ltxsdsequencegt
ltxsdelement ref"Title" minOccurs"1"
maxOccurs"1"/gt ltxsdelement
ref"Author" minOccurs"1" maxOccurs"1"/gt
ltxsdelement ref"Date" minOccurs"1"
maxOccurs"1"/gt ltxsdelement
ref"ISBN" minOccurs"1" maxOccurs"1"/gt
ltxsdelement ref"Publisher" minOccurs"1"
maxOccurs"1"/gt lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdelementgt
ltxsdelement name"Title" type"xsdstring"/gt
ltxsdelement name"Author" type"xsdstring"/gt
ltxsdelement name"Date" type"xsdstring"/gt
ltxsdelement name"ISBN" type"xsdstring"/gt
ltxsdelement name"Publisher" type"xsdstring"/gt
lt/xsdschemagt
The elements and datatypes that are used to
construct schemas - schema - element -
complexType - sequence - string come from the
http///XMLSchema namespace
Indicates that the elements defined by this
schema - BookStore - Book - Title -
Author - Date - ISBN - Publisher are to go
in the http//www.books.org namespace
From Costello
11
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.books
.org" xmlns"http//www.book
s.org" elementFormDefault"q
ualified"gt ltxsdelement name"BookStore"gt
ltxsdcomplexTypegt ltxsdsequencegt
ltxsdelement ref"Book"
minOccurs"1" maxOccurs"unbounded"/gt
lt/xsdsequencegt lt/xsdcomplexTypegt
lt/xsdelementgt ltxsdelement name"Book"gt
ltxsdcomplexTypegt ltxsdsequencegt
ltxsdelement ref"Title"
minOccurs"1" maxOccurs"1"/gt
ltxsdelement ref"Author" minOccurs"1"
maxOccurs"1"/gt ltxsdelement
ref"Date" minOccurs"1" maxOccurs"1"/gt
ltxsdelement ref"ISBN" minOccurs"1"
maxOccurs"1"/gt ltxsdelement
ref"Publisher" minOccurs"1" maxOccurs"1"/gt
lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdelementgt
ltxsdelement name"Title" type"xsdstring"/gt
ltxsdelement name"Author" type"xsdstring"/gt
ltxsdelement name"Date" type"xsdstring"/gt
ltxsdelement name"ISBN" type"xsdstring"/gt
ltxsdelement name"Publisher" type"xsdstring"/gt
lt/xsdschemagt
The default namespace is http//www.books.org whic
h is the targetNamespace!
This is referencing a Book element
declaration. The Book in what namespace? Since
there is no namespace qualifier it is referencing
the Book element in the default namespace, which
is the targetNamespace! Thus, this is a
reference to the Book element declaration in this
schema.
From Costello
12
Import in XML Schema
  • Now with the understanding of namespace, we can
    introduce some more advanced features in XML
    Schema.
  • The import element allows you to access elements
    and types in a different namespace.

Namespace B
Namespace A
B.xsd
A.xsd
ltxsdschema gt ltxsdimport namespace"A"
schemaLocation"A.xsd"/gt
ltxsdimport namespace"B"
schemaLocation"B.xsd"/gt lt/xsdschemagt
C.xsd
13
Example
Pentax.xsd
Nikon.xsd
Olympus.xsd
Camera.xsd
From Costello
14
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.nikon
.com" xmlns"http//www.niko
n.com" elementFormDefault"q
ualified"gt ltxsdcomplexType
name"body_type"gt ltxsdsequencegt
ltxsdelement name"description"
type"xsdstring"/gt lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdschemagt
Nikon.xsd
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.olympus.com"
xmlns"http//www.olympus.com"
elementFormDefault"qualified"gt
ltxsdcomplexType name"lens_type"gt
ltxsdsequencegt ltxsdelement
name"zoom" type"xsdstring"/gt
ltxsdelement name"f-stop" type"xsdstring"/gt
lt/xsdsequencegt lt/xsdcomplexTypegt lt/xsd
schemagt
Olympus.xsd
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.pentax.com"
xmlns"http//www.pentax.com"
elementFormDefault"qualified"gt
ltxsdcomplexType name"manual_adapter_type"gt
ltxsdsequencegt ltxsdelement
name"speed" type"xsdstring"/gt
lt/xsdsequencegt lt/xsdcomplexTypegt lt/xsdschem
agt
Pentax.xsd
From Costello
15
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.camer
a.org" xmlnsnikon"http//w
ww.nikon.com"
xmlnsolympus"http//www.olympus.com"
xmlnspentax"http//www.pentax.com"
elementFormDefault"qualified"
gt ltxsdimport namespace"http//www.nikon.com"
schemaLocation"Nikon.xsd"/gt
ltxsdimport namespace"http//www.olympus.com"
schemaLocation"Olympus.xsd"/gt
ltxsdimport namespace"http//www.pentax.com"
schemaLocation"Pentax.xsd"/gt
ltxsdelement name"camera"gt
ltxsdcomplexTypegt ltxsdsequencegt
ltxsdelement name"body"
type"nikonbody_type"/gt
ltxsdelement name"lens" type"olympuslens_type"/
gt ltxsdelement
name"manual_adapter
type"pentaxmanual_adapt
er_type"/gt lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdelementgt ltxsdschemagt
Here I am using the body_type that is defined in
the Nikon namespace
Camera.xsd
From Costello
16
lt?xml version"1.0"?gt ltccamera
xmlnsc"http//www.camera.org"
xmlnsnikon"http//www.nikon.com"
xmlnsolympus"http//www.olympus.com"
xmlnspentaxhttp//www.pentax.com
ltcbodygt ltnikondescriptiongtErgonomically
designed casing for easy handling
lt/nikondescriptiongt lt/cbodygt ltclensgt
ltolympuszoomgt300mmlt/olympuszoomgt
ltolympusf-stopgt1.2lt/olympusf-stopgt
lt/clensgt ltcmanual_adaptergt
ltpentaxspeedgt1/10,000 sec to 100
seclt/pentaxspeedgt lt/cmanual_adaptergt lt/ccam
eragt
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.olympus.com"
xmlns"http//www.olympus.com"
elementFormDefault"qualified"gt
ltxsdcomplexType name"lens_type"gt
ltxsdsequencegt ltxsdelement
name"zoom" type"xsdstring"/gt
ltxsdelement name"f-stop" type"xsdstring"/gt
lt/xsdsequencegt lt/xsdcomplexTypegt lt/xsd
schemagt
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.nikon
.com" xmlns"http//www.niko
n.com" elementFormDefault"q
ualified"gt ltxsdcomplexType
name"body_type"gt ltxsdsequencegt
ltxsdelement name"description"
type"xsdstring"/gt lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdschemagt
Camera.xml
From Costello
17
Include
  • The include element allows you to access
    components in other schemas
  • All the schemas you include must have the same
    namespace as your schema (i.e., the schema that
    is doing the include)
  • The net effect of include is as though you had
    typed all the definitions directly into the
    containing schema

LibraryEmployee.xsd
LibraryBook.xsd
ltxsdschema gt ltxsdinclude
schemaLocation"LibraryBook.xsd"/gt
ltxsdinclude schemaLocation"LibraryEmployee.xsd"/
gt lt/xsdschemagt
Library.xsd
From Costello
18
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
targetNamespace"http//www.libra
ry.org" xmlns"http//www.li
brary.org"
elementFormDefault"qualified"gt ltxsdinclude
schemaLocation"LibraryBook.xsd"/gt
ltxsdinclude schemaLocation"LibraryEmployee.xsd"/
gt ltxsdelement name"Library"gt
ltxsdcomplexTypegt ltxsdsequencegt
ltxsdelement name"Books"gt
ltxsdcomplexTypegt
ltxsdsequencegt
ltxsdelement ref"Book" maxOccurs"unbounded"/gt
lt/xsdsequencegt
lt/xsdcomplexTypegt
lt/xsdelementgt ltxsdelement
name"Employees"gt
ltxsdcomplexTypegt
ltxsdsequencegt
ltxsdelement ref"Employee" maxOccurs"unbounded"/
gt lt/xsdsequencegt
lt/xsdcomplexTypegt
lt/xsdelementgt lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdelementgt lt/xsdschem
agt
These are referencing element declarations in
other schemas.
Library.xsd
From Costello
19
XML Path
  • XML
  • DTD
  • XML Schema
  • XML Namespace
  • XPath
  • DOM Tree
  • XSLT

20
XPath
  • Language for addressing parts of an XML document.
  • It operates on the tree data model of XML
  • XPath is a syntax for defining parts of an XML
    document
  • XPath uses paths to define XML elements
  • It has a non-XML syntax
  • XPath defines a library of standard functions
  • Such as arithmetic expressions.
  • XPath is a major element in XSLT and XML query
    languages
  • XPath is a W3C Standard

21
What is XPath
  • Like traditional file paths
  • XPath uses path expressions to identify nodes in
    an XML document. These path expressions look very
    much like the expressions you see when you work
    with a computer file system
  • public_html/569/xml.ppt
  • Books/book/author/name/FirstName
  • Absolute path
  • /library/author/book
  • Relative path
  • author/book

22
XML path example
  • /library
  • /library/author
  • //author
  • /library/_at_location
  • //book_at_titleArtificial Intelligence
  • ltlibrary location"Bremen"gt
  • ltauthor name"Henry Wise"gt
  • ltbook title"Artificial Intelligence"/gt
  • ltbook title"Modern Web Services"/gt
  • ltbook title"Theory of Computation"/gt
  • lt/authorgt
  • ltauthor name"William Smart"gt
  • ltbook title"Artificial Intelligence"/gt
  • lt/authorgt
  • ltauthor name"Cynthia Singleton"gt
  • ltbook title"The Semantic Web"/gt
  • ltbook title"Browser Technology Revised"/gt
  • lt/authorgt
  • lt/librarygt

23
XML Path Example
  • Address all author elements
  • /library/author
  • Addresses all author elements that are children
    of the library element node, which resides
    immediately below the root
  • /t1/.../tn, where each ti1 is a child node of
    ti, is a path through the tree representation
  • Address all author elements
  • //author
  • Here // says that we should consider all elements
    in the document and check whether they are of
    type author
  • This path expression addresses all author
    elements anywhere in the document

24
XPath example
  • Select the location attribute nodes within
    library element nodes
  • /library/_at_location
  • The symbol _at_ is used to denote attribute nodes
  • Select all title attribute nodes within book
    elements anywhere in the document, which have the
    value Artificial Intelligence
  • //book/_at_title"Artificial Intelligence
  • Select all books with title Artificial
    Intelligence
  • /library/author/book_at_title"Artificial
    Intelligence"
  • Test within square brackets a filter expression
  • It restricts the set of addressed nodes.
  • Difference with previous query.
  • This query addresses book elements, the title of
    which satisfies a certain condition.
  • Previous query collects title attribute nodes of
    book elements

25
(No Transcript)
26
XPath syntax
  • A path expression consists of a series of steps,
    separated by slashes
  • A step consists of
  • An axis specifier,
  • A node test, and
  • An optional predicate
  • An axis specifier determines the tree
    relationship between the nodes to be addressed
    and the context node
  • E.g. parent, ancestor, child (the default),
    sibling, attribute node
  • // is such an axis specifier descendant or self
  • childbook select all book elements that are
    children of current node
  • A node test specifies which nodes to address
  • The most common node tests are element names
  • /library/author
  • E.g., addresses all element nodes
  • /library/
  • comment() selects all comment nodes
  • /library/commnets()

27
XPath syntax
  • Predicates (or filter expressions) are optional
    and are used to refine the set of addressed nodes
  • E.g., the expression 1 selects the first node
  • position()last() selects the last node
  • position() mod 2 0 selects the even nodes
  • XPath has a more complicated full syntax.
  • We have only presented the abbreviated syntax

28
More examples
  • Address the first author element node in the XML
    document
  • //author1
  • Address the last book element within the first
    author element node in the document
  • //author1/booklast()
  • Address all book element nodes without a title
    attribute
  • //booknot _at_title

29
Where we are
  • XML
  • DTD
  • XML Schema
  • XML Namespace
  • XPath
  • DOM Tree
  • XSLT

30
How to process XML
  • XML does not DO anything
  • Process XML using general purpose languages
  • Java, Perl, C
  • DOM is the basis
  • Process XML using special purpose languages
  • translate the stock XML file to an HTML table.
  • Transform the XML XSLT
  • tell me the stocks that are higher that 100.
  • Query XML XQuery

31
DOM (Document Object Model)
  • What DOM is application programming interface
    (API) for processing XML documents
  • http//www.w3c.org/DOM/
  • Why
  • unique interface.
  • Platform and language independence.
  • How It defines the logical structure of
    documents and the way to access and manipulate it
  • With the Document Object Model, one can
  • Create an object tree
  • Navigate its structure
  • Access, add, modify, or delete elements etc

32
XML tree hierarchy
  • XML can be described by a tree hierarchy

Document
Unit
Document
Sub-unit
Parent
Unit
Child
Sub-unit
Sibling
33
DOM tree model
  • Generic tree model
  • Node
  • Type, name, value
  • Attributes
  • Parent node
  • Previous, next sibling nodes
  • First, last child nodes
  • Many other entities extends node
  • Document
  • Element
  • Attribute
  • ... ...

34
DOM class hierarchy
DocumentFragment
Document
Text
CDATASection
CharacterData
Comment
Attr
Node
Element
DocumentType
NodeList
Notation
NamedNodeMap
Entity
DocumentType
EntityReference
ProcessingInstruction
35
JavaDoc of DOM API
http//xml.apache.org/xerces-j/apiDocs/index.html
36
Remarks on javadoc
  • javadoc is a command included in JDK
  • It is a useful tool generate HTML description for
    your programs, so that you can use a browser to
    look at the description of the classes
  • JavaDoc describes classes, their relationships,
    methods, attributes, and comments.
  • When you write java programs, the JavaDoc is the
    first place that you should look at
  • For core java, there is JavaDoc to describe every
    class in the language
  • To know how to use DOM, look at the javaDoc of
    org.w3c.dom package.
  • If you are a serious java programmer
  • you should have the core jdk javaDoc ready on
    your hard disk
  • You should generate the javaDoc for other people
    to look at.
  • To run javadoc, type
  • Dgtjavadoc .java
  • This is to generate JavaDoc for all the classes
    under current directory.

37
Methods in Node interface
  • Three categories of methods
  • Node characteristics
  • name, type, value
  • Contextual location and access to relatives
  • parents, siblings, children, ancestors,
    descendants
  • Node modification
  • Edit, delete, re-arrange child nodes

38
XML parser and DOM
DOM XML parser
Your XML application
DOM API
DOM Tree
  • When you parse an XML document with a DOM parser,
    you get back a tree structure that contains all
    of the elements of your document
  • DOM also provides a variety of functions you can
    use to examine the contents and structure of the
    document.

39
DOM tree and DOM classes
ltstocksgt
ltstock Exchangenyse gt
ltstock exchangenasdaqgt
ltnamegt
ltpricegt
ltnamegt
ltpricegt
ltsymbolgt
IBM
105
amzn
15.45
Amazon inc
Element
child
40
Use Java to process XML
  • Tasks
  • How to construct the DOM tree from an XML text
    file?
  • How to get the list of stock elements?
  • How to get the attribute value of the second
    stock element?
  • Construct the Document object
  • Need to use an XML parser (XML4J)
  • Remember to import the necessary packages
  • The benefits of DOM the following lines are the
    only difference if you use another DOM XML parser.

41
Get the first stock element
lt?xml version"1.0" ?gt ltstocksgt ltstock
exchange"nasdaq"gt  ltnamegtamazon corplt/namegt
  ltsymbolgtamznlt/symbolgt  
ltpricegt16lt/pricegt   lt/stockgt ltstock
exchange"nyse"gt  ltnamegtIBM inclt/namegt  
ltpricegt102lt/pricegt   lt/stockgt  lt/stocksgt
42
Navigate to the next sibling of the first stock
element
lt?xml version"1.0" ?gt ltstocksgt ltstock
exchange"nasdaq"gt  ltnamegtamazon corplt/namegt
  ltsymbolgtamznlt/symbolgt  
ltpricegt16lt/pricegt   lt/stockgt ltstock
exchange"nyse"gt  ltnamegtIBM inclt/namegt  
ltpricegt102lt/pricegt   lt/stockgt  lt/stocksgt
43
Be aware the Text object in two elements
lt?xml version"1.0" ?gt ltstocksgt ltstock
exchange"nasdaq"gt  ltnamegtamazon corplt/namegt
  ltsymbolgtamznlt/symbolgt  
ltpricegt16lt/pricegt   lt/stockgt ltstock
exchange"nyse"gt  ltnamegtIBM inclt/namegt  
ltpricegt102lt/pricegt   lt/stockgt  lt/stocksgt
Question How many children does the stocks node
have?
text
text
text
text
text
text
text
text
text
text
44
Remarks on XML parsers
  • There are several different ways to categorise
    parsers
  • Validating versus non-validating parsers
  • It takes a significant amount of effort for an
    XML parser to process a DTD and make sure that
    every element in an XML document follows the
    rules of the DTD
  • If only want to find tags and extract information
    - use non-validating
  • Validating or non-validating can be turned on or
    off in parsers.
  • Parsers that support the Document Object Model
    (DOM)
  • Parsers that support the Simple API for XML (SAX)
  • Parsers written in a particular language (Java,
    C, Perl, etc.).

45
Where we are
  • XML
  • DTD
  • XML Schema
  • XML Namespace
  • XPath
  • DOM Tree
  • XSLT

46
History
XSL
(low-precision graphics, e.g.,HTML, text, XML)
XQuery
(high-precision graphics, e.g., PDF)
XLink/ XPointer
XSL
XSLT
XMLSchemas
XPath
47
XSLT(XML Stylesheet Language Transformation)
  • XSLT Version 1.0 is a W3C Recommendation, 1999
  • http//www.w3.org/Style/XSL/
  • XSLT is used to transform XML to other formats

48
XSLT basics
  • XSLT is an XML document itself
  • It is a tree transformation language
  • It is a rule-based declarative language
  • XSLT program consists of a sequence of rules.
  • It is a functional programming language.

49
XSLT Example transform to another XML
lt?xml version"1.0" ?gt ltstocksgt ltstock
exchange"nasdaq"gt  ltnamegtamazon corp
lt/namegt   ltsymbolgtamznlt/symbolgt  
ltpricegt16lt/pricegt   lt/stockgt ltstock
exchange"nyse"gt  ltnamegtIBM inclt/namegt  
ltpricegt102lt/pricegt   lt/stockgt  lt/stocksgt
lt?xml version"1.0gt ltcompaniesgt ltcompanygt
ltvaluegt24 CAD lt/valuegt ltnamegtamazon
corplt/namegt lt/companygt ltcompanygt
ltvaluegt153 CAD lt/valuegt ltnamegtIBM inclt/namegt
lt/companygt lt/companiesgt
?
stock.xml
output
  • Rename the element names
  • Remove the attribute and the symbol element
  • Change the order between name and price.
  • Change the US dollar to CAD.

50
A most simple XSLT
51
Template definition and call
52
If statement
53
XSLT rule ltxsltemplategt
ltxsltemplate match"stock"gt ltcompanygt ltvaluegt
ltxslvalue-of select"price1.5"/gt
CAD lt/valuegt ltnamegt ltxslvalue-of
select"name"/gt lt/namegt ltcompanygt lt/xsltemplategt
xslt template for ltstockgt
lt?xml version"1.0" ?gt ltstocksgt ltstock
exchange"nasdaq"gt  ltnamegtamazon corp
lt/namegt   ltsymbolgtamznlt/symbolgt  
ltpricegt16lt/pricegt   lt/stockgt ltstock
exchange"nyse"gt  ltnamegtIBM inclt/namegt  
ltpricegt102lt/pricegt   lt/stockgt  lt/stocksgt
ltcompanygt ltvaluegt get the value of ltpricegt
1.5, i.e. 24 CAD lt/valuegt ltnamegt get the
value of ltnamegt, i.e amazon lt/namegt lt/companygt
Part of the output
stock.xml
54
XSLT process model
apply template 1 to ltstocksgt
ltxsltemplate match"/"gt ltcompaniesgt ltxslapply-
templates select"stocks/stock/gt lt/companiesgt lt/x
sltemplategt ltxsltemplate match"stock"gt ltcompa
nygt ltvaluegt ltxslvalue-of select"price1.5"/gt
CADlt/valuegt ltnamegt ltxslvalue-of
select"name"/gt lt/namegtlt/companygt lt/xsltemplategt
ltcompaniesgt apply template 2 to ltstockgt
1 apply template 2 to ltstockgt
2 lt/companiesgt
ltcompanygt ltvaluegt get the value of
ltpricegt1.5,i.e. 24 CAD lt/valuegt ltnamegt get
the value of ltnamegt, i.e amazon lt/namegtlt/companygt
ltcompanygt valuegt get the value of
ltpricegt1.5,i.e., 153 CAD lt/valuegt ltnamegt
get the value of ltnamegt, i.e., IBM lt/namegtlt/compan
ygt
toXML.xsl
xslt output
55
Transforming XML to HTML
toHTML.xsl
56
Running XSLT from the client side
  • Browser gets the XMLXSLT, and interprets them
    inside the browser.
  • How to specify the XSL associated with the XML
    file?
  • lt?xml-stylesheet type"text/xsl"
    href"stock.xsl"?gt
  • Advantages
  • Easy to develop and deploy.
  • Disadvantages
  • Not every browser supports XMLXSL
  • Browsers do not support all XSLT features
  • Not secure you only want to show part of the XML
    data
  • Not efficient.

57
Run XSLT from the server side
  • XSL processor transforms the XML and XSLT to
    HTML, and the web server send the HTML to the
    browser.
  • Popular tool xalan
  • java -classpath xalan/bin/xalan.jar
    org.apache.xalan.xslt.Process -in stock.xml -xsl
    stock.xsl -out stock.html

XSL Processor
Web server
58
Why XML is useful
  • Data exchange
  • Data integration

59
Why XML is useful(cont.)
  • Present to different devices

60
XML references
  • For XML and related specifications www.w3c.org
  • For Java support for XML, like XML parser, XSLT
    processor www.apache.org
  • For xml technologies www.xml.com
  • XML integrated development environment
    www.xmlspy.com
Write a Comment
User Comments (0)
About PowerShow.com