240 likes | 352 Views
ECA 228 Internet/Intranet Design I. Intro to XSL. 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.
E N D
ECA 228 Internet/Intranet Design I Intro to XSL
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 1 2 6 xml 5 3 4 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> <xsl:template match=“/”> ECA 228 Internet/Intranet Design I
Outputting XHTML • 2 kinds of components in XSLT stylesheet • 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 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 pathswill 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 elementwill 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 … • 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 themnotice 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 attributeotherwise, 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 conditionallycont … • 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 < 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:when > <xsl:otherwise > ECA 228 Internet/Intranet Design I
Testing more than one conditioncont … • <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