xml and xsl - it.uu.se filexml and xsl 8 february 2010 3 why use xml? it is platform and vendor...

58
XML and XSL 8 February 2010 1 XML and XSL XML (Extensible Markup Language) has the follow- ing features. Not used to generate layout but to describe data. Uses tags to describe different items just as HTML. No predefined tags, just syntax rules for tags. XML uses a DTD (Document Type Definition) or an XSD (XML Schema Definition) to formally describe a grammar for a document. Case sensitive. All tags must be closed and properly nested. All parameter values must be enclosed within apostrophes or quotation marks.

Upload: vuongnga

Post on 14-Feb-2019

222 views

Category:

Documents


0 download

TRANSCRIPT

XML and XSL 8 February 2010 1

XML and XSL

XML (Extensible Markup Language) has the follow-ing features.

• Not used to generate layout but to describe data.

• Uses tags to describe different items just as HTML.

• No predefined tags, just syntax rules for tags.

• XML uses a DTD (Document Type Definition) oran XSD (XML Schema Definition) to formallydescribe a grammar for a document.

• Case sensitive.

• All tags must be closed and properly nested.

• All parameter values must be enclosed withinapostrophes or quotation marks.

XML and XSL 8 February 2010 2

An XML document is well formed if it conforms tothe XML syntax rules.

An XML document is valid if it is well formed andconforms to the rules in the corresponding DTD orXSD

XML and XSL 8 February 2010 3

Why use XML?

It is platform and vendor independent, thus can beeasily transported between systems.

It is designed to describe data, therefore can be usedto store documents of different kinds.

It can be translated into other representations suchas HTML, PDF.

It can be read by standard Java and converted intoan internal tree structure. Then you can extend andmanipulate the tree and write it back to an XML file.

Office 2007 uses XML documents, files with exten-sion docx are zipped collections of xml documents.

Problems?

Not very compact. Takes time to parse large docu-ments.

XML and XSL 8 February 2010 4

How is XML used in a web application?

To configure the application. The deploymentdescriptor, web.xml, is an XML document.

To store data within the application and to exchangedata with other applications.

To postpone the design of the View until it used byproviding style sheets. No hardcoded design in theapplication.

XSLT is the transformation part of XSL.

XML and XSL 8 February 2010 5

When using XML and XSLT (Extendable StylesheetLanguage for Transformation) your JSP’s merelymanages the translations and the actual layout aredescribed in the style sheets. This means that it isextremely easy to modify the layout, just edit thestyle sheet. No compilation is needed. Easy to docustomized layouts.

It is also possible that your client isn’t a browser butsomething else. In that case data can still be trans-lated into a suitable format.

XML and XSL 8 February 2010 6

Basic syntax rules

All tags must be closed,

<a> ... </a>

or

<a ... />

XML and XSL 8 February 2010 7

All elements must be properly nested

<a><b>...</b>

</a>

not

<a><b>....

</a></b>

XML and XSL 8 February 2010 8

There must be a root/start tag.

<c><a>

bla bla</a><b>

bla bla</b>

</c>

not

<a> bla bla

</a><b>

bla bla</b>

XML and XSL 8 February 2010 9

An example with a proper header line

<?xml version=”1.0” encoding=”UTF-8” ?><!DOCTYPE person><person>

<firstname>Fredrik

</firstname><lastname>

Ålund</lastname><age>

32</age>

</person>

The DOCTYPE is optional but can be used to specifythe DTD that corresponds to the document type.

XML and XSL 8 February 2010 10

If you want to describe more than one person in thesame way you cannot do,

<?xml version=”1.0” encoding=”UTF-8” ?><!DOCTYPE person><person>

<firstname>Fredrik

</firstname><lastname>

Ålund</lastname><age>

32</age>

</person><person>

<firstname>Annika

</firstname><lastname>

Ålund</lastname><age>

28</age>

</person>

Because this violates the rule of one start tag.

XML and XSL 8 February 2010 11

You have to enclose this in an other document rootlike

