XSLTSlide 2Very simple exampleThe .xsl fileFinding the message textPutting it togetherHow XSLT worksWhere XSLT can be usedModern browsersxsl:value-ofxsl:for-eachFiltering outputFilter detailsBut it doesn’t work right!xsl:ifxsl:choosexsl:sortxsl:textCreating tags from XML dataCreating tags--solution 1Creating tags--solution 2ModularizationBook examplexsl:apply-templatesWhen templates are ignoredApplying templates to childrenCalling named templatesTemplates with parametersThoughts on XSLThe EndJan 13, 2019XSLT2XSLTXSLT stands for Extensible Stylesheet Language TransformationsXSLT is used to transform XML documents into other kinds of documents--usually, but not necessarily, XHTMLXSLT uses two input files:The XML document containing the actual dataThe XSL document containing both the “framework” in which to insert the data, and XSLT commands to do so3Very simple exampleFile data.xml: <?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="render.xsl"?><message>Howdy!</message>File render.xsl: <?xml version="1.0"?><xsl:stylesheet version="1.0” xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- one rule, to transform the input root (/) --> <xsl:template match="/"> <html><body> <h1><xsl:value-of select="message"/></h1> </body></html> </xsl:template></xsl:stylesheet>4The .xsl fileAn XSLT document has the .xsl extension The XSLT document begins with: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/ XSL/Transform">Contains one or more templates, such as: <xsl:template match="/"> ... </xsl:template>And ends with: </xsl:stylesheet>5Finding the message textThe template <xsl:template match="/"> says to select the entire fileYou can think of this as selecting the root node of the XML treeInside this template, <xsl:value-of select="message"/> selects the message childAlternative Xpath expressions that would also work:./message/message/text() (text() is an XPath function)./message/text()6Putting it togetherThe XSL was: <xsl:template match="/"> <html><body> <h1><xsl:value-of select="message"/></h1> </body></html> </xsl:template>The <xsl:template match="/"> chooses the rootThe <html><body> <h1> is written to the output fileThe contents of message is written to the output fileThe </h1> </body></html> is written to the output fileThe resultant file looks like: <html><body> <h1>Howdy!</h1> </body></html>7How XSLT worksThe XML text document is read in and stored as a tree of nodesThe <xsl:template match="/"> template is used to select the entire treeThe rules within the template are applied to the matching nodes, thus changing the structure of the XML treeIf there are other templates, they must be called explicitly from the main templateUnmatched parts of the XML tree are not changedAfter the template is applied, the tree is written out again as a text document8Where XSLT can be usedWith an appropriate program, such as Xerces, XSLT can be used to read and write filesA server can use XSLT to change XML files into HTML files before sending them to the clientA modern browser can use XSLT to change XML into HTML on the client sideThis is what we will mostly be doing in this classMost users seldom update their browsersIf you want “everyone” to see your pages, do any XSL processing on the server sideOtherwise, think about what best fits your situation9Modern browsersInternet Explorer 6 best supports XMLNetscape 6 supports some of XMLInternet Explorer 5.x supports an obsolete version of XMLIE5 is not good enough for this courseIf you must use IE5, the initial PI is different (you can look it up if you ever need it)10xsl:value-of<xsl:value-of select="XPath expression"/> selects the contents of an element and adds it to the output streamThe select attribute is requiredNotice that xsl:value-of is not a container, hence it needs to end with a slashExample (from an earlier slide): <h1> <xsl:value-of select="message"/> </h1>11xsl:for-eachxsl:for-each is a kind of loop statementThe syntax is <xsl:for-each select="XPath expression"> Text to insert and rules to apply </xsl:for-each> Example: to select every book (//book) and make an unordered list (<ul>) of their titles (title), use: <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title"/> </li> </xsl:for-each> </ul>12Filtering outputYou can filter (restrict) output by adding a criterion to the select attribute’s value: <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title[../author='Terry Pratchett']"/> </li> </xsl:for-each> </ul>This will select book titles by Terry Pratchett13Filter detailsHere is the filter we just used: <xsl:value-of select=" title[../author='Terry Pratchett' ]"/>author is a sibling of title, so from title we have to go up to its parent, book, then back down to authorThis filter requires a quote within a quote, so we need both single quotes and double quotesLegal filter operators are: = != < >Numbers should be quoted, but apparently don’t have to be14But it doesn’t work right!Here’s what we did: <xsl:for-each select="//book"> <li> <xsl:value-of select="title[../author='Terry Pratchett']"/> </li> </xsl:for-each>This will output <li> and </li> for every book, so we will get empty bullets for authors other than Terry PratchettThere is no obvious way to solve this with just xsl:value-of15xsl:ifxsl:if allows us to include content if a given condition (in the test attribute) is trueExample: <xsl:for-each select="//book"> <xsl:if test="author='Terry Pratchett'"> <li> <xsl:value-of select="title"/> </li> </xsl:if> </xsl:for-each>This does work correctly!16xsl:chooseThe xsl:choose ... xsl:when ... xsl:otherwise construct is XML’s equivalent of Java’s switch ... case ... default statementThe syntax is:<xsl:choose> <xsl:when test="some condition"> ... some code ... </xsl:when> <xsl:otherwise> ... some code ... </xsl:otherwise></xsl:choose>• xsl:choose is often used within an xsl:for-each loop17xsl:sortYou can place an xsl:sort inside an xsl:for-eachThe attribute of the sort tells what field to sort onExample: <ul> <xsl:for-each select="//book"> <xsl:sort select="author"/> <li> <xsl:value-of select="title"/> by
View Full Document