Xslt-Modified Slides From Dr Sagiv

download Xslt-Modified Slides From Dr Sagiv

of 56

Transcript of Xslt-Modified Slides From Dr Sagiv

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    1/56

    1

    XSLT eXtensible Stylesheet

    Language Transformations

    XSLT eXtensible Stylesheet

    Language Transformations

    Modified Slides from

    Dr. Sagiv

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    2/56

    2

    XSL

    XSL = eXtensible Stylesheet Language

    XSL consists of

    XPath (navigation in documents) XSLT (T fortransformations)

    XSLFO (FO forformatting objects)

    This is a rather complex language for typesetting

    (i.e., preparing text for printing) It will not be taught

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    3/56

    3

    Dark Side of the MoonPink Floyd

    10.90

    Space Oddity

    David Bowie

    9.90

    Aretha: Lady Soul

    Aretha Franklin

    9.90

    An XML document

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    4/56

    4

    XSLT

    XSLT

    Transforming XML documents into

    other XML documents

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    5/56

    5

    XSLT Stylesheet

    An XSLT stylesheet is a program thattransforms an XML document intoanother XML document

    For example:

    Transforming XML to XHTML (HTML thatconforms to XML syntax)

    Transforming an XML document to WML (aformat of XML that cellular phones candisplay)

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    6/56

    6

    A Few Things About XSL

    XSL is a high-level, functional language

    An XSL style sheet is a valid XML

    document Valid with respect to the XSL namespace

    Therefore, commands in XSL are XSL

    elements

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    7/56

    7

    Applying XSLT Stylesheets to

    XML Documents There are three ways of applying anXSLT stylesheet to an XML document

    Directly applying an XSLTprocessorto theXML document and the XSLT stylesheet

    Calling an XSLT processor from within a(Java) program

    Adding to the XML document a link to theXSL stylesheet and letting the browser dothe transformation

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    8/56

    8

    Using an XSL Processor

    XSL ProcessorXML

    document

    XSL

    stylesheet

    Result is

    either anXML, HTML

    or text

    document

    java org.apache.xalan.xslt.Process-IN myXmlFile.xml -XSL myXslFile.xsl

    -OUT myOutputFile.html

    Directly applying the Xalan XSL processor

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    9/56

    9

    Letting a Browser Perform the

    Transformation

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    10/56

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    11/56

    11

    How Does XSLT Work?

    An XSL stylesheet is a collection oftemplates that are applied to source nodes(i.e., nodes of the given XML document)

    Each template has a match attribute thatspecifies to which source nodes thetemplate can be applied

    The currentsource node isprocessedbyapplying a template that matches this node

    Processing always starts at the root (/)

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    12/56

    12

    Templates

    A template has the form

    ...

    The content of a template consists of XML elements and text that are copied to the

    result XSL elements that are actually instructions

    The pattern syntax is a subset of XPath

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    13/56

    13

    Hello World

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    14/56

    14

    Applying a browser to catalog.xml(catalog.xml has a link to catalog.xsl)

    >htmlbodyh1>Hello World/body/html10 are retrieved

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    36/56

    36

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    37/56

    37

    TheElement

    Theelement is used inconjunction withandto express test with

    multiple conditions There can be manyinside

    anelement, but there

    should be a singleinside anelement

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    38/56

    38

    Using

    To insert a conditional choose against thecontent of the XML file, simply add the

    ,, andelements to your XSL

    document like this:

    ... some code ...

    ... some code ....

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    39/56

    39

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    40/56

    40

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    41/56

    41

    Applying Templates Recursively

    The following example shows how toapply templates recursively

    Generally, it is possible (but not in thisexample) that more than one templatematches the current source node

    The specification (www.w3.org/TR/xslt)

    describes (Section 5.5) which templateshould be chosen for application

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    42/56

    42

    A CD Catalog

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    43/56

    43

    Title:


  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    44/56

    44

    Artist:


  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    45/56

    45

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    46/56

    46

    Is Recursive Application of

    Templates ReallyN

    eeded? The output of the previous example canalso be generated by an XSL stylesheetthat uses only one template that matches

    the root (and does not use the element)

    However, some tasks can only be done

    by applying templates recursively This typically happens when the structure of

    the source XML document is not known

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    47/56

    47

    For example

    Suppose that we want to write an XSLstylesheet that generates an exact copyof the source XML document

    It is rather easy to do it when the structure ofthe source XML document is known

    Can we write an XSL stylesheet that does

    it for every possible XML document? Yes! (see next slide)

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    48/56

    48

    Identity Transformation

    Stylesheet

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    49/56

    49

    TheElement

    ......

    Tells in what format the outputshould be: xml/html/text

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    50/56

    50

    Some Other XSL Elements

    The element allows to insertfree text in the output

    The element creates a copy

    of the current node The element is used to

    create a comment node in the result tree

    There are more elements and functions:look in the specification!(www.w3.org/TR/xslt)

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    51/56

    51

    My CD Collection

    Titles:

    , , and

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    52/56

    52

    (contd)

    !

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    53/56

    53

    Title

    Artist

    My CD Collection

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    54/56

    54

    (contd)

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    55/56

    55

    W3Schools Tutorial on XSLT

    The W3Schools XSLT Tutorial has(among other things) tables that list all theelements and functions of XSLT

    It also has some details aboutimplementations

    Some browsers may not implement all

    features or may implement some featuresdifferently from the specifications

  • 8/4/2019 Xslt-Modified Slides From Dr Sagiv

    56/56

    56

    Summary

    XSLT is a high-level transformationlanguage

    Create core output once in XML format(using Servlets, JSP, etc.)

    Use XSLT to transform the core output asneeded