ECA 228 InternetIntranet Design I Intro to XSL

  • Slides: 24
Download presentation
ECA 228 Internet/Intranet Design I Intro to XSL

ECA 228 Internet/Intranet Design I Intro to XSL

XSL basics l W 3 C standards for stylesheets – – CSS XSL: Extensible

XSL basics l W 3 C standards for stylesheets – – CSS XSL: Extensible Markup Language l XSLT – l XPath – l used for transforming XML documents a language for defining parts of an XML document XSL-FO – a language formatting XML documents ECA 228 Internet/Intranet Design I

XSL basics l cont … XSL example 6 companies 6 databases 1 2 6

XSL basics l cont … XSL example 6 companies 6 databases 1 2 6 xml 5 3 4 ECA 228 Internet/Intranet Design I

XSL basics l l cont … the most common application of XSLT is to

XSL basics l l 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 W 3 C XSLT recommendations – – – IE 5 is not compatible NN 6 supports most recommendations, but not all IE 6 and NN 7 support the W 3 C recommendation ECA 228 Internet/Intranet Design I

XSLT l transforms XML into HTML – – – l l additional elements filter

XSLT l transforms XML into HTML – – – l l 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 l every XSLT document is an XML document – – well-formed

XSLT root element l every XSLT document is an XML document – – well-formed begins with XML declaration <? xml version=“ 1. 0” ? > l root element for XSLT document is <xsl: stylesheet> – or <xsl: transform> they are synonymous ECA 228 Internet/Intranet Design I

XSLT root element l cont … define a namespace within the root element –

XSLT root element l cont … define a namespace within the root element – – use the official namespace of the W 3 C version number is required <xsl: stylesheet version=’ 1. 0’ xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> – matching closing tag </xsl: stylesheet> ECA 228 Internet/Intranet Design I

XSLT root template l XSLT uses sets of rules called templates – each template

XSLT root template l XSLT uses sets of rules called templates – each template contains rules to apply when a node is matched <xsl: template> – l </xsl: template> uses the match attribute for a whole XML document, or just a section to apply a template to the root node <xsl: template match=“/”> ECA 228 Internet/Intranet Design I

Outputting XHTML l 2 kinds of components in XSLT stylesheet 1. 2. l instructions:

Outputting XHTML l 2 kinds of components in XSLT stylesheet 1. 2. l instructions: how the source XML will be processed 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 l cont … inside the root template – – create structure of

Outputting XHTML l 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 l to output the actual content of a node <xsl:

Outputting content of nodes l to output the actual content of a node <xsl: value-of /> l uses the select attribute to identify a particular node set <xsl: value-of select='/pets/dog/name' /> l since <xsl: value-of /> contains no content, combine opening and closing tags ECA 228 Internet/Intranet Design I

Looping through node sets l l in the previous example, <xsl: value-of /> output

Looping through node sets l l in the previous example, <xsl: value-of /> output only one line of data to process all the data in a node set use <xsl: for-each > l </xsl: for-each > loops through a node set to select every element ECA 228 Internet/Intranet Design I

Looping through node sets l cont … <xsl: for-each> uses a required attribute, select

Looping through node sets l 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 <xsl: for-each > select=“pets/dog” > <xsl: value-of select='name' /> </xsl: for-each > – the path is an XPath expression ECA 228 Internet/Intranet Design I

XPath l a set of rules for defining the parts of an XML document

XPath l 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 pets/dog/name will match all name elements, within all dog elements, within any dogs element, within the root ECA 228 Internet/Intranet Design I

XPath l l cont … relative paths use shortcuts similar to directories uses wildcards

XPath l l cont … relative paths use shortcuts similar to directories uses wildcards ( * ) to indicate an unknown element pets/*/*/name l l will select all name elements 3 levels down XPath is not an XML document XPath uses a variety of operators and functions ECA 228 Internet/Intranet Design I

Filtering l Output from an XML file can be filtered before it is output

Filtering l 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/dog[name=‘Halle’]”> ECA 228 Internet/Intranet Design I

Filtering l cont … Operators for filtering OPERATOR REPRESENTS = equal to != not

Filtering l cont … Operators for filtering OPERATOR REPRESENTS = equal to != not equal to < less than > greater than ECA 228 Internet/Intranet Design I

Sorting l l by default, nodes are processed in the order in which they

Sorting l l by default, nodes are processed in the order in which they appear in the XML document sorting allows you to order them <xsl: sort /> notice the element does not have a separate closing tag ECA 228 Internet/Intranet Design I

Sorting l cont … <xsl: sort> uses the select attribute which indicates the node

Sorting l cont … <xsl: sort> uses the select attribute which indicates the node to sort on <xsl: sort select=“name” /> l l 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” order=“descending” /> ECA 228 Internet/Intranet Design I

Sorting l l cont … <xsl: sort> allows you to sort alphabetically and numerically

Sorting l l cont … <xsl: sort> allows you to sort alphabetically and numerically to sort numerically, use the data-type attribute data-type=“number” otherwise, the default data as a string, which may produce unexpected results <xsl: sort select=“name” data-type=“number” /> ECA 228 Internet/Intranet Design I

Processing node conditionally l process nodes only if certain conditions exist – – –

Processing node conditionally l process nodes only if certain conditions exist – – – l 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 l cont … xsl: if uses the test attribute – contains

Processing node conditionally l cont … xsl: if uses the test attribute – contains an expression to be evaluated l – – 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 < 60” > ECA 228 Internet/Intranet Design I

Testing more than one condition l l xsl: if allows for only one condition

Testing more than one condition l l xsl: if allows for only one condition to be tested to test for several conditions <xsl: choose > l <xsl: otherwise > nested inside xsl: choose, use series of xsl: when to define more than one condition – l <xsl: when > use as many xsl: when elements as necessary to designate default processing, use xsl: otherwise ECA 228 Internet/Intranet Design I

Testing more than one condition <xsl: choose> <xsl: when test="name = 'Halle'"> processing. .

Testing more than one condition <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> ECA 228 Internet/Intranet Design I cont …