XSLT: Transforming XML with USF's Distributed Software Development Course, Lab Reports of Software Engineering

This document from the university of san francisco's department of computer science introduces xslt, an xml-based language for transforming documents. Xslt allows users to specify output for particular elements and is useful for emitting html, converting tag vocabularies, extracting plain text, and modifying xml documents. Various aspects of xslt, including output formats, filtering elements, emitting html and xml, copying nodes, and incorporating css.

Typology: Lab Reports

Pre 2010

Uploaded on 07/30/2009

koofers-user-qzs-1
koofers-user-qzs-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Distributed Software Development
XSLT
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science University of San Francisco p. 1/??
9-2: XSLT
XSLT is an XML-based language that allows youto
declaratively specify how a document should be changed or
transformed.
Youspecify the output for a particular element; no need to
manage tree traversal.
Useful for:
Emitting an HTML display of an XML document
Converting between tag vocabularies
Extracting plain text from an XML document
AUtomatically modifying or filtering an XML document.
Departmentof Computer Science University of San Francisco p. 2/??
9-3: Output
Youcan transform an XML document into:
Plain text
HTML
XML (or any flavor thereof)
Departmentof Computer Science University of San Francisco
9-4: Our CD database
<catalog>
<song>
<title> Tomorrow Never Knows </title>
<artist> Beatles </artist>
<album> Revolver </album>
<genre> Rock </genre>
<rating> 5 </rating>
<length> 2:57 </length>
<date>
<day> 6 </day>
<month> Feb </month>
<year> 2005 </year>
</date>
</song>
...
</catalog>
Departmentof Computer Science University of San Francisco p. 4/??
9-5: Step 1: Using XSLT to emit plain text
To begin, let’s use XSLT to print a plaintext version of our
catalog.
We can run XSLTfrom the command line or within a browser.
/usr/bin/4xslt on lab machines
Most modern browsers have XSLTsuppor t
Debugging is easier from the command line
Departmentof Computer Science University of San Francisco p. 5/??
9-6: Step 2: filtering elements
That’s fine, but pretty dull.
Let’s select just artist, title, and album to display.
We do that through the use of apply-templates.
What if we left out apply-templates in the song template?
Departmentof Computer Science University of San Francisco
pf3
pf4
pf5

Partial preview of the text

Download XSLT: Transforming XML with USF's Distributed Software Development Course and more Lab Reports Software Engineering in PDF only on Docsity!

Distributed Software Development

XSLT Chris Brooks

Department of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p. 1/

??

9-2:^ XSLT^ •^ XSLT is an XML-based language that allows you to^ declaratively

specify how a document should be changed or transformed.^ ◦^ You specify the output for a particular element; no need tomanage tree traversal. • Useful for:^ ◦^ Emitting an HTML display of an XML document^ ◦^ Converting between tag vocabularies^ ◦^ Extracting plain text from an XML document^ ◦^ AUtomatically modifying or filtering an XML document.

Department of Computer Science — University of San Francisco – p. 2/

??

9-3:^ Output^ •^ You can transform an XML document into:^ ◦^

Plain text ◦ HTML ◦ XML (or any flavor thereof)

Department of Computer Science — University of San Fra

9-4:^ Our CD database

Tomorrow

Never^ Knows

Beatles

Revolver

Rock^

5

2:57^

6 ^ Feb^ ^2005 ...

Department of Computer Science — University of San Francisco – p. 4/

??

9-5:^ Step 1: Using XSLT to emit plain text^ •^ To begin, let’s use XSLT to print a plaintext version of ourcatalog.^ •^ We can run XSLT from the command line or within a browser.^ ◦^

/usr/bin/4xslt on lab machines ◦ Most modern browsers have XSLT support ◦ Debugging is easier from the command line

Department of Computer Science — University of San Francisco – p. 5/

??

9-6:^ Step 2: filtering elements^ •^ That’s fine, but pretty dull.^ •^ Let’s select just artist, title, and album to display.^ •^ We do that through the use of apply-templates.^ •^ What if we left out apply-templates in the song template?

Department of Computer Science — University of San Fra

