Extensible Stylesheet Language (XSL)

  • Published on
    19-Jan-2016

  • View
    24

  • Download
    0

DESCRIPTION

Extensible Stylesheet Language (XSL). XSL. XSL is a standard that consists of three parts: XPath (navigation in documents) XPath was taught in the DB course, so it will not be taught XSLT (transformation of documents) XSLFO (FO for formatting objects) - PowerPoint PPT Presentation

Transcript

<ul><li><p>*cs336607</p><p>cs336607</p></li><li><p>*XSLXSL is a standard that consists of three parts:XPath (navigation in documents)XPath was taught in the DB course, so it will not be taughtXSLT (transformation of documents)XSLFO (FO for formatting objects)This is a rather complex language for typesetting (i.e., preparing text for printing)It will not be taught </p><p>cs336607</p><p>cs336607</p></li><li><p>*cs336607</p><p>cs336607</p></li><li><p>*XSLTXSLT is a language for transforming XML documents into other XML documentsFor example, XHTML, RSS, KML, GML, MathML, WMLCan also transform XML to text documents, e.g., SQL programsAn XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input documents</p><p>cs336607</p><p>cs336607</p></li><li><p>*XSLT ProcessorsXSLT Processorcs336607</p><p>cs336607</p><p>Text</p><p>WML</p><p>DTD</p><p>XML</p><p>XSL</p><p>XHTML</p></li><li><p>*Web PageLayoutDoc. StructureDataWeb Pages The Whole PictureXMLXSL</p><p>XHTMLStyleKnowledgeCSScs336607</p><p>cs336607</p></li><li><p>*</p><p> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 </p><p>catalog.xmlcs336607</p><p>cs336607</p></li><li><p>*</p><p> cd catalog This is a cd catalog! </p><p>Valid XML!Commands are XML elements with the namespace xslIncludes XHTML elementscatalog.xslcs336607</p><p>cs336607</p></li><li><p>*Applying XSL Stylesheets to XMLThere are several ways of applying an XSL stylesheet to an XML document:Directly applying an XSLT processor to the XML document and the XSL stylesheetCalling an XSLT processor from within a programAdding to the XML document a link to the XSL stylesheet and letting the browser do the transformationThe resulting XHTML document is shown instead of the original XMLcs336607</p><p>cs336607</p></li><li><p>*Processing XSL in JavaYou can use the XALAN package of Apache in order to process XSL transformationsjava org.apache.xalan.xslt.Process -IN myXmlFile.xml -XSL myXslFile.xsl -OUT myOutputFile.htmlcs336607</p><p>cs336607</p></li><li><p>*How Does XSLT Work?An XSL stylesheet is a collection of templates that are applied to source nodes (i.e., nodes of the given XML)Each template has a match attribute that specifies to which source nodes the template can be appliedEach source node has at a template that matches itThe current source node is processed by applying a template that matches this nodeWhen processing a node, it is possible (but not necessary) to recursively process other nodes, e.g., the children of the processed node The XSLT processor processes the document root (/)cs336607</p><p>cs336607</p></li><li><p>*TemplatesA template has the form ...The content of a template consists of XML elements (e.g., XHTML) and text that are copied to the resultXSL elements () that are actually instructionsThe pattern syntax is a subset of XPathcs336607</p><p>cs336607</p></li><li><p>*</p><p> cd catalog This is a cd catalog! </p><p>catalog1.xslcs336607</p><p>cs336607</p></li><li><p>*</p><p> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 </p><p>catalog1.xmlcs336607</p><p>cs336607</p></li><li><p>*The Result</p><p> cd catalog This is a cd catalog! </p><p>In XALAN, automatically added to cs336607</p><p>cs336607</p></li><li><p>cs336607*</p><p>cs336607</p></li><li><p>*Examples of Match Attributesmatch="cd", All elements with tag name cdmatch="//cd", match="/catalog/cd/artist"All matches of the absolute XPathmatch="cd/artist"All artist nodes that have a cd parent match="catalog//artist"All artist nodes that have a catalog ancestormatch="cd[@country='UK']/artist" cs336607</p><p>cs336607</p></li><li><p>*Processing starts by applying a template to the root If no specified template matches the root, then one is inserted by default (see the next slide)The XSL stylesheet must specify explicitly whether templates should be applied to descendants of a node It is done by putting inside a template the instruction:</p><p>In xpath, the current processed node is used as the context nodeWithout the select attribute, this instruction processes all the children of the current node (including text nodes)</p><p>cs336607</p><p>cs336607</p></li><li><p>*Default TemplatesXSL provides implicit built-in templates that match every element and text nodes</p><p>Templates we write always override these built-in templates (when they match)</p><p>cs336607</p><p>cs336607</p></li><li><p>*</p><p> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 cs336607In XALAN and in IE (it yields an empty page in Firefox)</p><p>cs336607</p></li><li><p>*</p><p> A cd! </p><p>A cd! A cd! A cd! cs336607</p><p>cs336607</p></li><li><p>*</p><p> A cd!</p><p>A cd! A cd! Aretha: Lady Soul Aretha Franklin9.90 cs336607</p><p>cs336607</p></li><li><p>*</p><p> An artist! </p><p>An artist! An artist! cs336607</p><p>cs336607</p></li><li><p>*</p><p> An artist! </p><p>cs336607</p><p>cs336607</p></li><li><p>*</p><p> An artist! The artist is coming back! </p><p>cs336607</p><p>cs336607</p></li><li><p>cs336607*</p><p>cs336607</p></li><li><p>*The Most Frequently Used Elements of XSL</p><p>This element extracts the value of a node from the nodelist located by xpath</p><p>This element loops over all the nodes in the node list located by xpath, , etc.This element is for conditional processingcs336607</p><p>cs336607</p></li><li><p>*</p><p> cd catalog CD catalog [] </p><p>Currently selected element is the context (current) node Example 1catalog2.xslcs336607</p><p>cs336607</p></li><li><p>cs336607*</p><p>cs336607</p></li><li><p>*</p><p> cd catalog CD catalog Example 2catalog3.xslcs336607</p><p>cs336607</p></li><li><p>* : (now on sale: $ ) </p><p>Example 2 (cont.)Entities replace characters price</p></li><li><p>*</p><p> (Special price!) (Good price!) (Normal price.) </p><p>Example 3 : Switch syntax for conditionscs336607</p><p>cs336607</p></li><li><p>*The ElementThe element is used to sort the list of nodes that are looped over by the elementThus, the must appear inside the elementThe looping is done in sorted order</p><p>cs336607</p><p>cs336607</p></li><li><p>*</p><p> cd catalog CD catalog </p><p>CDs are iterated in ascending order of the titlescatalog4.xslcs336607</p><p>cs336607</p></li><li><p>*Setting Values in AttributesThe element cannot be used within attribute valueHowever, we can insert expressions into attribute values, by putting the expression inside curly braces ({})Alternatively, we can use in order to construct XML elements</p><p>cs336607</p><p>cs336607</p></li><li><p>*An ExampleIn the following example, we add to each CD entitled t a link to the URL /showcd.jsp?title=t</p><p> : </p><p>cs336607</p><p>cs336607</p></li><li><p>cs336607*</p><p>cs336607</p></li><li><p>*Using </p><p> : /showcd.jsp?title= </p><p>cs336607</p><p>cs336607</p></li><li><p>*On XSL CodeTypically, an XSLT program can be written in several, very different waysTemplates can sometime replace loops and vice versaConditions can sometimes be replaced with XPath predicates (e.g., in the select attribute) and vice versaA matter of convenience and elegancycs336607</p><p>cs336607</p></li><li><p>*On Recursive TemplatesIt is not always possible to avoid recursive templatesThat is, use only the template of the rootSuppose that we want to write an XSL stylesheet that generates a copy of the source documentIt is rather easy to do it when the structure of the source XML document is knownCan we write an XSL stylesheet that does it for every possible XML document?Yes! (see next slide)cs336607</p><p>cs336607</p></li><li><p>*</p><p>Identity TransformationStylesheetcs336607</p><p>cs336607</p></li><li><p>*Generating Valid XHTMLBy default, the documents that XSL stylesheets generate are not valid XHTMLNext, we will show how XSL stylesheet can be changed in order to generate valid XHTMLcs336607</p><p>cs336607</p></li><li><p>*</p><p> cd catalog This is a cd catalog! </p><p>The Original XSL Examplecs336607</p><p>cs336607</p></li><li><p>*</p><p> cd catalog This is a cd catalog! </p><p>Uppercase tag name, unclosed elementNo DOCTYPEThe Original Transformation Resultcs336607</p><p>cs336607</p></li><li><p>*</p><p> Modifying the XSL Examplecs336607</p><p>cs336607</p></li><li><p>cs336607*</p><p>cs336607</p></li><li><p>*</p><p> cd catalog This is a cd catalog! </p><p>META is not insertedThe Transformation Resultcs336607</p><p>cs336607</p></li><li><p>*Some Other XSL ElementsThe element inserts free text in the outputThe creates a copy of the specified nodes (deep copy, i.e., copies the entire subtree)The creates a copy of the specified nodes (does not copy children or attributes)The element creates a comment node in the result treeThe element defines a variable (local or global) that can be used within the programcs336607</p><p>cs336607</p></li><li><p>ExampleTransform this list of number to be SortedAlternatingly red and blue</p><p>cs336607*</p><p>cs336607</p></li><li><p>cs336607*</p><p>cs336607</p></li><li><p>cs336607*</p><p>cs336607</p></li><li><p>cs336607*Example of Using a Variable</p><p>cs336607</p></li><li><p>Using Variables and FunctionsUsing names, templates can be called directly and serve as functionsIt is possible to use variables and function in XSL to perform computationsReturned values of functions can be set to variables</p></li><li><p>Computing Fibonacci Numbers</p><p>8</p></li><li><p> Fibonacci The Fibonacci number at position is </p></li><li><p>Transforming XML to XHTML, in the BrowserUse JavaScriptCheck what is the current browser before applying the transformation (different browsers use a slightly different DOM)cs336607*</p><p>cs336607</p></li><li><p>cs336607* function loadXMLDoc(fname) { var xmlDoc; // code for IE if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } </p><p> // code for Mozilla, Firefox, Opera, etc. else if (document.implementation &amp;&amp; document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } xmlDoc.async=true; xmlDoc.load(fname); return(xmlDoc); } XML to XHTML in Browser</p><p>cs336607</p></li><li><p>cs336607*function displayResult() { xml=loadXMLDoc("cdcatalog.xml"); xsl=loadXMLDoc("cdcatalog.xsl"); // code for IE if (window.ActiveXObject) { ex=xml.transformNode(xsl); document.getElementById("example").innerHTML=ex; } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation &amp;&amp; document.implementation.createDocument) { xsltProcessor=new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); resultDocument = xsltProcessor.transformToFragment(xml,document); document.getElementById("example").appendChild(resultDocument); } } </p><p>XML to XHTML in Browser</p><p>cs336607</p></li><li><p>Applying Xalan from Java ProgramsThe following example illustrate how to apply Xalan on a given XML and XSL from within a Java programcs336607*</p><p>cs336607</p></li><li><p>cs336607*// Instantiate a DocumentBuilderFactory. javax.xml.parsers.DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); </p><p>// Use the DocumentBuilderFactory to provide access to a DocumentBuilder. javax.xml.parsers.DocumentBuilder dBuilder = dfactory.newDocumentBuilder(); </p><p>// Use the DocumentBuilder to parse the XML input. org.w3c.dom.Document inDoc = dBuilder.parse("foo.xml");Generate a document builderParse the XML document</p><p>cs336607</p></li><li><p>cs336607*// Generate a Transformer. javax.xml.transform.Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource("foo.xsl")); // Create an empy DOMResult object for the output. javax.xml.transform.dom.DOMResult domResult = new javax.xml.transform.dom.DOMResult(); // Perform the transformation. transformer.transform(new javax.xml.transform.dom.DOMSource(inDoc), domResult); // Now you can get the output Node from the DOMResult. org.w3c.dom.Node node = domResult.getNode();Generate a transformerCreate an empty DOM resultApply the transformationGet the output node</p><p>cs336607</p></li><li><p>LinksW3C Recommendation: http://www.w3.org/TR/xslthttp://www.xml.com/pub/a/2000/08/holman/index.htmlhttp://www.w3schools.com/xsl/default.asphttp://xml.apache.org/xalan-j/apidocs/</p><p>cs336607*</p><p>cs336607</p></li></ul>

Recommended

View more >