Docsity

XSLT - Advance Java Web Technology - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download XSLT - Advance Java Web Technology - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

XSLT

XSLT

• XSLT stands for Extensible Stylesheet Language

Transformations

• XSLT is used to transform XML documents into

other kinds of documents--usually, but not

necessarily, XHTML

• XSLT uses two input files:

  • The XML document containing the actual data
  • The XSL document containing both the “framework”

in which to insert the data, and XSLT commands to do

so

The .xsl file

• An XSLT document has the .xsl extension

• The XSLT document begins with:

• Contains one or more templates, such as:

...

• And ends with:

Finding the message text

• The template says to

select the entire file

  • You can think of this as selecting the root node of the

XML tree

• Inside this template,

  • selects the

message child

  • Alternative Xpath expressions that would also work:
    • ./message
    • /message/text() (text() is an XPath function )
    • ./message/text()

How XSLT works

  • The XML text document is read in and stored as a tree of nodes
  • The template is used to select the entire tree
  • The rules within the template are applied to the matching nodes, thus changing the structure of the XML tree - If there are other templates, they must be called explicitly from the main template
  • Unmatched parts of the XML tree are not changed
  • After the template is applied, the tree is written out again as a text document

Where XSLT can be used

• With an appropriate program, such as Xerces,

XSLT can be used to read and write files

• A server can use XSLT to change XML files into

HTML files before sending them to the client

• A modern browser can use XSLT to change XML

into HTML on the client side

  • This is what we will mostly be doing in this class

• Most users seldom update their browsers

  • If you want “everyone” to see your pages, do any XSL

processing on the server side

  • Otherwise, think about what best fits your situation

xsl:value-of

•

selects the contents of an element and adds it

to the output stream

  • The select attribute is required
  • Notice that xsl:value-of is not a container, hence it needs to end with a slash

• Example (from an earlier slide):

xsl:for-each

  • xsl:for-each is a kind of loop statement

  • The syntax is

     _Text to insert and rules to apply_ 
  • Example: to select every book (//book) and make an unordered list () of their titles (title), use:

Filter details

• Here is the filter we just used:

• author is a sibling of title, so from title we have

to go up to its parent, book, then back down to

author

• This filter requires a quote within a quote, so we

need both single quotes and double quotes

• Legal filter operators are:

= != < >

  • Numbers should be quoted, but apparently don’t have

to be

But it doesn’t work right!

  • Here’s what we did:
  • This will output and for every book, so we will get empty bullets for authors other than Terry Pratchett
  • There is no obvious way to solve this with just xsl:value-of

xsl:choose

• The xsl:choose ... xsl:when ...

xsl:otherwise construct is XML’s equivalent

of Java’s switch ... case ... default

statement

• The syntax is:

... some code ...

... some code ...

  • xsl:choose is often used within an xsl:for-each loop

xsl:sort

• You can place an xsl:sort inside an xsl:for-each

• The attribute of the sort tells what field to sort on

• Example:

by

  • This example creates a list of titles and authors, sorted

by author

Creating tags from XML data

• Suppose the XML contains

Dr. Dave's Home Page http://www.cis.upenn.edu/~matuszek

• And you want to turn this into

Dr. Dave's Home Page

• We need additional tools to do this

  • It doesn’t even help if the XML directly contains

Dr. Dave's Home Page -- we still can’t move it to the

output

  • The same problem occurs with images in the XML

Creating tags--solution 1

  • Suppose the XML contains Dr. Dave's Home Page http://www.cis.upenn.edu/~matuszek
  • adds the named attribute to the enclosing tag
  • The value of the attribute is the content of this tag
  • Example:
  • Result: Dr. Dave's Home Page