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 example 6 companies 6 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
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
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
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
ECA 228 Internet/Intranet Design I Outputting XHTML 2 kinds of components in XSLT stylesheet 1. 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 blah...
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 contains no content, combine opening and closing tags
ECA 228 Internet/Intranet Design I Looping through node sets in the previous example, 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
ECA 228 Internet/Intranet Design I Looping through node sets cont … 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 select=“pets/dogs/dog” >
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
ECA 228 Internet/Intranet Design I Filtering cont … OPERATOR REPRESENTS =equal to !=not equal to <less than >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
ECA 228 Internet/Intranet Design I Sorting cont … 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
ECA 228 Internet/Intranet Design I Sorting cont … 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”
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
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 processing...
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
ECA 228 Internet/Intranet Design I Testing more than one condition cont … processing... processing... default processing....