<?xml version=”1.0” encoding=”UTF-8” ?><!DOCTYPE family><family>

<person><firstname>

Fredrik</firstname><lastname>

Ålund</lastname><age>

32</age>

</person>

XML and XSL 8 February 2010 12

<person><firstname>

Annika</firstname><lastname>

Ålund</lastname><age>

28</age>

</person></family>

XML and XSL 8 February 2010 13

Tags can have attributes, that is name-value pairs.The values are enclosed within apostrophes or quo-tation marks.

<?xml version=”1.0” encoding=”UTF-8” ?><!DOCTYPE employee><employee department=”customer services”>

<firstname>Fredrik

</firstname><lastname>

Ålund</lastname><age>

32</age>

</employee>

XML and XSL 8 February 2010 14

The CDATA sections

Sometimes you need to put binary data or specialcharacters (such as >, &, <) in your document. TheCDATA (Character Data) allows you to do that. Thismeans that the data is passed through without inter-pretation.

E. g.

<element><! [CDATA [ 2 < 4 && 4 > 6]]>

</element>

XML and XSL 8 February 2010 15

To navigate in an XML document, you use XPath’s.

<?xml version=”1.0” encoding=”UTF-8” ?><!DOCTYPE employee><employee department=”customer services”>

<firstname>Fredrik

</firstname><lastname>

Ålund</lastname><age>

32</age>

</employee>

XML and XSL 8 February 2010 16

The age here is referred as /employee/age

An attribute is referred as /employee/@department

A predicate is something that is true or false. E. g

employee[@department = “customer service”]

Valid ops are =, =!, &lt; , &lt;=, &gt; , &gt;=, and, or

XML and XSL 8 February 2010 17

To translate XML to HTML you use XSLT. You con-struct stylesheets that is used for translation.

XSL style sheets are valid XML files.

They describe the output and the transformationmethod.

XML and XSL 8 February 2010 18

Assume that we have the following XML-file:

<?xml version=”1.0” encoding=”UTF-8” ?><!DOCTYPE employees><employees>

<employee department=”customer services”><firstname>

Fredrik</firstname><lastname>

Ålund</lastname><age>

32</age>

</employee>

XML and XSL 8 February 2010 19

<employee department=”customer services”><firstname>

Annika</firstname><lastname>

Ålund</lastname><age>

32</age>

</employee></employees>

XML and XSL 8 February 2010 20

A style sheet to translate this can be

<?xml version=”1.0” encoding=”UTF-8” ?><xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/

XSL/Transform” version=”1.0”><xsl:output method=”html”/>

<xsl:template match=”/”><html><body><table border=”3” bgcolor=”yellow”><tr><th>Name</th><th>Department</th></tr><xsl:for-each select=”employees/employee”><tr>

XML and XSL 8 February 2010 21

<td><xsl:value-of-select=”firstname”/>,<xsl:value-of-select=”lastname”/></td>

<td><xsl:value-of-select=”@department”/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>

XML and XSL 8 February 2010 22

XSL is a language that operates by matching differ-ent patterns to its input, thereby producing output.

This line matches the XPath / in an XML-file. This isusually a virtual outer layer.

<xsl:template match=”/”>

Then we have the desired HTML-code that shouldbe produced.

<html><body><table border=”3” bgcolor=”yellow”><tr><th>Name></th><th>Department</th></tr>

XML and XSL 8 February 2010 23

Then we have

<xsl:for-each select=”employees/employee”>

This will iterate through the document and selecteach employee.

XML and XSL 8 February 2010 24

Followed by

<tr><td><xsl:value-of-select=”firstname”/>,

<xsl:value-of-select=”lastname”/></td><td><xsl:value-of-select=”@department”/></td></tr>

These lines will get the value of the name tags andthe parameter value for the current employee. Thusthe iteration will walk through the document andprocess all employees.

XML and XSL 8 February 2010 25

To use this, you setup a JSP like:

<c:set var=”employee_xslt”><c:import url=”employee.xsl”/>

</c:set>

