XSLT - PowerPoint PPT Presentation

About This Presentation
Title:

XSLT

Description:

XSLT Brent P. Christie Major USMC XSLT Overview What is XSLT? XSL is the Extensible Style Language. It has two parts: the transformation language and the formatting ... – PowerPoint PPT presentation

Number of Views:168
Avg rating:3.0/5.0
Slides: 41
Provided by: Bren156
Learn more at: https://faculty.nps.edu
Category:
Tags: xslt | xslt

less

Transcript and Presenter's Notes

Title: XSLT


1
XSLT
  • Brent P. Christie
  • Major USMC

2
XSLT Overview
  • What is XSLT?
  • XSL is the Extensible Style Language.
  • It has two parts the transformation language and
    the formatting language.
  • In this lecture we are concerned with XSL
    transformations, or XSLT.
  • The formatting language will be discussed in a
    separate brief.
  • XSLT provides a syntax for defining rules that
    transform an XML document to another document.
  • For example, to an HTML document.
  • An XSLT style sheet consists primarily of a set
    of template rules that are used to transform
    nodes matching some patterns.

3
XSLT Overview
  • Example of XML document
  • lt?xml version1.0?gt
  • lt?xml-stylesheet typetext/xml
    hrefplanets.xsl?gt
  • ltPLANETSgt
  •  
  • ltPLANETgt
  • ltNAMEgtMercurylt/NAMEgt
  • ltMASS UNITS(Earth 1)gt.0553lt/MASSgt
  • ltDAY UNITSdaysgt58.65lt/DAYgt
  • ltRADIUS UNITSmilesgt1516lt/RADIUSgt
  • ltDENSITY UNITS(Earth
    1)gt.983lt/DENSITYgt
  • ltDISTANCE UNITSmillion
    milesgt43.4lt/DISTANCEgtlt!--At perihelion--gt
  • lt/PLANETgt
  •  

4
XSLT Overview
  • XML document example cont
  • ltPLANETgt
  • ltNAMEgtVenuslt/NAMEgt
  • ltMASS UNITS(Earth 1)gt.815lt/MASSgt
  • ltDAY UNITSdaysgt116.75lt/DAYgt
  • ltRADIUS UNITSmilesgt3716lt/RADIUSgt
  • ltDENSITY UNITS(Earth
    1)gt.943lt/DENSITYgt
  • ltDISTANCE UNITSmillion
    milesgt66.8lt/DISTANCEgtlt!--At perihelion--gt
  • lt/PLANETgt
  •  
  • ltPLANETgt
  • ltNAMEgtEarthlt/NAMEgt
  • ltMASS UNITS(Earth 1)gt1lt/MASSgt
  • ltDAY UNITSdaysgt1lt/DAYgt
  • ltRADIUS UNITSmilesgt2107lt/RADIUSgt
  • ltDENSITY UNITS(Earth 1)gt1lt/DENSITYgt
  • ltDISTANCE UNITSmillion
    milesgt128.4lt/DISTANCEgtlt!--At perihelion--gt
  • lt/PLANETgt
  •  

5
XSLT Overview
  • Example of a style sheet planet.xsl
  • lt?xml version1.0?gt
  • ltxslstylesheet version1.0 xmlnsxslhttp//ww
    w.w3.org/1999/XSL/Transformgt
  •  
  • ltxsltemplate matchPLANETSgt
  • ltHTMLgt
  • ltxslapply-templates/gt
  • lt/HTMLgt
  • lt/xsltemplategt
  •  
  • ltxsltemplate matchPLANETgt
  • ltPgt
  • ltxslvalue-of selectNAME/gt
  • lt/Pgt
  • lt/xsltemplategt
  •  
  • lt/xslstylesheetgt

6
XSLT Overview
  • Result
  • ltHTMLgt
  •  
  • ltPgtMercurylt/Pgt
  •  
  • ltPgtVenuslt/Pgt
  •  
  • ltPgtEarthlt/Pgt
  •  
  • lt/HTMLgt

7
XSLT Overview
  • The xml-stylesheet element in the XML instance
    references an XSL style sheet.
  • In general, children of the stylesheet element in
    a stylesheet are templates.
  • A template specifies a pattern the template is
    applied to nodes in the XML source document that
    match this pattern.
  • Note the pattern / matches the root node of
    the document, we will see this later
  • In the transformed document, the body of the
    template element replaces the matched node in the
    source document.
  • In addition to text, the body may contain further
    XSL terms, e.g.
  • xslvalue-of extracts data from selected
    sub-nodes.

