xml data processing and transformation ดร. มารุต บูรณรัช...
TRANSCRIPT
XML Data Processing and Transformationดร.มาร�ต บูรณร�ช [email protected]
269618: หั�วข้�อพิ�เศษด�านเทคโนโลยี�สารสนเทศข้��นสง - เทคโนโลยี�เว!บูเช�งความหัมายี (Special Topics in Advanced Information Technology – Semantic Web Technology)
ภาคว�ชาว�ทยีาการคอมพิ�วเตอร$และเทคโนโลยี�สารสนเทศคณะว�ทยีาศาสตร$ มหัาว�ทยีาล�ยีนเรศวรภาคการศ'กษาท�( 2 ปี*การศ'กษา 2557
Outline
XML Data Processing APIs SAX (Simple API for XML) DOM (Document Object Model)
XSL Tranformation (XSLT) Language
2
XML Data Processing APIs: DOM and SAX
Slides adapted from Pekka Kilpeläinen, Universiry of Kuopio, Finland- http://www.cs.uku.fi/~kilpelai/RDK02/
XML Document Parsers
Every XML application contains a parser XML editors, XML browsers XML data transformation systems
XML parsers are becoming standard tools of application development frameworks JDK v. 1.4 contains JAXP, with its default parser
(Apache Crimson) JAXP = Java API for XML Processing
4
Tasks of an XML Parser
Document instance decomposition Elements, Attributes, Text, Processing instructions,
Entities, ... Verification
Well-formedness checking syntactical correctness of XML markup
Validation (against a DTD or Schema) Access to contents of the DTD
Not always supported
5
XML Processing APIs
There are two major types of XML APIs Event-based API
The application implements handlers to deal with the various events
Simple API for XML (SAX)
Tree-based API Compiles an XML document into an internal tree structure
and allows an application to navigate that tree Document Object Model (DOM)
6
SAX – an Event-based API
Event-based API
Application implements a set of callback methods for handling parse events parser notifies the application by method calls method parameters qualify events further
element type name names and values of attributes values of content strings, …
8
Event-based API (2)
ApplicationApplication
SGML/XML ParserSGML/XML Parser
CommandCommandline callline call ESISESIS
StreamStream
<A<A </A></A>Hi!Hi!
(A(A
i="1"i="1" >>
Ai CDATA 1Ai CDATA 1
-Hi!-Hi!)A)A
ESIS = Element Structure Information Set 9
An event call-back application
Application Main Application Main RoutineRoutine
startDocument()startDocument()
startElement()startElement()
characters()characters()
Parse()Parse()
Callback
Callback
Routines
Routines
endElement()endElement() <A i="1"><A i="1"> </A></A>Hi!Hi!
"A",[i="1"]"A",[i="1"]
"Hi!""Hi!"
"A""A"<?xml version='1.0'?><?xml version='1.0'?>
10
SAX Event Call-back API
• A de-facto industry standardo Not an official standard or W3C
Recommendationo Developed by members of the xml-dev mailing
list
• Supported directly by major XML parserso Most are Java based and free: Sun JAXP, IBM
XML4J, Oracle's XML Parser for Java, Apache Xerces; MSXML (in IE 5), James Clark's XP
11
SAX 2.0 Interfaces
SAX interfaces: Parser-to-application (or call-back) interfaces
to attach special behaviour to parser-generated events
Application-to-parser to use the parser
Auxiliary to manipulate parser-provided information
12
SAX Processing Example
<?xml version="1.0" encoding="ISO-8859-1"?><db>
<person idnum="1234"><last>Kilpeläinen</last><first>Pekka</first></person><person idnum="5678"><last>Möttönen</last><first>Matti</first></person><person idnum="9012"><last>Möttönen</last><first>Maija</first></person><person idnum="3456"><last>Römppänen</last><first>Maija</first></person>
</db>
13
SAX Processing Example (2)
Task: Format the document as a list like this:
Pekka Kilpeläinen (1234)Matti Möttönen (5678)Maija Möttönen (9012)Maija Römppänen (3456)
14
SAX Processing Example (3)
Solution: using event-based processing: at the start of a person, record the idnum (e.g.,
1234) keep track of starts and ends of elements last and first, in order to record content of those elements (e.g., "Kilpeläinen" and "Pekka")
at the end of each person, output the collected data
15
SAX Programming Example Application: Begin by importing relevant classes:
import org.xml.sax.XMLReader;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;
//Default (no-op) implementation of//interface ContentHandler:import org.xml.sax.helpers.DefaultHandler;
// SUN JAXP used to obtain a SAX parser:import javax.xml.parsers.*;
16
SAX Programming Example (2) Define a class to implement relevant call-back
methods:
public class SAXDBApp extends DefaultHandler{// Flags to remember element context:private boolean InFirst = false,
InLast = false;// Storage for element contents and // attribute values:
private String FirstName, LastName, IdNum;
17
SAX Programming Example (3) Call-back methods: record the start of first and last
elements, and the idnum attribute of a person:public void startElement (
String namespaceURI, String localName, String rawName, Attributes atts) {
if (localName.equals("first")) InFirst = true;
if (localName.equals("last")) InLast = true;
if (localName.equals("person")) IdNum = atts.getValue("idnum");
} // startElement
18
SAX Programming Example (4) Call-back methods continue: Record the text content of
elements first and last in corresponding variables:
public void characters (char ch[], int start, int length) {
if (InFirst) FirstName = new String(ch, start, length);
if (InLast) LastName = new String(ch, start, length);
} // characters
19
SAX Programming Example (5) Call-back methods continue: at an exit from person,
output the collected data:
public void endElement(String namespaceURI, String localName, String qName) {
if (localName.equals("person"))
System.out.println(FirstName + " " + LastName + " (" + IdNum + ")" );
//Update the context flags:
if (localName.equals("first"))
InFirst = false;
//(Correspondingly for "last" and InLast)
20
SAX Programming Example (6) Application main method:public static void main (String args[])
throws Exception {// Instantiate an XMLReader (from JAXP // SAXParserFactory): SAXParserFactory spf =
SAXParserFactory.newInstance(); try {
SAXParser saxParser = spf.newSAXParser(); XMLReader xmlReader =
saxParser.getXMLReader();
21
SAX Processing Example (9) Main method continues:// Instantiate and pass a new // ContentHandler to xmlReader:
ContentHandler handler = new SAXDBApp(); xmlReader.setContentHandler(handler); for (int i = 0; i < args.length; i++) { xmlReader.parse(args[i]); }
} catch (Exception e) {System.err.println(e.getMessage());
System.exit(1);};
} // main
22
DOM – a Tree-based API
Object Model Interfaces
Application interacts with an object-oriented representation of the parser the document parse tree consisting of objects like
Document, Element, Attribute, Text, … Abstraction level higher than in event based
interfaces; more powerful access to descendants, following siblings, …
Disadvantage: Higher memory consumption
24
An Object-Model Based Application
ApplicationApplication
ParserParserObjectObject
In-Memory In-Memory Document Document
RepresentationRepresentation
ParseParse
Access/Access/ModifyModify
BuildBuild
DocumentDocument
i=1i=1AA
"Hi!""Hi!"
<A i="1"><A i="1"> </A></A>Hi!Hi!
25
Document Object Model (DOM)
To provide uniform access to structured documents in diverse applications (parsers, browsers, editors, databases)
Overview of W3C DOM Specification Level 1, W3C Rec, Oct. 1998 Level 2, W3C Rec, Nov. 2000 Level 3, W3C Rec, Apr 2004
26
Document Object Model (DOM) (2)
An object-based, language-neutral API for XML and HTML documents
allows programs and scripts to build documents, navigate their structure, add, modify or delete elements and content
Provides a foundation for developing, querying, filtering, transformation, rendering etc.
27
DOM structure model
Based on O-O concepts: methods (to access or change object’s state) interfaces (declaration of a set of methods) objects (encapsulation of data and methods)
Roughly similar to the XSLT/XPath data model a parse tree
28
29
invoiceinvoice
invoicepageinvoicepage
namename
addresseeaddressee
addressdataaddressdata
addressaddress
form="00"form="00"type="estimatedbill"type="estimatedbill"
Leila LaskuprinttiLeila Laskuprintti streetaddressstreetaddress postofficepostoffice
70460 KUOPIO70460 KUOPIOPyynpolku 1Pyynpolku 1
<invoice><invoice> <invoicepage form="00" <invoicepage form="00" type="estimatedbill">type="estimatedbill"> <addressee><addressee> <addressdata><addressdata> <name>Leila Laskuprintti</name><name>Leila Laskuprintti</name> <address><address> <streetaddress>Pyynpolku 1<streetaddress>Pyynpolku 1 </streetaddress></streetaddress> <postoffice>70460 KUOPIO<postoffice>70460 KUOPIO </postoffice></postoffice> </address></address> </addressdata></addressdata> </addressee> ...</addressee> ...
DocumentDocument
ElementElement
NamedNodeMapNamedNodeMap
TextText
DOM structure DOM structure modelmodel
Structure of DOM Level 1
I: DOM Core Interfaces Fundamental interfaces
basic interfaces to structured documents Extended interfaces
XML specific: CDATASection, DocumentType, Notation, Entity, EntityReference, ProcessingInstruction
II: DOM HTML Interfaces more convenient to access HTML documents
30
DOM Level 2 Level 1: basic representation and
manipulation of document structure and content (No access to the contents of a DTD)
DOM Level 2 adds support for namespaces accessing elements by ID attribute values optional features
interfaces to document views and style sheets an event model (user actions on elements) methods for traversing the document tree and manipulating
regions of document (e.g., selected by the user of an editor)
31
DOM(core)
The primary types of objects : Node
Base type of most objects Element
Represents the elements in a document DocFragment
Root node of a document fragment Document
Represents the root node of a standalone document
32
DOM(core)
The following are auxiliary types of objects : NodeIterator
Used for iterating over a set of nodes specified by filter AttributeList
Represents a collection of attribute objects, indexed by attribute name
Attribute Represents an attribute in an element object
DocumentContext A repository for metadata about a document
DOM Provides instance-independent document operations
33
34
DOM interfaces: NodeDOM interfaces: Node
invoice
invoicepage
name
addressee
addressdata
address
form="00"type="estimatedbill"
Leila Laskuprintti streetaddress postoffice
70460 KUOPIOPyynpolku 1
NodeNodegetNodeTypegetNodeTypegetNodeValuegetNodeValuegetOwnerDocumentgetOwnerDocumentgetParentNodegetParentNodehasChildNodeshasChildNodes getChildNodesgetChildNodesgetFirstChildgetFirstChildgetLastChildgetLastChildgetPreviousSiblinggetPreviousSiblinggetNextSiblinggetNextSiblinghasAttributeshasAttributes getAttributesgetAttributesappendChild(newChild)appendChild(newChild)insertBefore(newChild,refChild)insertBefore(newChild,refChild)replaceChild(newChild,oldChild)replaceChild(newChild,oldChild)removeChild(oldChild)removeChild(oldChild)
DocumentDocument
ElementElement
NamedNodeMapNamedNodeMap
TextText
Object Creation in DOM Each DOM object X lives in the context of a Document: X.getOwnerDocument()
Objects implementing interface X are created by factory methods
D.createX(…) ,where D is a Document object. E.g: createElement("A"), createAttribute("href"), createTextNode("Hello!")
Creation and persistent saving of Documents left to be specified by implementations
35
36
invoiceinvoice
invoicepageinvoicepage
namename
addresseeaddressee
addressdataaddressdata
addressaddress
form="00"form="00"type="estimatedbill"type="estimatedbill"
Leila LaskuprinttiLeila Laskuprintti streetaddressstreetaddress postofficepostoffice
70460 KUOPIO70460 KUOPIOPyynpolku 1Pyynpolku 1
DocumentDocumentgetDocumentElementgetDocumentElementcreateAttribute(name)createAttribute(name)createElement(tagName)createElement(tagName)createTextNode(data)createTextNode(data)getDocType()getDocType()getElementById(IdVal)getElementById(IdVal)
NodeNode
DocumentDocument
ElementElement
NamedNodeMapNamedNodeMap
TextText
DOM interfaces: DocumentDOM interfaces: Document
37
DOM interfaces: ElementDOM interfaces: Element
invoiceinvoice
invoicepageinvoicepage
namename
addresseeaddressee
addressdataaddressdata
addressaddress
form="00"form="00"type="estimatedbill"type="estimatedbill"
Leila LaskuprinttiLeila Laskuprintti streetaddressstreetaddress postofficepostoffice
70460 KUOPIO70460 KUOPIOPyynpolku 1Pyynpolku 1
ElementElementgetTagNamegetTagNamegetAttributeNode(name)getAttributeNode(name)setAttributeNode(attr)setAttributeNode(attr)removeAttribute(name)removeAttribute(name)getElementsByTagName(name)getElementsByTagName(name)hasAttribute(name)hasAttribute(name)
NodeNode
DocumentDocument
ElementElement
NamedNodeMapNamedNodeMap
TextText
Additional Core Interfaces NodeList for ordered lists of nodes
e.g. from Node.getChildNodes() or Element.getElementsByTagName("name")
all descendant elements of type "name" in document order (wild-card "*"matches any element type)
• Accessing a specific node, or iterating over Accessing a specific node, or iterating over all nodes of a all nodes of a NodeListNodeList::– E.g. Java code to process all children:E.g. Java code to process all children:for for (i=0;(i=0;
i<node.getChildNodes().getLength(); i<node.getChildNodes().getLength();
i++) i++)
process(node.getChildNodes().item(i));process(node.getChildNodes().item(i));38
Additional Core Interfaces (2)
NamedNodeMap for unordered sets of nodes accessed by their name: e.g. from Node.getAttributes()
39
DOM: Implementations
Java-based parsers e.g. IBM XML4J, Apache Xerces, Apache Crimson
MS IE5 browser: COM programming interfaces for C/C++ and MS Visual Basic, ActiveX object programming interfaces for script languages
XML::DOM (Perl implementation of DOM Level 1) etc.
40
A Java-DOM Example
A stand-alone toy application BuildXml either creates a new db document with two person elements, or adds them to an existing db document
based on the example in Sect. 8.6 of Deitel et al: XML - How to program
Use DOM support in Sun JAXP
41
Code of BuildXml (1) Begin by importing necessary packages:
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
// Native (parse and write) methods of the
// JAXP 1.1 default parser (Apache Crimson):
import org.apache.crimson.tree.XmlDocument;
42
Code of BuildXml (2) Class for modifying the document in file fileName:
public class BuildXml { private Document document;
public BuildXml(String fileName) { File docFile = new File(fileName); Element root = null; // doc root element
// Obtain a SAX-based parser: DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
43
Code of BuildXml (3)
try { // to get a new DocumentBuilder: documentBuilder builder =
factory.newInstance();
if (!docFile.exists()) { //create new doc document = builder.newDocument();
// add a comment: Comment comment =
document.createComment( "A simple personnel
list"); document.appendChild(comment);
// Create the root element: root = document.createElement("db");
document.appendChild(root); 44
Code of BuildXml (4)
… or if docFile already exists:
} else { // access an existing doc try { // to parse docFile
document = builder.parse(docFile);root = document.getDocumentElement();
} catch (SAXException se) {System.err.println("Error: " +
se.getMessage() );System.exit(1);
}
/* A similar catch for a possible IOException */
45
Code of BuildXml (5)
Create and add two child elements to root:
Node personNode = createPersonNode(document, "1234",
"Pekka", "Kilpeläinen");
root.appendChild(personNode);
personNode = createPersonNode(document, "5678",
"Irma", "Könönen");
root.appendChild(personNode);
46
Code of BuildXml (6)
Finally, store the result document:
try { // to write the // XML document to file fileName
((XmlDocument) document).write( new FileOutputStream(fileName));
} catch ( IOException ioe ) {
ioe.printStackTrace(); }
47
Methods to create person elements
public Node createPersonNode(Document document, String idNum, String fName, String lName)
{ Element person =
document.createElement("person"); person.setAttribute("idnum", idNum); Element firstName =
document. createElement("first");person.appendChild(firstName);
firstName. appendChild( document. createTextNode(fName) );
/* … similarly for a lastName */ return person;
} 48
The main method for BuildXml
public static void main(String args[]){ if (args.length > 0) {
String fileName = args[0]; BuildXml buildXml = new
BuildXml(fileName); } else {
System.err.println("Give filename as argument");
};} // main
49
Summary of XML APIs
XML processors make the structure and contents of XML documents available to applications through APIs
Event-based APIs notify application through parsing events e.g., the SAX call-back interfaces
Object-model (or tree) based APIs provide a full parse tree
e.g, DOM, W3C Recommendation more convenient, but may require too much
resources with very large documents Major parsers support both SAX and DOM
50
XSL Transformation(XSLT)
Slides taken & adapted from -Grigoris Antoniou & Frank van Harmelen, A Semantic Web Primer - Chapter 2 - Structured Web Documents in XML-Andy Clark, XML Style Sheets, http://people.apache.org/~andyc/xml/present/
52
Displaying XML Documents
<author><name>Grigoris Antoniou</name><affiliation>University of Bremen</affiliation><email>[email protected]</email>
</author>
may be displayed in different ways: Grigoris Antoniou Grigoris AntoniouUniversity of Bremen University of [email protected] [email protected]
53
Style Sheets
Style sheets can be written in various languagesE.g. CSS2 (cascading style sheets level 2)XSL (extensible stylesheet language)
XSL includes a transformation language (XSLT)a formatting languageBoth are XML applications
54
XSL Transformations (XSLT)
XSLT specifies rules with which an input XML document is transformed to another XML document an HTML document plain text
The output document may use the same DTD or schema, or a completely different vocabulary
XSLT can be used independently of the formatting language
55
XSLT (2)
Move data and metadata from one XML representation to another
XSLT is chosen when applications that use different DTDs or schemas need to communicate
XSLT can be used for machine processing of content without any regard to displaying the information for people to read.
In the following example, we use XSLT only to display XML documents
56
XSLT Transformation into HTML <xsl:template match="/author">
<html><head><title>An author</title></head><body bgcolor="white">
<b><xsl:value-of select="name"/></b><br /><xsl:value-of select="affiliation"/><br /><i><xsl:value-of select="email"/></i>
</body></html>
</xsl:template>
57
Style Sheet Output
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b>Grigoris Antoniou</b><br>
University of Bremen<br>
<i>[email protected]</i>
</body>
</html>
58
Observations About XSLT
XSLT documents are XML documents XSLT resides on top of XML
The XSLT document defines a template In this case an HTML document, with some
placeholders for content to be inserted xsl:value-of retrieves the value of an
element and copies it into the output document It places some content into the template
59
A Template
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b>...</b><br>
...<br>
<i>...</i>
</body>
</html>
60
Auxiliary Templates
We have an XML document with details of several authors
It is a waste of effort to treat each author element separately
In such cases, a special template is defined for author elements, which is used by the main template
61
Example of XML Document
<authors><author>
<name>Grigoris Antoniou</name><affiliation>University of Bremen</affiliation><email>[email protected]</email>
</author><author>
<name>David Billington</name><affiliation>Griffith University</affiliation><email>[email protected]</email>
</author></authors>
62
Example of an Auxiliary Template
<xsl:template match="/"><html>
<head><title>Authors</title></head><body bgcolor="white">
<xsl:apply-templates select="authors"/><!-- Apply templates for AUTHORS children -->
</body></html>
</xsl:template>
63
Example of an Auxiliary Template (2)
<xsl:template match="authors"><xsl:apply-templates select="author"/>
</xsl:template><xsl:template match="author">
<h2><xsl:value-of select="name"/></h2>Affiliation:<xsl:value-of
select="affiliation"/><br>Email: <xsl:value-of select="email"/><p>
</xsl:template>
64
Multiple Authors Output
<html><head><title>Authors</title></head><body bgcolor="white">
<h2>Grigoris Antoniou</h2>Affiliation: University of Bremen<br>Email: [email protected]<p><h2>David Billington</h2>Affiliation: Griffith University<br>Email: [email protected]<p>
</body></html>
65
Explanation of the Example
xsl:apply-templates element causes all children of the context node to be matched against the selected path expression E.g., if the current template applies to /, then the element
xsl:apply-templates applies to the root element I.e. the authors element (/ is located above the root element) If the current context node is the authors element, then the
element xsl:apply-templates select="author" causes the template for the author elements to be applied to all author children of the authors element
66
Explanation of the Example (2)
It is good practice to define a template for each element type in the documentEven if no specific processing is applied to
certain elements, the xsl:apply-templates element should be used
E.g. authors In this way, we work from the root to the
leaves of the tree, and all templates are applied
67
Processing XML Attributes
Suppose we wish to transform to itself the element:
<person firstname="John" lastname="Woo"/>
Wrong solution:
<xsl:template match="person"><person firstname="<xsl:value-of select="@firstname">"lastname="<xsl:value-of select="@lastname">"/>
</xsl:template>
68
Processing XML Attributes (2)
Not well-formed because tags are not allowed within the values of attributes
We wish to add attribute values into template
<xsl:template match="person"><person
firstname="{@firstname}"lastname="{@lastname}"/>
</xsl:template>
69
Transforming an XML Document to Another
70
Transforming an XML Document to Another (2)
<xsl:template match="/"><?xml version="1.0" encoding="UTF-16"?><authors>
<xsl:apply-templates select="authors"/></authors>
</xsl:template>
<xsl:template match="authors"><author>
<xsl:apply-templates select="author"/></author>
</xsl:template>
71
Transforming an XML Document to Another (3)
<xsl:template match="author"><name><xsl:value-of select="name"/></name><contact>
<institution><xsl:value-of select="affiliation"/>
</institution><email><xsl:value-of
select="email"/></email></contact>
</xsl:template>
Creating XSLT document
Example of empty XSLT document
<?xml version=‘1.0’ encoding=‘UTF-8’?><xsl:stylesheet
xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ version=‘1.0’></xsl:stylesheet>
72
Note: This will simply copy the text content of the input document to the output.
XSLT Features
TemplatesMap input patterns to output
Conditionals Loops Functions Extensions
73
Conditionals
If statement<xsl:if test='expression'> ... </xsl:if>
Switch statement<xsl:choose>
<xsl:when test='expression'> ... </xsl:when> <xsl:otherwise> ... </xsl:otherwise>
</xsl:choose> Predicates
foo[@bar="value"]
74
Loops
For statement<xsl:for-each select='expression'>
<xsl:sort select='expression'/> …
</xsl:for-each>
75
XPath Functions
Node-set functionse.g. position(), last(), local-name(), etc…
String functionse.g. string(), contains(), substring(), etc…
Boolean functionse.g. boolean(), not(), etc…
Number functionse.g. number(), sum(), round(), etc…
76
Example Transformation Source
Destination
01 <order>02 <item code=‘BK123’>03 <name>Care and Feeding of Wombats</name>04 <price currency=‘USD’>42.00</price>05 </item>06 </order>
01 <HTML>02 <BODY>03 <TABLE border=‘1’>04 <TR> <TH>Item</TH> <TH>Price</TH> </TR>05 <TR> 06 <TD>BK123 - <U>Care and Feeding of Wombats</U></TD>07 <TD>$42.00</TD>08 </TR>09 </TABLE>10 </BODY>11 </HTML>
77
Example Transformation (1 of 14)
Match <order> element01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’02 version=‘1.0’>0304 <xsl:template match=‘/order’>05 <HTML>06 <BODY>07 <TABLE border=‘1’>08 <TR> <TH>Item</TH> <TH>Price</TH> </TR>09 <xsl:for-each select=‘item’>10 <xsl:apply-templates/>11 </xsl:for-each>12 </TABLE>13 </BODY>14 </HTML>15 </xsl:template>
78
Example Transformation (2 of 14)
Match <order> element01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’02 version=‘1.0’>0304 <xsl:template match=‘/order’>05 <HTML>06 <BODY>07 <TABLE border=‘1’>08 <TR> <TH>Item</TH> <TH>Price</TH> </TR>09 <xsl:for-each select=‘item’>10 <xsl:apply-templates/>11 </xsl:for-each>12 </TABLE>13 </BODY>14 </HTML>15 </xsl:template>
79
Example Transformation (3 of 14)
Match <order> element01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’02 version=‘1.0’>0304 <xsl:template match=‘/order’>05 <HTML>06 <BODY>07 <TABLE border=‘1’>08 <TR> <TH>Item</TH> <TH>Price</TH> </TR>09 <xsl:for-each select=‘item’>10 <xsl:apply-templates/>11 </xsl:for-each>12 </TABLE>13 </BODY>14 </HTML>15 </xsl:template>
80
Example Transformation (4 of 14)
Match <order> element01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’02 version=‘1.0’>0304 <xsl:template match=‘/order’>05 <HTML>06 <BODY>07 <TABLE border=‘1’>08 <TR> <TH>Item</TH> <TH>Price</TH> </TR>09 <xsl:for-each select=‘item’>10 <xsl:apply-templates/>11 </xsl:for-each>12 </TABLE>13 </BODY>14 </HTML>15 </xsl:template>
81
Example Transformation (5 of 14)
Match <order> element01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’02 version=‘1.0’>0304 <xsl:template match=‘/order’>05 <HTML>06 <BODY>07 <TABLE border=‘1’>08 <TR> <TH>Item</TH> <TH>Price</TH> </TR>09 <xsl:for-each select=‘item’>10 <xsl:apply-templates/>11 </xsl:for-each>12 </TABLE>13 </BODY>14 </HTML>15 </xsl:template>
82
Example Transformation (6 of 14)
Match <item> element
17 <xsl:template match=‘item’>18 <TR>19 <TD>20 <xsl:value-of select=‘@code’/> -21 <U> <xsl:value-of select=‘name’/> </U>22 </TD>23 <xsl:apply-templates select=‘price’/>24 </TR>25 </xsl:template>
83
Example Transformation (7 of 14)
Match <item> element
17 <xsl:template match=‘item’>18 <TR>19 <TD>20 <xsl:value-of select=‘@code’/> -21 <U> <xsl:value-of select=‘name’/> </U>22 </TD>23 <xsl:apply-templates select=‘price’/>24 </TR>25 </xsl:template>
84
Example Transformation (8 of 14)
Match <item> element17 <xsl:template match=‘item’>18 <TR>19 <TD>20 <xsl:value-of select=‘@code’/> -21 <U> <xsl:value-of select=‘name’/> </U>22 </TD>23 <xsl:apply-templates select=‘price’/>24 </TR>25 </xsl:template>
85
Example Transformation (9 of 14)
Match <item> element
17 <xsl:template match=‘item’>18 <TR>19 <TD>20 <xsl:value-of select=‘@code’/> -21 <U> <xsl:value-of select=‘name’/> </U>22 </TD>23 <xsl:apply-templates select=‘price’/>24 </TR>25 </xsl:template>
86
Example Transformation (10 of 14)
Match <price> element27 <xsl:template match=‘price’>28 <TD>29 <xsl:choose>30 <xsl:when test=‘@currency/text()=“JPN”’> ¥ </xsl:when>31 <xsl:otherwise> $ </xsl:otherwise>32 </xsl:choose>33 <xsl:value-of select=‘text()’/>34 </TD>35 </xsl:template>3637 </xsl:stylesheet>
87
Example Transformation (11 of 14)
Match <price> element27 <xsl:template match=‘price’>28 <TD>29 <xsl:choose>30 <xsl:when test=‘@currency/text()=“JPN”’> ¥ </xsl:when>31 <xsl:otherwise> $ </xsl:otherwise>32 </xsl:choose>33 <xsl:value-of select=‘text()’/>34 </TD>35 </xsl:template>3637 </xsl:stylesheet>
88
Example Transformation (12 of 14)
Match <price> element
27 <xsl:template match=‘price’>28 <TD>29 <xsl:choose>30 <xsl:when test=‘@currency/text()=“JPN”’> ¥ </xsl:when>31 <xsl:otherwise> $ </xsl:otherwise>32 </xsl:choose>33 <xsl:value-of select=‘text()’/>34 </TD>35 </xsl:template>3637 </xsl:stylesheet>
89
Example Transformation (13 of 14)
Match <price> element
27 <xsl:template match=‘price’>28 <TD>29 <xsl:choose>30 <xsl:when test=‘@currency/text()=“JPN”’> ¥ </xsl:when>31 <xsl:otherwise> $ </xsl:otherwise>32 </xsl:choose>33 <xsl:value-of select=‘text()’/>34 </TD>35 </xsl:template>3637 </xsl:stylesheet>
90
Example Transformation (14 of 14)
Output
Item Price
BK123 - Care and Feeding of Wombats $42.00
91
Rendering XML in Browsers Latest browsers (e.g. IE 6.0+) have support
for XSLT Insert “xml-stylesheet” processing instruction
<?xml-stylesheet type=‘text/xsl’ href=‘style.xsl’?> Output
Item Price
BK123 - Care and Feeding of Wombats $42.00
92
Useful Links
XPath 1.0 Specificationhttp://www.w3.org/TR/xpath
XSLT 1.0 Specificationhttp://www.w3.org/TR/xslt
93