XSL: EXtensible Stylesheet Language - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

XSL: EXtensible Stylesheet Language

Description:

... values, numeric values, date and time comparison, node and QName manipulation, ... Attribute Selects all attributes of the current node ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 29
Provided by: wan145
Category:

less

Transcript and Presenter's Notes

Title: XSL: EXtensible Stylesheet Language


1
XSL EXtensible Stylesheet Language
  • The World Wide Web Consortium (W3C) started to
    develop XSL because there was a need for an
    XML-based Stylesheet Language
  • XSL describes how the XML document should be
    displayed

2
XSL - More Than a Style Sheet Language
  • XSL consists of three parts
  • XSLT - a language for transforming XML documents
  • XPath - a language for navigating in XML
    documents
  • XSL-FO - a language for formatting XML documents

3
XSLT XSL Transformations
  • XSLT is the most important part of XSL
  • XSLT transforms an XML document into another XML
    document, or another type of document that is
    recognized by a browser
  • XSLT uses XPath to navigate in XML documents (to
    find elements and attributes)
  • XSLT became a W3C Recommendation 16. November
    1999
  • XSLT transforms an XML source-tree into an XML
    result-tree.

4
Style Sheet Declaration
  • In XML file
  • lt?xml version"1.0" encoding"Big5"?gt
  • lt?xml-stylesheet type"text/xsl"
    href"hello.xsl"?gt
  • lthellogt Hello, World ! lt/hellogt

5
In xsl file
  • lt?xml version"1.0" encoding"Big5"?gt
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • ltxsltemplate match"/"gt
  • lthtmlgt
  • ltheadgtlttitlegtFirst XSLTlt/titlegtlt/headgt
  • ltbodygt
  • lth2gtltcentergtltxslvalue-of select"hello"
    /gtlt/centergtlt/h2gt
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

6
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • To get access to the XSLT elements, attributes
    and features we must declare the XSLT namespace
    at the top of the document.
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
    points to the official W3C XSLT namespace.
  • If you use this namespace, you must also include
    the attribute version"1.0".

7
ltxsltemplategt Element
  • An XSL style sheet consists of one or more set of
    rules that are called templates.
  • Each template contains rules to apply when a
    specified node is matched

8
  • The match attribute is used to associate a
    template with an XML element.
  • The match attribute can also be used to define a
    template for the entire XML document.
  • The value of the match attribute is an XPath
    expression (i.e. match"/" defines the whole
    document).

9
ltxslvalue-ofgt Element
  • The ltxslvalue-ofgt element can be used to extract
    the value of an XML element and add it to the
    output stream of the transformation
  • The value of the select attribute is an XPath
    expression. An XPath expression works like
    navigating a file system where a forward slash
    (/) selects subdirectories

10
ltxslfor-eachgt Element
  • The ltxslfor-eachgt element allows you to do
    looping in XSLT.
  • The XSL ltxslfor-eachgt element can be used to
    select every XML element of a specified node-set

11
Filtering the Output
  • We can also filter the output from the XML file
    by adding a criterion to the select attribute in
    the ltxslfor-eachgt element.
  • ltxslfor-each select"catalog/cdartist'Bob
    Dylan'"gt
  • Legal filter operators are
  •   (equal)
  • ! (not equal)
  • lt less than
  • gt greater than

12
ltxslsortgt Element
  • The ltxslsortgt element is used to sort the
    output.
  • To sort the output, simply add an ltxslsortgt
    element inside the ltxslfor-eachgt element in the
    XSL file
  • ltxslsort select"title" order"descending"/gt
  • descending
  • ascending
  • The select attribute indicates what XML element
    to sort on

13
ltxslifgt Element
  • To put a conditional if test against the content
    of the XML file, add an ltxslifgt element to the
    XSL document.
  • ltxslif test"expression"gt
  • ... ...some output if the expression is
  • true... ...
  • lt/xslifgt
  • The value of the required test attribute contains
    the expression to be evaluated.

14
ltxslchoosegt Element
  • The ltxslchoosegt element is used in conjunction
    with ltxslwhengt and ltxslotherwisegt to express
    multiple conditional tests.
  • Syntax
  • ltxslchoosegt
  • ltxslwhen test"expression"gt
  • ... some output ...
  • lt/xslwhengt
  • ltxslotherwisegt
  • ... some output ....
  • lt/xslotherwisegt
  • lt/xslchoosegt

15
ltxslapply-templatesgt Element
  • The ltxslapply-templatesgt element applies a
    template to the current element or to the current
    element's child nodes.
  • . with the select attribute to specify the
    current node.

16
XPath
  • XPath is a language for finding information in an
    XML document.
  • XPath is used to navigate through elements and
    attributes in an XML document.
  • XPath is a major element in the W3C's XSLT
    standard
  • and XQuery and XPointer are both built on XPath
    expressions