8
XSLT Overview
  • We have an XML document and the style sheet (or
    rules) to transform it. So, how do you transform
    the document?.
  • You can transform documents in three ways
  • In the server. A server program, such as a Java
    servlet, can use a style sheet to transform a
    document automatically and serve it to the
    client. Example, XML Enabler, which is a servlet
    that youll find at XML for Java Web site,
    www.alphaworks.ibm.com/tech/xml4j
  • In the client. An XSL-enabled browser may
    convert XML downloaded from the server to HTML,
    prior to display. Currently Internet Explorer
    supports a subset of XSLT.
  • In a standalone program. XML stored in or
    generated from a database, say, may be manually
    converted to HTML before placing it in the
    servers document directory.
  • In any case, a suitable program takes an XML
    document as input, together with an XSLT
    style-sheet.

9
Format of Style Sheet
  • You guessed it, XSLT style sheet is itself an XML
    document.
  • We will be using the XSLT elements from the
    namespace http//www.w3.org/1999/XSL/Transform
    for this brief
  • As a matter of convention we use the prefix xsl
    for this namespace.
  • The document root in an XSLT style sheet is an
    xslstylesheet element, e.g.
  • ltxslstylesheet version"1.0"
  • xmlnsxsl"http//www.w3.org/1999/XSL/
    Transform" gt
  • . . .
  • lt/xslstylesheetgt
  • A synonym for xslstylesheet is xsltransform.
  • Several kinds of element can be nested inside
    xslstylesheet, but by far the most important is
    the xsltemplate element.

10
The xsltemplate element
  • When you match or select nodes, a template tells
    the XSLT processor how to transform the node for
    output
  • So all our templates will have the form
  • ltxsltemplate
    matchpatterngt
  • template body
  • lt/xsltemplategt
  • The pattern is an Xpath expression describing the
    nodes to which the template can be applied.
  • The processor scans the input document for nodes
    matching this pattern, and replaces them with the
    text included in the template body.
  • In a nutshell, this explains the whole operation
    of XSLT.

11
XPATH
  • The XML Path Language, or XPath, is a language
    for addressing parts of an XML document.
  • The patterns and other node selections appearing
    in XSLT rules are represented using XPath syntax.
  • Including the match element of xsltemplate or
    the select element of xslvalue-of.
  • Weve seen that you can use the match attribute
    to find nodes by name, child elements(s),
    attributes. Can even find a descendant.
  • XPath does all this and more with the select
    attribute.
  • Finding nodes by parent or sibling elements, as
    well as much more involved tests.
  • More of a true language than the expressions you
    can use with match attribute
  • Return not only lists of nodes, but also Boolean,
    string, and numeric values

12
XPath
  • It is an essential part of XSLT, and also
    XPointer (as wel as being used in XML schema).
  • In simple cases an XPath expression looks like a
    UNIX path name, with nested directory names
    replaced by nested element names
  • / is the root element of a document
  • expressions may be absolute (relative to the
    root) or relative to some context node

13
Types of XPath expressions
  • Xpath expressions evaluate to one of four
    possible types of thing
  • A node-set a collection of nodes in the XML
    document. See below for the description of
    node.
  • A boolean value true or false.
  • A number always represented internally as 64-bit
    IEEE 754 floating-point double-precision format,
    although they may be written and used as an
    integer.
  • A string.
  • In the end we are interested in Xpath expressions
    that evaluate to a node-set, although other
    expression types will appear.

14
Xpath Node
  • XSL transformations accept a document tree as
    input and produce a tree as output. From the XSLT
    point of view, documents are trees built of
    nodes, and there are seven types of nodes that
    can appear in a node-set here are those nodes,
    and how XSLT processors treat them
  • Node Description
  • Document root Is the very start of the
    document, /
  • Attribute Holds the value of an attribute after
    entity references have been expanded and
    surrounding whitespace has been trimmed
  • Comment Holds the text of a comment, not
    including lt!-- and --gt
  • Element Consists of all character data in the
    element, which includes character data in any
    of the children of the element
  • Namespace Holds the namespaces URI
  • Processing instruction Holds the text of the
    processing instruction, which does not
    include lt? and ?gt
  • Text Holds the text of the node

15
Location paths
  • The most important kind of Xpath expressionthe
    one that gives XPath its nameis the location
    path.
  • absolute location path path from the root node
  • relative starting with current node, called
    context node.
  • In general a location path consists of a series
    of location steps separated by the slash /.
    Made up of
  • axis, a node test, and zero or more predicates.
  • As noted earlier, the most common example of a
    location stepanalogous to a UNIX directory
    nameis an XML element name.
  • Actually this common case is an example of what
    is called abbreviated syntax.
  • To be systematic, we will describe the general,
    unabbreviated syntax for location paths first