<c:set var=”employee_xml”><c:import url=”employee.xml”/>

</c:set>

<x:transform xslt=”${employee_xslt}”xml = “${employee_xml}”/>

This sets two variables and then calls the translator.The result is written to the output.

XML and XSL 8 February 2010 26

Elements in XSL

<xsl:template match=”XPath expression”>....

</xsl:template>

Create an XSL template that later can be applied onthe parts of the XML document that matches.

<xsl:apply-templates [select = “XPath expression”]/>

Apply the selected templates on all matching partsof your XML file. If the select parameter is omittedall templates will be applied.

XML and XSL 8 February 2010 27

<xsl:for-each select = “XPath expression”>...

</xsl:for-each>

Iterates over the current scope and processes allmatches for the XPath expression.

<xsl:if match = “XPath predicate”>...

</xsl:if>

A conditional statement that test the predicate.

XML and XSL 8 February 2010 28

<xsl:value-of select=”name”/>

Returns the value of the name element

<xsl:value-of select=”@parameter”/>

Returns the value of the parameter. Must be in thecurrent element, otherwise a full XPath must begiven.

<xsl:text disable-output-escaping=”true|false”>text, typical CDATA elements

</xsl:text>

Used to output text, mostly CDATA because thiswill allow special characters to pass through thetranslations unchanged.

XML and XSL 8 February 2010 29

You cannot have xslt tags inside html tags. Thereforeto generate dynamic tags, some special methods areneeded. This can be anchor tags, input tags etc.

In such cases you can use the XSL element tag.

<xsl:element name=”name”><xsl:attribute name=”blabla”>

value</xsl:attribute>...

</xsl:element>

XML and XSL 8 February 2010 30

or just the tag itself, eg <a> or <input>

<name><xsl:attribute name=”blabla”>

value</xsl:attribute>...

</name>

XML and XSL 8 February 2010 31

JSTL contains the translators, in the XML tag library,usually prefixed with “x:”.

<x:transform xml=”xmlfile” xslt=”stylesheet”/>

or

<x:transform xslt=”stylesheet”>xml document

<x:transform>

XML and XSL 8 February 2010 32

An example. Assume that we have our bookstorewith our books. The JavaBean that loads the datafrom mySQL tables produces XML that describesmy books. This is just a long string produced by thegetXml method in the bean.

XML and XSL 8 February 2010 33

It goes like this:

<booklist><book>

<id>1

</id><title>

<![CDATA[BUILDNING SCALABLE AND HIGH-PERFORMANCE JAVA WEB APPLICATIONS USING J2EE TECHNOLOGY]]>

</title><authorname>

<![CDATA[GREG]]></authorname>

XML and XSL 8 February 2010 34

<authorsurname><![CDATA[BARISH]]>

</authorsurname><price>

600</price><pages>

392</pages>

XML and XSL 8 February 2010 35

<description><![CDATA[A BOOK ABOUTBUILDNING SCALABLE ANDHIGH-PERFORMANCE JAVA WEBAPPLICATIONS USING J2EETECHNOLOGY. THE BOOKSDESCRIBES HOW TO USE THEDIFFERENT PARTS OF J2EE TOBUILD A WEB APPLICATION]]>

</description></book><book>

... other books

</book></booklist>

XML and XSL 8 February 2010 36

This data should be presented like this:

XML and XSL 8 February 2010 37

The html for this goes like:

<html><head><title>BookShop::Shop</title></head><body>

<h2>Fredriks Book Shop</h2>

<table border=”0”><tr cellspacing=”0” bgcolor=”silver”>

<td><strong>Book</strong>

</td><td>

<strong>Author</strong></td><td>

<strong>Price</strong></td>

</tr>

XML and XSL 8 February 2010 38

<form action=”shop” method=”post”><tr bgcolor=”#FFDC75”>

<td>BUILDNING SCALABLE ANDHIGH-PERFORMANCE JAVAWEB APPLICATIONS USINGJ2EE TECHNOLOGY

</td><td>

BARISH, GREG</td><td>

