Extensible Stylesheet Language (XSL)

Embed Size (px)

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

Text of Extensible Stylesheet Language (XSL)

  • *cs336607

    cs336607

  • *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

    cs336607

    cs336607

  • *cs336607

    cs336607

  • *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

    cs336607

    cs336607

  • *XSLT ProcessorsXSLT Processorcs336607

    cs336607

    Text

    WML

    DTD

    XML

    XSL

    XHTML

  • *Web PageLayoutDoc. StructureDataWeb Pages The Whole PictureXMLXSL

    XHTMLStyleKnowledgeCSScs336607

    cs336607

  • *

    Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90

    catalog.xmlcs336607

    cs336607

  • *

    cd catalog This is a cd catalog!

    Valid XML!Commands are XML elements with the namespace xslIncludes XHTML elementscatalog.xslcs336607

    cs336607

  • *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

    cs336607

  • *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

    cs336607

  • *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

    cs336607

  • *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

    cs336607

  • *

    cd catalog This is a cd catalog!

    catalog1.xslcs336607

    cs336607

  • *

    Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90

    catalog1.xmlcs336607

    cs336607

  • *The Result

    cd catalog This is a cd catalog!

    In XALAN, automatically added to cs336607

    cs336607

  • cs336607*

    cs336607

  • *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

    cs336607

  • *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:

    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)

    cs336607

    cs336607

  • *Default TemplatesXSL provides implicit built-in templates that match every element and text nodes

    Templates we write always override these built-in templates (when they match)

    cs336607

    cs336607

  • *

    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)

    cs336607

  • *

    A cd!

    A cd! A cd! A cd! cs336607

    cs336607

  • *

    A cd!

    A cd! A cd! Aretha: Lady Soul Aretha Franklin9.90 cs336607

    cs336607

  • *

    An artist!

    An artist! An artist! cs336607

    cs336607

  • *

    An artist!

    cs336607

    cs336607

  • *

    An artist! The artist is coming back!

    cs336607

    cs336607

  • cs336607*

    cs336607

  • *The Most Frequently Used Elements of XSL

    This element extracts the value of a node from the nodelist located by xpath

    This element loops over all the nodes in the node list located by xpath, , etc.This element is for conditional processingcs336607

    cs336607

  • *

    cd catalog CD catalog []

    Currently selected element is the context (current) node Example 1catalog2.xslcs336607

    cs336607

  • cs336607*

    cs336607

  • *

    cd catalog CD catalog Example 2catalog3.xslcs336607

    cs336607

  • * : (now on sale: $ )

    Example 2 (cont.)Entities replace characters price

  • *

    (Special price!) (Good price!) (Normal price.)

    Example 3 : Switch syntax for conditionscs336607

    cs336607

  • *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

    cs336607

    cs336607

  • *

    cd catalog CD catalog

    CDs are iterated in ascending order of the titlescatalog4.xslcs336607

    cs336607

  • *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

    cs336607

    cs336607

  • *An ExampleIn the following example, we add to each CD entitled t a link to the URL /showcd.jsp?title=t

    :

    cs336607

    cs336607

  • cs336607*

    cs336607

  • *Using

    : /showcd.jsp?title=

    cs336607

    cs336607

  • *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

    cs336607

  • *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

    cs336607

  • *

    Identity TransformationStylesheetcs336607

    cs336607

  • *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

    cs336607

  • *

    cd catalog This is a cd catalog!

    The Original XSL Examplecs336607

    cs336607

  • *

    cd catalog This is a cd catalog!

    Uppercase tag name, unclosed elementNo DOCTYPEThe Original Transformation Resultcs336607

    cs336607

  • *

    Modifying the XSL Examplecs336607

    cs336607

  • cs336607*

    cs336607

  • *

    cd catalog This is a cd catalog!

    META is not insertedThe Transformation Resultcs336607

    cs336607

  • *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

    cs336607

  • ExampleTransform this list of number to be SortedAlternatingly red and blue

    cs336607*

    cs336607

  • cs336607*

    cs336607

  • cs336607*

    cs336607

  • cs336607*Example of Using a Variable

    cs336607

  • Using Variables and FunctionsUsing names, templates can be called directly and serve as functionsIt is possible to use variables and function i