16
Parts of a location step
  • An individual location step has three logical
    parts
  • The axisa keyword which, loosely speaking,
    describes the dimension this location step
    takes us into.
  • Simple examples are child and attribute which,
    respectively, say that this step enters the set
    of children or the set of attributes of an
    element.
  • A node testthis is typically an element or
    attribute name, selecting within the chosen axis.
    It may also, less specifically, be a node type.
  • Zero or more predicates, which use arbitrary
    XPath expressions to further refine the set of
    selected nodes.
  • The unabbreviated syntax for a location step is
  • axis node-test predicate1
    predicate2 . . .

17
Axes
  • Any location step starts from some context node
    the axis is relative to this node.
  • The available axes are
  • childcontains the children of the context node.
  • descendentcontains children and all descendents
    of children.
  • parentcontains the parent of the context node.
  • ancestorcontains parent of the context node and
    ancestors of parent. All the way back to and
    including root node.
  • attributecontains the attributes of the context
    node.
  • followingall following element nodes, in
    document order.
  • precedingall preceding element nodes, in
    document order.
  • following-sibling, preceding-siblingelements at
    the same syntactic level.
  • namespacecontains namespace nodes of context
    node.
  • self, descendent-or-self, ancestor-or-self

18
The NodeTest
  • After choosing the axis, we refine the selection
    with a node test.
  • The most common cases for a the node-test field
    are likely to be
  • an element or attribute name, selecting nodes in
    the axis with the given name, or
  • the wildcard, , selecting all nodes of the
    principal type for this axis (typically, all
    element nodes, or all attribute nodes if axis is
    attribute).
  • The node-test field may also be a node type
    expression
  • comment(), text(), processing-instruction(),
    node()
  • Optionally, the processing-instruction() function
    may include a literal string specifying a
    particular type of instruction.

19
Predicates
  • The node test is optionally followed by a series
    of predicate expressions.
  • Each expression appears in s.
  • Syntax of the expressions will be briefly
    discussed later. Some examples appear in the
    next few slides.
  • The predicates are computed successively to
    further filter the set of selected nodesafter
    each predicate is applied, the selected node set
    is reduced to exclude those elements for which
    the expression evaluates to false.
  • Following examples are taken from the XML Path
    Language specification.

20
Example location paths
  • child para
  • para element children of the context node
  • child
  • All element children of the context node
  • child text()
  • All text node children of the context node
  • child node()
  • All children, regardless of node type
  • attribute name
  • The name attribute of the context node
  • attribute
  • All attributes of the context node
  • decendent para
  • para element descendents of the context node
  • ancestor div
  • div element ancestors of the context node

21
More complex examples
  • child chapter/descendent para
  • para element descendents of chapter element
    children of the context node.
  • child /child para
  • All para element grandchildren of the context
    node.
  • /descendent para
  • All para elements in this document.
  • child para position() 1
  • First para element child of the context node.
  • child para position() last()
  • Last para element child of the context node.
  • child para position() gt 1
  • All para element children of the context node,
    except the first.
  • /child doc/child chapter position()
    5/child section
  • section elements of 5th chapter element of root
    doc element.
  • child para attribute type warning
    position() 5
  • 5th para child of context node having type
    attribute value warning.

22
Predicate expressions
  • The axis in a location step defines a node-set,
    which is then filtered by the node test to
    produce a reduced node set.
  • A predicate is evaluated for each element of the
    node set selected so far
  • The context node for the predicate expression is
    the element being filtered (not the context node
    for the location step as a whole!)
  • The context set for the predicate expression is
    the node set currently being filtered.

23
Context Position
  • Various functions are available in Xpath
    expressions, e.g.
  • last() returns the size of the context set for
    the expression.
  • position() the position of the context node in
    the context set.
  • If the Xpath expression that appears in the
    predicate of a path step evaluates to numeric
    type, it is converted to true if its value is
    equal to position().
  • Otherwise it is converted to false.
  • Thus, by definition, the location step
  • child para
    5
  • is an alternative to
  • child para position()
    5
  • i.e., the 5th para child.

24
Booleans
  • In general an Xpath expression is converted to a
    booleanif context demandsby the following
    rules
  • A non-zero number converts to true, zero converts
    to false.
  • A non-empty string converts to true true, empty
    to false.
  • A non-empty node converts to true, empty to
    false.
  • According to the third rule, in the location
    step
  • child
    section child para
  • the predicate is true if the context node
    for the predicate (i.e. the section node) has at
    least one para child.
  • Operators and, or, not() are available.