600</td><td>

<input size=”2” type=”text”value=”1” name=”quantity”>

</td>

XML and XSL 8 February 2010 39

<td><input value=”BUY”type=”submit”>

<a href=”shop?action=detail&bookid=1”>

Detail</a>

</td></tr>

<input type=”hidden” value=”1”name=”bookid”><input value=”add”

name=”action” type=”hidden”></form>

.....</table>

XML and XSL 8 February 2010 40

To generate this, we have the following JSP

<%@page contentType=”text/html;charset=UTF-8” pageEncoding=”ISO8859-1”

import=”se.upright.education.uu.pvk.assignmen.two.beans.*,se.upright.education.uu.pvk.assignmenttwo.tags.*“

%>

<%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%><%@taglib prefix=”x” uri=”http://java.sun.com/jsp/jstl/xml”%><%@taglib prefix=”bookshop” uri=”/bookshop”%>

<html><head><title>BookShop::Shop</title></head><body>

<h2>Fredriks Book Shop</h2><jsp:useBean id=”bookList”

class=”se.upright.education.uu.pvk.assignmenttwo.beans.BookListBean”

scope=”application”>Error, the bean should have been created in the servlet!

</jsp:useBean>

XML and XSL 8 February 2010 41

<c:set var=”booklist_xslt”> <c:import url=”booklist_xslt.xsl”/></c:set>

<x:transform xslt=”${booklist_xslt}”> <jsp:getProperty name=”bookList” property=”xml”/></x:transform>

</body>

</html>

XML and XSL 8 February 2010 42

And now all the fun, that is the style sheet.

<?xml version=”1.0” encoding=”UTF-8” ?><xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/

XSL/Transform” version=”1.0”> <xsl:output method=”html”/>

<xsl:template match=”booklist”> <table border=”0”> <tr bgcolor=”silver” cellspacing=”0”> <td> <strong>Book</strong> </td> <td> <strong>Author</strong> </td> <td> <strong>Price</strong> </td> </tr> <xsl:apply-templates/> </table> </xsl:template>

XML and XSL 8 February 2010 43

<xsl:template match=”book”> <form method=”post” action=”shop”>

<tr bgcolor=”#FFDC75” ><td>

<xsl:value-of select=”title”/></td><td>

<xsl:value-of select=”authorsurname”/>,<xsl:value-of select=”authorname”/>

</td><td>

<xsl:value-of select=”price”/></td><td>

<!--A ordinary input in XSLT--><xsl:element name=”input”> <xsl:attribute name=”size”>

2</xsl:attribute>

XML and XSL 8 February 2010 44

<xsl:attribute name=”type”>text

</xsl:attribute><xsl:attribute name=”value”>

1</xsl:attribute><xsl:attribute name=”name”>

quantity</xsl:attribute> </xsl:element>

</td>

XML and XSL 8 February 2010 45

<td><input type=”submit” value=”BUY”/><!--A LINK in XSLT--><xsl:element name=”a”>

<xsl:attribute name=”href”><xsl:text disable-output-escaping=”yes”>

<![CDATA[shop?action=detail&bookid=]]></xsl:text>

<xsl:value-of select=”id”/></xsl:attribute>

<xsl:text>Detail</xsl:text></xsl:element>

</td></tr>

XML and XSL 8 February 2010 46

<xsl:element name=”input”><xsl:attribute name=”type”>

hidden</xsl:attribute><xsl:attribute name=”value”>

<xsl:value-of select=”id”/></xsl:attribute><xsl:attribute name=”name”>

bookid</xsl:attribute>

</xsl:element><input type=”hidden” name=”action”

value=”add”/></form>

</xsl:template></xsl:stylesheet>

XML and XSL 8 February 2010 47

We start with the outermost <xsl:template> element.That is implicitly applied. This means that it willsearch for a <booklist> element in the XML data, andthat will set my current XPath.

Since we have a booklist tag in the XML data we willget a match and XSLT will start processing this tag.

It will output the html statements that it finds here.Then there is a <xsl:apply-templates/>