17
What is XPath
  • XPath is a syntax for defining parts of an XML
    document
  • XPath uses path expressions to navigate in XML
    documents
  • XPath contains a library of standard functions
  • XPath is a major element in XSLT
  • XPath is a W3C Standard

18
XPath
  • XPath uses path expressions to select nodes or
    node-sets in an XML document.
  • XPath includes over 100 built-in functions. There
    are functions for string values, numeric values,
    date and time comparison, node and QName
    manipulation, sequence manipulation, Boolean
    values, and more.

19
XPath Nodes
  • In XPath, there are seven kinds of nodes
    element, attribute, text, namespace,
    processing-instruction, comment, and document
    (root) nodes.

20
XPath Syntax
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltbookstoregt
  • ltbookgt
  • lttitle lang"eng"gtHarry
    Potterlt/titlegt
  • ltpricegt29.99lt/pricegt
  • lt/bookgt
  • ltbookgt
  • lttitle lang"eng"gtLearning
    XMLlt/titlegt
  • ltpricegt39.95lt/pricegt
  • lt/bookgt
  • lt/bookstoregt

21
Selecting Nodes
  • Nodename Selects all child nodes of the
    node/Selects from the root node
  • // Selects nodes in the
    document from the current node that match the
    selection no matter where they are
  • . Selects the current node
  • .. Selects the parent of
    the current node
  • _at_ Selects attributes

22
Selecting Nodes
  • Bookstore Selects all the child nodes of the
    bookstore element
  • /bookstore Selects the root element bookstore
    Note If the path starts with a slash ( / ) it
    always represents an absolute path to an element!
  • Bookstore/book Selects all book elements that
    are children of bookstore
  • //book Selects all book elements no
    matter where they are in the document
  • bookstore//book Selects all book elements
    that are descendant of the bookstore element, no
    matter where they are under the bookstore element
  • //_at_lang Selects all attributes that
    are named lang

23
Predicates
  • Predicates are used to find a specific node or a
    node that contains a specific value.
  • Predicates are always embedded in square brackets.

24
  • /bookstore/book1 Selects the first book
    element that is the child of the bookstore
    element
  • /bookstore/booklast() Selects the last book
    element that is the child of the bookstore
    element
  • /bookstore/booklast()-1 Selects the last but
    one book element that is the child of the
    bookstore element
  • /bookstore/bookposition()lt3 Selects the first
    two book elements that are children of the
    bookstore element
  • //title_at_lang Selects all the title elements
    that have an attribute named lang
  • //title_at_lang'eng' Selects all the title
    elements that have an attribute named lang with a
    value of 'eng
  • /bookstore/bookpricegt35.00 Selects all the
    book elements of the bookstore element that have
    a price element with a value greater than 35.00
  • /bookstore/bookpricegt35.00/title Selects all
    the title elements of the book elements of the
    bookstore element that have a price element with
    a value greater than 35.00

25
Selecting Unknown Nodes
  • XPath wildcards can be used to select unknown XML
    elements
  • Matches any element node
  • _at_ Matches any attribute node
  • node() Matches any node of any kind
  • /bookstore/ Selects all the child nodes of the
    bookstore element
  • // Selects all elements in the document
  • //title_at_ Selects all title elements which
    have any attribute

26
Selecting Several Paths
  • By using the operator in an XPath expression
    you can select several paths
  • //book/title //book/price Selects all the
    title AND price elements of all book elements
  • //title //price Selects all the title AND
    price elements in the document
  • /bookstore/book/title //price Selects all the
    title elements of the book element of the
    bookstore element AND all the price elements in
    the document

27
XPath Axes
  • An axis defines a node-set relative to the
    current node.
  • Ancestor Selects all ancestors (parent,
    grandparent, etc.) of the current node
  • ancestor-or-self Selects all ancestors
    (parent, grandparent, etc.) of the current node
    and the current node itself
  • Attribute Selects all attributes of the current
    node
  • Child Selects all children of the current
    nodedescendantSelects all descendants (children,
    grandchildren, etc.) of the current
    nodedescendant-or-selfSelects all descendants
    (children, grandchildren, etc.) of the current
    node and the current node itselffollowingSelects
    everything in the document after the closing tag
    of the current nodefollowing-siblingSelects all
    siblings after the current nodenamespaceSelects
    all namespace nodes of the current
    nodeparentSelects the parent of the current
    nodeprecedingSelects everything in the document
    that is before the start tag of the current
    nodepreceding-siblingSelects all siblings before
    the current nodeselfSelects the current node

28
ltxslselectgt Element
  • ltxslfor-each select"booklist/book_at_sales'Y'"gt
Write a Comment
User Comments (0)
About PowerShow.com