25
Comparisons
  • Numeric and string comparisons in Xpath
    predicates follows obvious rules.
  • Comparisons involving node sets are defined to be
    true if the comparison would hold true for the
    string-value of any elements of the sets
    involved.
  • Note, the string value of an element node is a
    concatenation of the string values of its
    children.
  • For example, in
  • child para attribute type
    warning
  • the predicate is true iff the node set
    attribute type includes an element with
    string-value warning, i.e. if the para child
    has an attribute with value warning.

26
Unions
  • The operator forms the union of two node
    sets.
  • e.g.
  • child chapter child section child
    para
  • selects chapters that directly contain a
    section or a para.

27
Abbreviated syntax for paths
  • Together, the following abbreviations allow the
    UNIX-like path syntax seen earlier
  • The axis selector child can always be omitted
    a node test alone implicitly refers to the child
    axis.
  • The location step . is short for self
    node().
  • The location step .. is short for parent
    node().
  • Other useful abbreviations are
  • The axis selector attribute can be abbreviated
    to _at_.
  • // is short for /descendent-or-self node()/
  • e.g //para is short for any para element in the
    document.

28
An input document
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • lt?xml-stylesheet type"text/xml" href"eg.xsl"?gt
  • ltplanetsgt
  • ltplanetgt
  • ltnamegtMercurylt/namegt
  • ltmassgt0.0553lt/massgt
  • ltday units"days"gt58.65lt/daygt
  • ltradius units"miles"gt1516lt/radiusgt
  • ltdensitygt0.983lt/densitygt
  • lt/planetgt
  • ltplanetgt
  • ltnamegtVenuslt/namegt
  • ltmassgt0.815lt/massgt
  • ltday units"days"gt116.75lt/daygt
  • ltradius units"miles"gt3716lt/radiusgt
  • ltdensitygt0.943lt/densitygt
  • lt/planetgt
  • ltplanetgt
  • ltnamegtEarthlt/namegt

Simplified version of example from the Inside
XML book (complete with astronomical errors).
29
Using an empty style sheet
  • Consider the example where there are no templates
    explicitly specified, eg.xsl has the form
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltxslstylesheet version"1.0"
  • xmlnsxsl"http//www.w3.org/1999/XSL/Trans
    form" gt
  • lt/xslstylesheetgt
  • The transformation of the input document is
  • Mercury0.055358.6515160.983Venus0.815116.7537160.9
    43Earth1121071
  • i.e. just the concatenated string values in
    all text nodes.
  • This happens because there is a default template
    rule
  • ltxsltemplate matchtext()gt
  • ltxslvalue-of select./gt
  • lt/xsltemplategt

30
Templates without embedded XSLT
  • Now consider a single template, with no embedded
    XSLT commands
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltxslstylesheet version"1.0
  • xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform" gt
  • ltxsltemplate match"planet"gt
  • ltpgtplanet discoveredlt/pgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt
  • The transformation of the input document is
  • lt?xml version"1.0" encoding"UTF-16"?gtltpgtplanet
  • discoveredlt/pgtltpgtplanet discoveredlt/pgtltpgtplanet
    discoveredlt/pgt
  • This is valid HTML, but not very readable
    (as text).
  • We can add the command
  • ltxsloutput indent"yes"/gt
  • to the xslstylesheet element to get
    prettier output formatting.

31
The xslapply-templates element
  • Suppose a second template matching the planets
    element is added
  • ltxsltemplate
    match"planet"gt
  • ltpgtplanet
    discoveredlt/pgt
  • lt/xsltemplategt
  • ltxsltemplate
    match"planets"gt
  • lth1gtAll Known
    Planetslt/h1gt
  • lt/xsltemplategt
  • The output now only contains the header
  • lth1gtAll Known
    Planetslt/h1gt
  • not the planet discovered messages from
    processing the nested planet elements.
  • Once a match is found, nested elements are not
    processed unless there is an explicit
    ltxslapply-templatesgt instruction
  • ltxsltemplate
    match"planets"gt
  • lth1gtAll Known
    Planetslt/h1gt
  • ltxslapply-template
    s/gt
  • lt/xsltemplategt

32
The xslvalue-of element
  • We can now match arbitrary nodes in the source
    document, but we dont yet have a way to extract
    data from those nodes.
  • To do this we need the xslvalue-of element,
    e.g.
  • ltxsltemplate match"planet"gt
  • ltpgtplanet ltxslvalue-of
    select"name"/gt discoveredlt/pgt
  • lt/xsltemplategt
  • ltxsltemplate match"planets"gt
  • lth1gtAll Known Planetslt/h1gt
  • ltxslapply-templates/gt
  • lt/xsltemplategt
  • We now get the more interesting output
  • lth1gtAll Known
    Planetslt/h1gt
  • ltpgtplanet Mercury
    discoveredlt/pgt
  • ltpgtplanet Venus
    discoveredlt/pgt
  • ltpgtplanet Earth
    discoveredlt/pgt

