XSL Stands for Extensible Style Sheet Language

  • Upload
    yadna01

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    1/20

    XSL stands for EXtensible Stylesheet Language, and is a style sheet languagefor XML documents.

    XSLT stands for XSL Transformations. In this tutorial you will learn how to useXSLT to transform XML documents into other formats, like XHTML.

    You might remember from a previous lesson that we can use a language calledExtensible Styles Language XSL to format our XML documents. XSLactually comes in two parts - a transformation language (XSLT) and a

    formatting language using formatting objects.

    This section of the XML tutorial covers XSLT.

    XSLT, which stands for Extensible Styles Language Transformations, enables

    you to transform XML documents into another form. For example, you can takeyour XML document, combine it with HTML/CSS, and it will look completelydifferent when viewing it in your user agent/browser.

    Processing a Transformation

    A transformation can take place in one of three locations:

    On the server On the client (for example, your web browser) With a standalone program

    The examples in this tutorial will use the client for transforming the XMLdocuments.

    All XSLT documents need to be well-formed and valid XML documents, so

    you need to follow the same syntax rules that apply to any other XMLdocument.

    As well as ensuring that your XSLT documents are valid XML, you need toensure they are valid XSLT documents. Here's what you need to remember

    when creating XSLT documents.

    XML Version

    XSL documents are also XML documents and so we should include the XML

    version in the document's prolog. We should also set the standalone attribute to"no" as we now rely on an external resource (i.e. the external XSL file).

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    2/20

    XSL Root Element

    Then we open the root element - xsl:stylesheet. The root element needs to

    include the XSL version as well as the XSL namespace (hence the xsl prefix andthe xmlns... part).

    XSL Namespace Prefix

    All XSL elements in your XSLT document must include the XSL prefix.

    Syntax:

    Example:

    ....

    XSLT Element

    XSLT is all about being able to select one or more nodes from your XMLdocument and transforming or replacing its content with something else. A

    node could be any of the following: elements, attributes, text, namespaces,processing-instructions, and comments.

    The element is what you use to select a node from your XML

    document and transform its contents.

    To select an element, you use the match attribute. To transform its

    contents, you simply place the new content between the opening

    () and closing () tags.

    In this case, I'm selecting the root node (i.e. tutorials). By selecting this

    node, the template element tells the XSLT processor how to transform the

    output. What I'm doing here is telling the processor to replace the root node

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    3/20

    (i.e. the whole XML document) with what I've written between the tags.

    In this case, I have written the contents of an HTML document inside the

    tags. When a user views any XML document that uses this

    XSL document, they will simply see the line "New content..." and thebrowser's title bar will read "My XSLT Example".

    My XSLT Example

    New content...

    Selecting the Root Node

    In the example above, we selected the "tutorials" node which happens to be

    the root node of our XML document. Another way of selecting the root node

    is to use a forward slash in place of the node's name. The following exampleresults in the same output as the above example.

    Example:

    My XSLT Example

    New content...

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    4/20

    XSLT Element

    We've already learned that the element allows us to select anynode in our XML document and transform its contents. You'll probably be happy to

    learn that we can also use this element to work with the children of that node.

    The XSLT element allows us to determine where the

    content of its children appear on our transformed document.

    Here, we are using two elements; one for the root node, and one

    for its children.

    We have placed the element within the

    element for the root node. Doing this applies the results of our other element.

    Cool Tutorials

    Hey, check out these tutorials!

    XSLT Element

    The element allows you to retrieve the value from a node.

    When using the element, you use the select attribute to specifywhich node you want to retrieve data from.

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    5/20

    XSLT Element

    The XSLT element allows you to loop through multiple nodes thatmatch the selection criteria. This in turn, enables you to retrieve data from thesenodes.

    For example, imagine if our XML file had two elements called "name" - each underthe "tutorial" element. Like this:

    XML TutorialHomer Flinstone

    http://www.quackit.com/xml/tutorial

    HTML Tutorial

    Fred Simpsonhttp://www.quackit.com/html/tutorial

    To extract data from both "name" elements, we can use inconjunction with .

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    6/20

    Example

    Here, we use to loop through each "tutorial" element, and to sort by the "name" node. We then use the to extract

    data from the "name" node.

    The Requirement

    food.xml

    Agar818

    0.51280

    Asparagus11040

    Cabbage01014

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    7/20

    Potato21.521460

    Pumpkin610.5150

    Yam30.520.5550

    Zucchini

    1.51.50.555

    Abalone001400

    Barramundi00

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    8/20

    2390

    Apple152.50250

    Kiwi Fruit7.52.5

    0150

    Oatbran621471400

    Wheatgerm1.510.570

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    9/20

    vegetable.xls

    Food Item Carbs (g) Fiber (g)

    Fat (g) Energy (kj)

    Now, imagine we're only interested in the vegetables - we only want to

    display the food that have a type attribute of "vegetable". And, we also want

    to display it nicely formatted in an HTML table. Something like this:

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    10/20

    It Started with XSL

    XSL stands for EXtensible Stylesheet Language.

    The World Wide Web Consortium (W3C) started to develop XSL because therewas a need for an XML-based Stylesheet Language.

    CSS = Style Sheets for HTML

    HTML uses predefined tags, and the meaning of each tag is well understood.

    The tag in HTML defines a table - and a browser knows how to display it.

    Adding styles to HTML elements are simple. Telling a browser to display an element in a special font orcolor, is easy with CSS.

    XSL = Style Sheets for XML

    XML does not use predefined tags (we can use any tag-names we like), and therefore the meaning ofeach tag is not well understood.

    A tag could mean an HTML table, a piece of furniture, or something else - and a browserdoes not know how to display it.

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    11/20

    XSL describes how the XML document should be displayed!

    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

    XSLT is a language for transforming XML documents into XHTML documents or toother XML documents.

    XPath is a language for navigating in XML documents.

    What You Should Already Know

    Before you continue you should have a basic understanding of the following:

    HTML / XHTML

    XML / XML Namespaces

    XPath

    If you want to study these subjects first, find the tutorials on our Home page.

    What is XSLT?

    XSLT stands for XSL Transformations

    XSLT is the most important part of XSL

    XSLT transforms an XML document into another XML document

    XSLT uses XPath to navigate in XML documents

    XSLT is a W3C Recommendation

    XSLT = XSL Transformations

    XSLT is the most important part of XSL.

    XSLT is used to transform an XML document into another XML document, or another type of documentthat is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming eachXML element into an (X)HTML element.

    With XSLT you can add/remove elements and attributes to or from the output file. You can alsorearrange and sort elements, perform tests and make decisions about which elements to hide anddisplay, and a lot more.

    http://www.w3schools.com/default.asphttp://www.w3schools.com/default.asp
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    12/20

    A common way to describe the transformation process is to say that XSLT transforms an XMLsource-tree into an XML result-tree.

    XSLT Uses XPath

    XSLT uses XPath to find information in an XML document. XPath is used to navigate through elementsand attributes in XML documents.

    If you want to study XPath first, please read our XPath Tutorial.

    How Does it Work?

    In the transformation process, XSLT uses XPath to define parts of the source document that shouldmatch one or more predefined templates. When a match is found, XSLT will transform the matchingpart of the source document into the result document.

    Correct Style Sheet Declaration

    The root element that declares the document to be an XSL style sheet is or.

    Note: and are completely synonymous and either can be used!

    The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:

    or:

    To get access to the XSLT elements, attributes and features we must declare the XSLT namespace atthe top of the document.

    The xmlns:xsl="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".

    Start with a Raw XML DocumentWe want to transform the following XML document ("cdcatalog.xml") into XHTML:

    Empire BurlesqueBob Dylan

    http://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/xpath/default.asp
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    13/20

    USAColumbia10.901985

    ..

    Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking on alink) - The XML document will be displayed with color-coded root and child elements. A plus (+) or

    minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure.To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source"from the browser menu.

    Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select "ViewPage Source". The XML document will then be displayed with color-coded root and child elements.

    Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select "Frame" /"View Source". The XML document will be displayed as plain text.

    View "cdcatalog.xml"

    Create an XSL Style Sheet

    Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:

    My CD Collection

    TitleArtist

    View "cdcatalog.xsl"

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog.xslhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog.xsl
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    14/20

    Link the XSL Style Sheet to the XML Document

    Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):

    Empire BurlesqueBob DylanUSAColumbia10.901985

    ..

    XSLT Element

    An XSL style sheet consists of one or more set of rules that are called templates.

    A template contains rules to apply when a specified node is matched.

    The Element

    The element is used to build templates.

    The match attribute is used to associate a template with an XML element. The match attribute canalso be used to define a template for the entire XML document. The value of the match attribute is anXPath expression (i.e. match="/" defines the whole document).

    Ok, let's look at a simplified version of the XSL file from the previous chapter:

    Example

    My CD CollectionTitleArtist

  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    15/20

    ..

    Try it yourself

    Example Explained

    Since an XSL style sheet is an XML document, it always begins with the XML declaration: .

    The next element, ,defines that this document is an XSLT style sheet document(along with the version number and XSLT namespace attributes).

    The element defines a template. The match="/" attribute associates the templatewith the root of the XML source document.

    The content inside the element defines some HTML to write to the output.

    The last two lines define the end of the template and the end of the style sheet.

    The result from this example was a little disappointing, because no data was copied from the XMLdocument to the output. In the next chapter you will learn how to use the element toselect values from the XML elements.

    XSLT Element

    The Element

    The element can be used to extract the value of an XML element and add it to theoutput stream of the transformation:

    Example

    My CD CollectionTitle

    http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    16/20

    Artist

    Try it yourself

    Example Explained

    Note: The select attribute in the example above, contains an XPath expression. An XPath expressionworks like navigating a file system; a forward slash (/) selects subdirectories.

    The result from the example above was a little disappointing; only one line of data was copied fromthe XML document to the output. In the next chapter you will learn how to use the element to loop through the XML elements, and display all of the records.

    XSLT Element

    The element allows you to do looping in XSLT.

    The Element

    The XSL element can be used to select every XML element of a specified node-set:

    Example

    My CD Collection

    TitleArtist

    http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex2http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex2
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    17/20

    Try it yourself

    Note: The value of the select attribute is an XPath expression. An XPath expression works likenavigating a file system; where a forward slash (/) selects subdirectories.

    Filtering the Output

    We can also filter the output from the XML file by adding a criterion to the select attribute in the element.

    Legal filter operators are:

    = (equal)

    != (not equal)

    < less than

    > greater than

    Take a look at the adjusted XSL style sheet:

    Example

    My CD CollectionTitleArtist

    http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex3http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex3
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    18/20

    XSLT Element

    The element is used to sort the output.

    Where to put the Sort Information

    To sort the output, simply add an element inside the element in the XSLfile:

    Example

    My CD CollectionTitleArtist

    Try it yourself

    Note: The select attribute indicates what XML element to sort on.

    XSLT Element

    The element is used to put a conditional test against the content of theXML file.

    http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=tryxsl_sorthttp://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=tryxsl_sort
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    19/20

    The Element

    To put a conditional if test against the content of the XML file, add an element to the XSLdocument.

    Syntax

    ...some output if the expression is true...

    Where to Put the Element

    To add a conditional test, add the element inside the element in the XSL file:

    Example

    My CD Collection

    Title

    Artist

    Try it yourself

    http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ifhttp://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_if
  • 8/3/2019 XSL Stands for Extensible Style Sheet Language

    20/20

    Note: The value of the required test attribute contains the expression to be evaluated.

    The code above will only output the title and artist elements of the CDs that has a price that is higherthan 10