9-7:^ Emitting HTML^ •^ We can also emit other markup languages, such as HTML.(XHTML, actually).^ •^ Just indicate the tags to be produced by a template match.

Department of Computer Science — University of San Francisco – p. 7/

??

9-8:^ Emitting XML^ •^ We can also use XSLT to create new XML documents withdifferent tag names or contents.^ •^ For example, let’s say we want to change the tags to be inSpanish.

Department of Computer Science — University of San Francisco – p. 8/

??

9-9:^ Copying Nodes^ •^ When transforming from XML to XML, often, it’s useful to copysections of a document without changing it.^ •^ copy makes a shallow copy of a node.^ ◦^

Useful if you want to change a bunch of values or attributes. • copy-of makes a deep copy and lets you specify a path. • For example, let’s make a new database with just artist, albumand title.

Department of Computer Science — University of San Fra

9-10:^ Incorporating CSS^ •^ We can still use CSS to control presentational elements.^ •^ With HTML, we can just embed a ’link’ tag in the generatedHTML.

Department of Computer Science — University of San Francisco – p. 10/

??

9-11:^ Incorporating CSS^ •^ If we’re emitting XML, we can instead embed a

processing

instruction

into the output document.

-^ Note: this will work best if we do the XSLT on the server side.^

href="songs.css"

type="text/css"

Department of Computer Science — University of San Francisco – p. 11/

??

9-12:^ Referencing a stylesheet from an XML document^ •^ The command line is great for debugging, but much of the time,we want the client to do the work.^ •^ Most web browsers have at least some support for XSLT.^ ◦^

More advanced features are not universally supported. ◦ In particular, the browser’s XSLT processor may make asingle pass and not apply the CSS. (firefox)

Department of Computer Science — University of San Fran

9-19:^ Predicates^ •^ If you need more flexibility in specifying nodes of interest, youcan use a predicate.^ •^ Predicates are contained inside square brackets.^ •^ To be included in final node set, a node must pass both axisand predicate tests.

Department of Computer Science — University of San Francisco – p. 19/

??

9-20:^ Examples^ •^ //song/[id=”s1”]/title/text - text for all ’s1’ songs.^ •^ //song[title] - all quotations that have a source subelement.^ •^ //song[not(source)] - songs that do not have a title sub-element.^ •^ //song[position() == 2] or //song[2] - the second quotation.

Department of Computer Science — University of San Francisco – p. 20/

??

9-21:^ So what’s all this good for?^ •^ XPath is very useful for allowing users to query an XMLdocument.^ •^ Even more useful for specifying which transformations shouldbe applied in an XML document.^ •^ gives us a way to easily specify transformations that shouldtake place based on a node’s context.

Department of Computer Science — University of San Fran

9-22:^ Sorting^ •^ XSLT also has built-in support for sorting and processing yourelements.

Department of Computer Science — University of San Francisco – p. 22/

??

9-23:^ Parameters in XSLT^ •^ You can also pass paramters into an XSLT stylesheet.^ •^ You can also define them at the top of your XSLT program.^ •^ Parameters can be referenced with a $.

...

Department of Computer Science — University of San Francisco – p. 23/

??

9-24:^ Using XSLT from Python !/usr/bin/pythonfrom^ Ft.Xml.Xslt.Processor

import^

Processor

from^ Ft.Xml

import

InputSource xsltproc

=^ Processor(

)

sweaterdata

= InputSource.DefaultFactory.fromUri(’sweater.xml’) sweaterxsl

=^ InputSource.DefaultFactory.fromUri(’sweater.xsl’) xsltproc.appendStylesheet(sweaterxsl)sweaterXML

=^ xsltproc.run(sweaterdata) print^ sweaterXML

Department of Computer Science — University of San Fran

9-25:^ Summary^ •^ XSLT allows you to declaratively specify how a documentshould be transformed.^ •^ Works on the parse tree.^ ◦^

Can emit text or XML • XPath allows you to easily identify nodes. • Makes it easy to have separate representations for storage ornetwor transmission and for display.

Department of Computer Science — University of San Francisco – p. 25/

??