33
Selections
  • The select attribute of the xslvalue-of element
    is a general Xpath expression.
  • Its resultwhich may be a node set or other
    allowed valueis converted to a string and
    included in the output.
  • For example, the selection can be an attribute
    node, a set of elements, or it could be the
    result of a numeric computation.
  • If the selection is a set of elements, the text
    contents of all the element bodies, including
    nested elements, are concatenated and returned as
    the value.

34
Xpath expressions in attributes
  • Suppose we want to generate an XML element in the
    output with an attribute whose value is computed
    from source data.
  • One might be tempted to try a template like
  • ltplanet name "ltxslvalue-of
    select'name'/gt" gt
  • Status discovered
  • lt/planetgt
  • This is ill-formed XML we cannot have an XML
    element as an attribute value.
  • Instead s can be used in an attribute value to
    surround an Xpath expression
  • ltplanet name "name" gt
  • Status discovered
  • lt/planetgt
  • The Xpath expression name is evaluated exactly as
    for a select attribute, and interpolated into the
    attribute value string.

35
The xslelement element
  • For similar reasons we cannot use ltxslvalue-ofgt
    to compute an expression that is used as the name
    of an element in the generated file.
  • Instead one can use instead the xslelement
    element.
  • These can optionally include nested xslattribute
    elements (as their first children)
  • ltxsltemplate match"planet"gt
  • ltxslelement name"name"gt
  • ltxslattribute name"distance"gt
  • ltxslvalue-of select"distance"/gt
  • lt/xslattributegt
  • Status discovered
  • lt/xslelementgt
  • lt/xsltemplategt
  • When this template matches a planet, it generates
    an XML element whose name is the planet, with a
    distance attribute.

36
A Table of Planets
  • ltxsltemplate match"planets"gt
  • lthtmlgtltbodygt
  • lth1gtAll Known Planetslt/h1gt
  • lttable width"100" align"center"
    border"1"gt
  • lttrgtltthgtnamelt/thgtltthgtmasslt/thgtltthgtdens
    itylt/thgt
  • ltthgtradiuslt/thgtlt/trgt
  • ltxslapply-templates/gt lt!-- rows of
    table --gt
  • lttrgt
  • lttdgtAVERAGESlt/tdgt
  • lttdgt
  • ltxslvalue-of
  • select"sum(planet/density
    ) div count(planet)"/gt
  • lt/tdgt
  • lttdgtlt/tdgt
  • lttdgtlt/tdgt
  • lt/trgt
  • lt/tablegt
  • lt/bodygtlt/htmlgt
  • lt/xsltemplategt

37
A row of the table
  • ltxsltemplate match"planet"gt
  • lttrgt
  • lttdgtltxslvalue-of select"name"/gtlt/tdgt
  • lttdgtltxslvalue-of select"mass"/gtlt/tdgt
  • lttdgtltxslvalue-of select"density"/gtlt/
    tdgt
  • lttdgtltxslvalue-of select"radius"/gtlt/t
    dgt
  • lt/trgt
  • lt/xsltemplategt

38
The display
39
References
  • Inside XML, Chapter 13 XSL Transformations.
  • XSL Transformations (XSLT), version 1.0
  • http//www.w3.org/TR/xslt
  • XML Path Language (XPath), version 1.0
  • http//www.w3.org/TR/xpath
  • Nancy McCracken, Ozgur Balsoy
  • http//aspen.csit.fsu.edu/webtech/xml/

40
XML and Related Acronyms
  • Document Type Definition (DTD), which defines the
    tags and their relationships
  • Extensible Style Language (XSL) style sheets,
    which specify the presentation of the document
  • Cascading Style Sheets(CSS) less powerful
    presentation technology without tag mapping
    capability
  • XPATH which specifies location in document
  • XLINK and XPOINTER which defines link-handling
    details
  • Resource Description Framework (RDF), document
    metadata
  • Document Object Model (DOM), API for converting
    the document to a tree object in your program for
    processing and updating
  • Simple API for XML (SAX), serial access
    protocol, fast-to-execute protocol for processing
    document on the fly
  • XML Namespaces, for an environment of multiple
    sets of XML tags
  • XHTML, a definition of HTML tags for XML
    documents (which are then just HTML documents)
  • XML schema, offers a more flexible alternative to
    DTD
Write a Comment
User Comments (0)
About PowerShow.com