ECA 228 Internet/Intranet Design I Intro to XSL. ECA 228 Internet/Intranet Design I XSL basics W3C...

24
ECA 228 Internet/Intranet Design I Intro to XSL

Transcript of ECA 228 Internet/Intranet Design I Intro to XSL. ECA 228 Internet/Intranet Design I XSL basics W3C...

ECA 228 Internet/Intranet Design I

Intro to XSL

ECA 228 Internet/Intranet Design I

XSL basics

W3C standards for stylesheets– CSS– XSL: Extensible Markup Language

XSLT– used for transforming XML documents

XPath– a language for defining parts of an XML document

XSL-FO– a language for formatting XML documents

ECA 228 Internet/Intranet Design I

XSL basics cont …

XSL example6 companies6 databases

xml

6

5

4

3

2

1

ECA 228 Internet/Intranet Design I

XSL basics cont …

the most common application of XSLT is to convert XML to HTML for display in a browser

only the newest browsers are compatible with the W3C XSLT recommendations– IE5 is not compatible– NN6 supports most recommendations, but not all– IE6 and NN7 support the W3C recommendation

ECA 228 Internet/Intranet Design I

XSLT

transforms XML into HTML– add additional elements– filter and sort– loop

XSLT transforms an XML source tree into an XML result tree

reference the stylesheet in the XML document

<?xml-stylesheet type=“text/xsl” href=“path_to_doc.xsl” ?>

ECA 228 Internet/Intranet Design I

XSLT root element

every XSLT document is an XML document– well-formed– begins with XML declaration

root element for XSLT document is

or– they are synonymous

<?xml version=“1.0” ?>

<xsl:stylesheet> <xsl:transform>

ECA 228 Internet/Intranet Design I

XSLT root element cont …

define a namespace within the root element– use the official namespace of the W3C– version number is required

– matching closing tag

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

</xsl:stylesheet>

ECA 228 Internet/Intranet Design I

XSLT root template

XSLT uses sets of rules called templates– each template contains rules to apply when a node

is matched

– uses the match attribute for a whole XML document, or just a section

to apply a template to the root node

<xsl:template>

<xsl:template match=“/”>

</xsl:template>

ECA 228 Internet/Intranet Design I

Outputting XHTML

2 kinds of components in XSLT stylesheet1. instructions: how the source XML will be

processed

2. literals: HTML code and text, just as they will appear

HTML code – must be well-formed– write element and attribute names in lower case

ECA 228 Internet/Intranet Design I

Outputting XHTML cont …

inside the root template– create structure of transformed document– add HTML tags

<xsl:template match=“/”><html><head><title>blah</title> . . .

</xsl:template>

ECA 228 Internet/Intranet Design I

Outputting content of nodes

to output the actual content of a node

uses the select attribute to identify a particular node set

since <xsl:value-of /> contains no content, combine opening and closing tags

<xsl:value-of />

<xsl:value-of select='/pets/dogs/dog/name' />

ECA 228 Internet/Intranet Design I

Looping through node sets

in the previous example, <xsl:value-of /> output only one line of data

to process all the data in a node set use

loops through a node set to select every element

<xsl:for-each > </xsl:for-each >

ECA 228 Internet/Intranet Design I

Looping through node sets cont …

<xsl:for-each> uses a required attribute, select– the select attribute contains the path to the node set– path is like a file system, where forward slashes

represent subdirectories

– the path is an XPath expression

<xsl:for-each > select=“pets/dogs/dog” ><xsl:value-of select='name' />

</xsl:for-each >

ECA 228 Internet/Intranet Design I

XPath

a set of rules for defining the parts of an XML document– uses paths to define XML elements– similar to directory structures– can use both absolute and relative paths

will match all name elements, within all dog elements, within any dogs element, within the root

pets/dogs/dog/name

ECA 228 Internet/Intranet Design I

XPath cont …

relative paths use shortcuts similar to directories uses wildcards ( * ) to indicate an unknown

element

will select all name elements 3 levels down XPath is not an XML document XPath uses a variety of operators and functions

pets/*/*/name

ECA 228 Internet/Intranet Design I

Filtering

Output from an XML file can be filtered before it is output– add criteria to the select attribute in the xsl:for-each

element– place values which define the filter inside square

brackets, as part of the path

<xsl:for-each select=“/pets/dogs/dog[name=‘Halle’]”>

ECA 228 Internet/Intranet Design I

Filtering cont …

OPERATOR REPRESENTS

= equal to

!= not equal to

&lt; less than

&gt; greater than

Operators for filtering

ECA 228 Internet/Intranet Design I

Sorting

by default, nodes are processed in the order in which they appear in the XML document

sorting allows you to order them

notice the element does not have a separate closing tag

<xsl:sort />

ECA 228 Internet/Intranet Design I

Sorting cont …

<xsl:sort> uses the select attribute which indicates the node to sort on

to sort a loop alphabetically, place xsl:sort inside the xsl:for-each element

the order attribute will sort in the opposite order

<xsl:sort select=“name” />

<xsl:sort select=“name” order=“descending” />

ECA 228 Internet/Intranet Design I

Sorting cont …

<xsl:sort> allows you to sort alphabetically and numerically

to sort numerically, use the data-type attribute

otherwise, the default data as a string, which may produce unexpected results

data-type=“number”

<xsl:sort select=“name” data-type=“number” />

ECA 228 Internet/Intranet Design I

Processing node conditionally

process nodes only if certain conditions exist– equal to a certain word– less than or greater than a particular value– similar to filtering

to run a conditional test against the content of a file

<xsl:if > </xsl:if >

ECA 228 Internet/Intranet Design I

Processing node conditionally cont …

xsl:if uses the test attribute– contains an expression to be evaluated

place strings inside quotes

– uses the same operators as filtering– may test against nodes not included in xsl:value-of

<xsl:if test=“name=‘Halle’” >processing . . .

</xsl:if>

</xsl:if test=“weight &lt; 60” >

ECA 228 Internet/Intranet Design I

Testing more than one condition

xsl:if allows for only one condition to be tested to test for several conditions

nested inside xsl:choose, use series of xsl:when to define more than one condition– use as many xsl:when elements as necessary

to designate default processing, use xsl:otherwise

<xsl:choose > <xsl:otherwise ><xsl:when >

ECA 228 Internet/Intranet Design I

Testing more than one condition cont …

<xsl:choose><xsl:when test="name = 'Halle'">

processing ...</xsl:when><xsl:when test="name = 'Marley'">

processing ...</xsl:when>

<xsl:otherwise>default processing ....

</xsl:otherwise></xsl:choose>