This will cause XSLT to look for all other templatesin the style sheet and apply them at the current posi-tion.

It will find the book template, so it will try to applythis. Since we are in <booklist> it will search for<booklist/book>.

XML and XSL 8 February 2010 48

It will apply the template on the first book item.That is, output the HTML form header.

Then it will build the table, first inserting the valueof booklist/book/title then the rest. To produce aninput tag that depends on variable data you need touse a xsl:element tag with the proper parameters, ifyour input tag is only constants you can output it inHTML.

When all of the book template is finished, XLST williterate to see if there are more matches for booklist/book. So the template will be applied for all books.

When no more matches are found, XLST will con-tinue in the booklist template again. This will justoutput the </table> tag and the finish up because thistemplate matches only once.

XML and XSL 8 February 2010 49

Another example is based on the following input

<shoppingcart><order>

<book>book data

</book><quantity>

number of books ordered</quantity>

</order><order>

....</order

</shoppingcart>

XML and XSL 8 February 2010 50

We want do produce the following output

XML and XSL 8 February 2010 51

The producing HTML for this is

<table cellspacing=”0” border=”0”><tr bgcolor=”silver”><td colspan=”4”><strong>Shoppingcart</strong></td>

<tr bgcolor=”silver”>

<td>Title</td><td>Quantity</td><td colspan=”2”>Remove</td></tr></tr>

XML and XSL 8 February 2010 52

<form action=”shop” method=”post”><tr><td>

BUILDNING SCALABLE AND HIGH-PER-FORMANCE JAVA WEB APPLICATIONSUSING J2EE TECHNOLOGY

</td><td align=”right”>

3</td><td>

<input size=”2” type=”text” value=”1”name=”quantity”>

</td>

XML and XSL 8 February 2010 53

<td><input value=”Remove” type=”submit”>

</td><input type=”hidden” value=”1”

name=”bookid”><input type=”hidden”value=”remove” name=”action”>

</tr>

</form>

</table>

XML and XSL 8 February 2010 54

To produce this we have the following style sheet

<?xml version=”1.0” encoding=”UTF-8” ?>

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”>

<xsl:output method=”html”/>

<xsl:template match=”shoppingcart”>

<br /> <br /> <table border=”0” cellspacing=”0”>

<tr bgcolor=”silver”><td colspan=”4”>

<strong>Shoppingcart</strong></td>

</tr>

XML and XSL 8 February 2010 55

<tr bgcolor=”silver”><td>Title</td>

<td>Quantity</td><td colspan=”2”>Remove</td>

</tr> <xsl:apply-templates/>

<tr><td colspan=”2”>

<a href=”shop?action=checkout”>Checkout</a>

</td>

</tr>

</table>

</xsl:template>

XML and XSL 8 February 2010 56

<xsl:template match=”order”><form method=”post” action=”shop”><tr>

<td><xsl:value-of select=”book/title”/>

</td><td align=”right”>

<xsl:value-of select=”quantity”/></td><td>

<xsl:element name=”input”><xsl:attribute name=”size”>

2</xsl:attribute><xsl:attribute name=”type”>

text</xsl:attribute><xsl:attribute name=”value”>

1</xsl:attribute><xsl:attribute name=”name”>

quantity</xsl:attribute></xsl:element>

</td>

XML and XSL 8 February 2010 57

<td><input type=”submit” value=”Remove”/>

</td><xsl:element name=”input”><xsl:attribute name=”type”>

hidden</xsl:attribute><xsl:attribute name=”value”>

<xsl:value-of select=”book/id”/></xsl:attribute><xsl:attribute name=”name”>

bookid</xsl:attribute></xsl:element>

XML and XSL 8 February 2010 58

<xsl:element name=”input”><xsl:attribute name=”type”>

hidden</xsl:attribute><xsl:attribute name=”value”>

remove</xsl:attribute><xsl:attribute name=”name”>

action</xsl:attribute></xsl:element>

</tr></form></xsl:template></xsl:stylesheet>