ITEC 2336 Internet Application Development XML - XSL
XML A style sheet is linked to an XML document to format the document. XML processor combines style sheet with XML document to display a formatted document. There are two main style sheet languages used with XML: Cascading Style Sheets (CSS) Extensible Style Sheets (XSL)
EXtensible Stylesheet Language (XSL) W3C started to develop XSL because there was a need for an XML-based Stylesheet Language XSL consists of three parts: XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents
What is XSLT? extensible Stylesheet Language for Transformations Language for transforming XML documents A programming language for XML documents A functional language, based on value substitution Augmented with pattern matching And also template substitution to construct output (based on namespaces) Uses XML syntax
Some special transforms XML to HTML for old browsers XML to LaTeX for TeX layout XML to SVG graphs graphs, charts, trees XML to tab-delimited for db/stat packages XML to plain-text occasionally useful XML to FO XSL formatting objects
XML Documents as trees of nodes Root Elements Attributes Text Nodes (not characters) Namespaces Processing Instructions Comments
XML Document order Root Elements Text Nodes -- First -- Occur in order of their starts -- As if children (leaves) Attributes, namespaces -- Attached to element, unordered PIs, comments -- Leaves like text nodes
extensible Stylesheet Language Transformation (XSL) The history of XSL The origins of XSL are in Cascading Style Sheets (CSS), where a "stylesheet" is used to add formatting to an HTML file. The syntax to use a stylesheet in XSLT is similar to the syntax in CSS. XSLT is rule-based For example, a stylesheet could declare that when the XSLT transformation engine finds a 'NAME' element it should add markup by calling the 'NAME' template. <xsl:template match="name">... </xsl:template>
XSL MSXML Parser 3.0 is the XML parser shipped with IE 60 6.0 and dwindows XP. According to Microsoft, MSXML Parser 3.0 is 100% compatible with the official W3C XSLT Recommendation. <xsl:stylesheet> or <xsl:transform> is the root element that declares the document to be an XSL style sheet. The root node of a stylesheet housing the templates y g p to be applied to the XML tree. Note: <xsl:stylesheet> and <xsl:transform> are synonymous either can be used!
XSL The correct way to declare an XSL style sheet is: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/trans form"> or: <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/trans l /1999/XSL/T form">
XSL The <xsl:template> element defines the transformation ti or formatting to be applied. The template s match attribute associates a template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document). The match="/" attribute associates (matches) the template to the root (/) of the XML source document. Match parameters indicate the path to the element in the XML tree. The <xsl:value-of> element inserts the string value of a named node.
XSL The XSL <xsl:for-each> element sets up a loop to iterate through several nodes. Output from an XML file can filtered by adding a criterion i to the select attribute t in the <xsl:for-each> element. For example: <xsl:for-each select="catalog/cd[artist='bob Dylan']"> Legal filter operators are: = (equal)!= (not equal) < less than > greater than
XSL To output the XML file as an XHTML file, and sort it at the same time, simply add a sort element inside the for-each element in your XSL file: The select attribute t indicates what XML element to sort on. The <xsl:if> element contains a template that will be applied only if a specified condition is true. To insert a conditional choose test against the content t of the XML file, simply add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to your XSL document.
XSL Example: <?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xsl" text/xsl href="cdcatalog.xsl"?>
ITEC 2336 Internet Application Development XML - XSL