XSL Languages tutorial

download XSL Languages tutorial

of 45

Transcript of XSL Languages tutorial

  • 8/7/2019 XSL Languages tutorial

    1/43

    1 XSL Languages .................................................................................................................3

    It Started with XSL ......................................................................................................... 3

    1.1 CSS = HTML Style Sheets ........................................................................................ 31.2 XSL = XML Style Sheets .......................................................................................... 4

    1.3 XSL - More Than a Style Sheet Language ................................................................ 4

    1.4 This Tutorial is About XSLT .....................................................................................42 Introduction to XSLT ........................................................................................................4

    2.1 What You Should Already Know .............................................................................. 4

    2.2 What is XSLT? .......................................................................................................... 42.3 XSLT = XSL Transformations .................................................................................. 5

    2.4 XSLT Uses XPath ...................................................................................................... 5

    2.5 How Does it Work? ................................................................................................... 5

    2.6 XSLT is a Web Standard ........................................................................................... 53 XSLT Browsers ................................................................................................................ 5

    3.1 Firefox 1.0.2 ............................................................................................................... 5

    3.2 Mozilla 1.7.8 .............................................................................................................. 6

    3.3 Netscape 8 .................................................................................................................. 63.4 Opera 8 .......................................................................................................................6

    3.5 Internet Explorer 6 ..................................................................................................... 63.6 Internet Explorer 5 ..................................................................................................... 6

    3.7 Internet Explorer MSXML Parser ............................................................................. 6

    4 XSLT Transformation ....................................................................................................6

    4.1 Correct Style Sheet Declaration .................................................................................64.2 Start with a Raw XML Document ............................................................................. 7

    4.3 Create an XSL Style Sheet .......................................................................................11

    4.4 Link the XSL Style Sheet to the XML Document ...................................................124.5 My CD Collection ....................................................................................................12

    5 XSLT Templates .......................................................................................................... 135.1 The Element .................................................................................... 135.2 My CD Collection ....................................................................................................14

    6 XSLT - The Element .............................................................................. 14

    6.1 The Element .................................................................................... 146.2 My CD Collection ....................................................................................................15

    7 XSLT - The Element .............................................................................. 15

    7.1 The Element .................................................................................... 15

    7.2 My CD Collection ....................................................................................................167.3 Filtering the Output ..................................................................................................16

    7.4 My CD Collection ....................................................................................................17

    8 XSLT - The Element ..................................................................................... 178.1 Where to put the Sort Information ...........................................................................17

    8.2 My CD Collection ....................................................................................................18

    9 XSLT - The Element .........................................................................................199.1 The Element ...............................................................................................19

    9.1.1 Syntax ...............................................................................................................19

    9.2 Where to Put the Element ..........................................................................19

    9.3 My CD Collection ....................................................................................................20

    1

  • 8/7/2019 XSL Languages tutorial

    2/43

    10 XSLT - The Element ..............................................................................20

    10.1 The Element .....................................................................................20

    10.1.1 Syntax .............................................................................................................2010.2 Where to put the Choose Condition .......................................................................20

    10.3 My CD Collection ..................................................................................................21

    10.4 Another Example ................................................................................................... 2210.5 My CD Collection ..................................................................................................23

    11 XSLT - The Element ................................................................23

    11.1 The Element ......................................................................2411.2 My CD Collection ..................................................................................................24

    12 XSLT - On the Client ....................................................................................................24

    12.1 A JavaScript Solution ............................................................................................25

    12.2 The XML File and the XSL File ............................................................................2512.3 Transforming XML to XHTML in the Browser ....................................................26

    13 XSLT - On the Server ...................................................................................................27

    13.1 A Cross Browser Solution .....................................................................................27

    13.2 The XML File and the XSLT File .........................................................................2713.3 Transforming XML to XHTML on the Server ......................................................28

    14 XSLT - Editing XML ....................................................................................................2814.1 Open, Edit and Save XML .....................................................................................29

    14.2 The XML File and the XSL File ............................................................................29

    14.3 The ASP File ..........................................................................................................30

    15 XML Editors .................................................................................................................3215.1 XML is Text-based ................................................................................................ 32

    15.2 Why Not Notepad? ................................................................................................ 32

    15.3 Why an XML Editor? ............................................................................................3215.4 XML Editors ..........................................................................................................32

    15.5 Altova's XMLSPY .................................................................................................33

    16 XSLT Elements Reference ........................................................................................... 3316.1 XSLT Elements ......................................................................................................33

    17 XSLT Functions ............................................................................................................35

    17.1 XSLT Functions .....................................................................................................3518 The current() Function .................................................................................................. 35

    18.1 Definition and Usage .............................................................................................35

    18.2 Syntax ....................................................................................................................36

    18.2.1 Example 1 .......................................................................................................3619 The document() Function ..............................................................................................36

    19.1 Definition and Usage .............................................................................................36

    19.2 Syntax ....................................................................................................................3619.3 Parameters ..............................................................................................................37

    20 The element-available() Function ................................................................................. 37

    20.1 Definition and Usage .............................................................................................3720.2 Syntax ....................................................................................................................37

    20.3 Parameters ..............................................................................................................37

    20.3.1 Example 1 .......................................................................................................37

    21 The format-number() Function ..................................................................................... 38

    2

  • 8/7/2019 XSL Languages tutorial

    3/43

    21.1 Definition and Usage .............................................................................................38

    21.2 Syntax ....................................................................................................................38

    21.3 Parameters ..............................................................................................................3821.3.1 Example 1 .......................................................................................................39

    22 The function-available() Function ................................................................................ 39

    22.1 Definition and Usage .............................................................................................3922.2 Syntax ....................................................................................................................39

    22.3 Parameters ..............................................................................................................39

    22.3.1 Example 1 .......................................................................................................3923 The generate-id() Function ........................................................................................... 40

    23.1 Definition and Usage .............................................................................................40

    23.2 Syntax ....................................................................................................................40

    23.3 Parameters ..............................................................................................................4023.3.1 Example 1 .......................................................................................................40

    24 The key() Function ........................................................................................................41

    24.1 Definition and Usage .............................................................................................41

    24.2 Syntax ....................................................................................................................4124.3 Parameters ..............................................................................................................41

    24.3.1 Example 1 .......................................................................................................4125 The system-property() Function ................................................................................... 42

    25.1 Definition and Usage .............................................................................................42

    25.2 Syntax ....................................................................................................................42

    25.3 Parameters ..............................................................................................................4225.3.1 Example 1 .......................................................................................................42

    26 The unparsed-entity-uri() Function ..............................................................................43

    26.1 Definition and Usage .............................................................................................4326.2 Syntax ....................................................................................................................43

    26.3 Parameters ..............................................................................................................43

    1 XSL Languages

    It started with XSL and ended up with XSLT, XPath, and XSL-FO.

    It Started with XSL

    XSL stands for EXtensible Stylesheet Language.

    The World Wide Web Consortium (W3C) started to develop XSL because there was a need for anXML-based Stylesheet Language.

    1.1 CSS = HTML Style Sheets

    HTML uses predefined tags and the meaning of the tags are well understood.

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

    3

  • 8/7/2019 XSL Languages tutorial

    4/43

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

    1.2 XSL = XML Style Sheets

    XML does not use predefined tags (we can use any tag-names we like), and the meaning of thesetags are not well understood.

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

    XSL describes how the XML document should be displayed!

    1.3 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

    1.4 This Tutorial is About XSLT

    The rest of this tutorial is about XSLT - the language for transforming XML documents.

    But you can also study our XPath Tutorial and our XSL-FO Tutorial.

    2 Introduction to XSLT

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

    XPath is a language for navigating in XML documents.

    2.1 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.

    2.2 What is XSLT?

    XSLT stands for XSL Transformations

    4

    http://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/xslfo/default.asphttp://www.w3schools.com/xslfo/default.asphttp://www.w3schools.com/default.asphttp://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/xslfo/default.asphttp://www.w3schools.com/default.asp
  • 8/7/2019 XSL Languages tutorial

    5/43

    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

    2.3 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 ofdocument that is recognized by a browser, like HTML and XHTML. Normally XSLT does this bytransforming each XML 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.

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

    2.4 XSLT Uses XPath

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

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

    2.5 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.

    2.6 XSLT is a Web Standard

    XSLT became a W3C Recommendation 16. November 1999.

    To read more about the XSLT activities at W3C, please read our W3C Tutorial.

    3 XSLT BrowsersNot all Internet browsers have full support for XML and XSLT.

    3.1 Firefox 1.0.2

    This web browser has support for XML and XSLT (and CSS).

    5

    http://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/w3c/w3c_xsl.asphttp://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/w3c/w3c_xsl.asp
  • 8/7/2019 XSL Languages tutorial

    6/43

    3.2 Mozilla 1.7.8

    Mozilla includes Expat for XML parsing and has support to display XML + CSS. Mozilla also has somesupport for Namespaces.

    Mozilla is available with an XSLT implementation.

    3.3 Netscape 8

    Netscape 8 is based on the Mozilla engine. Same XML / XSLT support as Mozilla.

    3.4 Opera 8

    This web browser supports XML + CSS.

    3.5 Internet Explorer 6

    Microsoft's web browser has full XML support, including Namespaces, Style sheets in CSS, and XSLT1.0.

    The XML Parser 3.0 in Internet Explorer 6.0 and Windows XP is based on both the W3C XSLT 1.0and the W3C XPath 1.0 Recommendations.

    If you are serious about learning XSLT you should upgrade to Internet Explorer 6.0.

    3.6 Internet Explorer 5

    XSLT in Internet Explorer 5 is NOT compatible with the official W3C XSL Recommendation.

    3.7 Internet Explorer MSXML Parser

    MSXML Parser 2.5 is the XML parser that is shipped with Windows 2000 and IE 5.5.

    MSXML Parser 3.0 is the XML parser that is shipped with IE 6.0 and Windows XP.

    According to Microsoft, the MSXML Parser 3.0 is 100% compatible with the official W3C XSLTRecommendation.

    4 XSLT Transformation

    Example study: How to transform XML into XHTML using XSLT.

    The details of this example will be explained in the next chapter.

    4.1 Correct Style Sheet Declaration

    6

  • 8/7/2019 XSL Languages tutorial

    7/43

    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 namespaceat the top of the document.

    The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLTnamespace. If you use this namespace, you must also include the attribute version="1.0".

    4.2 Start with a Raw XML Document

    We want to transform the following XML document ("cdcatalog.xml") into XHTML:

    Empire BurlesqueBob DylanUSAColumbia10.901985

    ...

    Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking ona link) - The XML document will be displayed with color-coded root and child elements. A plus (+) orminus sign (-) to the left of the elements can be clicked to expand or collapse the elementstructure. 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"

    7

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog.xml
  • 8/7/2019 XSL Languages tutorial

    8/43

    -

    --

    Empire Burlesque

    Bob DylanUSAColumbia

    10.901985

    -

    Hide your heartBonnie Tyler

    UKCBS Records

    9.901988

    -

    Greatest HitsDolly Parton

    USARCA

    9.901982

    -

    Still got the bluesGary Moore

    UKVirgin records

    10.201990

    -

    ErosEros Ramazzotti

    EUBMG

    9.901997

    - One night only

    Bee Gees

    UK

    Polydor

    10.90

    1998

    - Sylvias Mother

    Dr.Hook

    UK

    CBS

    8.10

    1973

    8

    http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23
  • 8/7/2019 XSL Languages tutorial

    9/43

    - Maggie May

    Rod StewartUK

    Pickwick8.50

    1990

    - Romanza

    Andrea BocelliEU

    Polydor10.80

    1996

    - When a man loves a woman

    Percy SledgeUSA

    Atlantic8.70

    1987

    - Black angel

    Savage RoseEU

    Mega10.90

    1995

    - 1999 Grammy Nominees

    ManyUSA

    Grammy10.20

    1999

    - For the good times

    Kenny Rogers

    UK

    Mucik Master

    8.70

    1995

    - Big Willie style

    Will Smith

    USAColumbia

    9.90

    1997

    - Tupelo Honey

    Van Morrison

    UK

    Polydor

    8.20

    9

    http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23
  • 8/7/2019 XSL Languages tutorial

    10/43

    1971

    - Soulsville

    Jorn HoelNorway

    WEA

    7.901996

    - The very best of

    Cat StevensUK

    Island8.90

    1990

    - Stop

    Sam BrownUK

    A and M8.90

    1988

    - Bridge of Spies

    T`PauUK

    Siren7.90

    1987

    - Private Dancer

    Tina TurnerUK

    Capitol

    8.90

    1983

    - Midt om natten

    Kim Larsen

    EU

    Medley

    7.80

    1983

    -

    Pavarotti Gala ConcertLuciano Pavarotti

    UK

    DECCA

    9.90

    1991

    - The dock of the bay

    Otis Redding

    USA

    10

    http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23
  • 8/7/2019 XSL Languages tutorial

    11/43

    Atlantic7.90

    1987

    - Picture book

    Simply Red

    EUElektra7.20

    1985

    - Red

    The CommunardsUK

    London7.80

    1987

    - Unchain my heart

    Joe CockerUSA

    EMI8.20

    1987

    4.3 Create an XSL Style Sheet

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

    My CD CollectionTitleArtist

    View "cdcatalog.xsl"

    11

    http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xslhttp://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xml#%23http://www.w3schools.com/xsl/cdcatalog.xsl
  • 8/7/2019 XSL Languages tutorial

    12/43

    4.4 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

    ...

    If you have an XSLT compliant browser it will nicely transform your XML into XHTML!

    View the result!

    4.5 My CD Collection

    Title Artist

    Empire Burlesque Bob DylanHide your heart Bonnie Tyler

    Greatest Hits Dolly Parton

    Still got the blues Gary Moore

    Eros Eros Ramazzotti

    One night only Bee Gees

    Sylvias Mother Dr.Hook

    Maggie May Rod Stewart

    Romanza Andrea Bocelli

    When a man loves a woman Percy Sledge

    Black angel Savage Rose

    1999 Grammy Nominees Many

    For the good times Kenny Rogers

    Big Willie style Will Smith

    Tupelo Honey Van Morrison

    Soulsville Jorn Hoel

    The very best of Cat Stevens

    Stop Sam Brown

    Bridge of Spies T`Pau

    Private Dancer Tina Turner

    12

    http://www.w3schools.com/xsl/cdcatalog_with_xsl.xmlhttp://www.w3schools.com/xsl/cdcatalog_with_xsl.xml
  • 8/7/2019 XSL Languages tutorial

    13/43

    Midt om natten Kim Larsen

    Pavarotti Gala Concert Luciano Pavarotti

    The dock of the bay Otis Redding

    Picture book Simply Red

    Red The Communards

    Unchain my heart Joe Cocker

    The details of the example above will be explained in the next chapters!

    5 XSLT Templates

    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.

    5.1 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 isan XPath expression (i.e. match="/" defines the whole document).

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

    My CD CollectionTitleArtist

    ..

    Since an XSL style sheet is an XML document itself, 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).

    13

  • 8/7/2019 XSL Languages tutorial

    14/43

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

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

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

    The result of the transformation above will look like this:

    5.2 My CD Collection

    Title Artist

    . .

    View the XML file,View the XSL file, and View the result

    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 to select values from theXML elements.

    6 XSLT - The Element

    The element is used to extract the value of a selected node.

    6.1 The Element

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

    My CD Collection

    TitleArtist

    14

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_ex1.xslhttp://www.w3schools.com/xsl/cdcatalog_ex1.xslhttp://www.w3schools.com/xsl/cdcatalog_with_ex1.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_ex1.xslhttp://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
  • 8/7/2019 XSL Languages tutorial

    15/43

    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.

    The result of the transformation above will look like this:

    6.2 My CD Collection

    Title Artist

    Empire Burlesque Bob Dylan

    View the XML file,View the XSL file, and View the result

    The result from this example was also a little disappointing, because only one line of data wascopied from the XML document to the output.

    In the next chapter you will learn how to use the element to loop through the XMLelements, and display all of the records.

    7 XSLT - The Element

    The element allows you to do looping in XSLT.

    7.1 The Element

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

    My CD CollectionTitleArtist

    15

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_ex2.xslhttp://www.w3schools.com/xsl/cdcatalog_ex2.xslhttp://www.w3schools.com/xsl/cdcatalog_with_ex2.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_ex2.xslhttp://www.w3schools.com/xsl/cdcatalog_with_ex2.xml
  • 8/7/2019 XSL Languages tutorial

    16/43

    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.

    The result of the transformation above will look like this:

    7.2 My CD Collection

    Title Artist

    Empire Burlesque Bob Dylan

    Hide your heart Bonnie Tyler

    Greatest Hits Dolly Parton

    Still got the blues Gary More

    Eros Eros Ramazzotti

    One night only Bee Gees

    Sylvias Mother Dr.Hook

    Maggie May Rod Stewart

    Romanza Andrea Bocelli

    When a man loves a woman Percy SledgeBlack angel Savage Rose

    1999 Grammy Nominees Many

    For the good times Kenny Rogers

    Big Willie style Will Smith

    Tupelo Honey Van Morrison

    Soulsville Jorn Hoel

    The very best of Cat Stevens

    Stop Sam Brown

    Bridge of Spies T`Pau

    Private Dancer Tina Turner

    Midt om natten Kim Larsen

    Pavarotti Gala Concert Luciano Pavarotti

    The dock of the bay Otis Redding

    Picture book Simply Red

    Red The Communards

    Unchain my heart Joe Cocker

    View the XML file,View the XSL file, and View the result

    7.3 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)

    16

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_ex3.xslhttp://www.w3schools.com/xsl/cdcatalog_ex3.xslhttp://www.w3schools.com/xsl/cdcatalog_with_ex3.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_ex3.xslhttp://www.w3schools.com/xsl/cdcatalog_with_ex3.xml
  • 8/7/2019 XSL Languages tutorial

    17/43

    != (not equal)

    < less than

    > greater than

    Take a look at the adjusted XSL style sheet:

    My CD Collection

    TitleArtist

    The result of the transformation above will look like this:

    7.4 My CD Collection

    Title ArtistEmpire Burlesque Bob Dylan

    View the XML file,View the XSL file, View the result

    8 XSLT - The Element

    The element is used to sort the output.

    8.1 Where to put the Sort Information

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

    17

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_filter.xslhttp://www.w3schools.com/xsl/cdcatalog_filter.xslhttp://www.w3schools.com/xsl/cdcatalog_filter.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_filter.xslhttp://www.w3schools.com/xsl/cdcatalog_filter.xml
  • 8/7/2019 XSL Languages tutorial

    18/43

    My CD CollectionTitleArtist

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

    The result of the transformation above will look like this:

    8.2 My CD Collection

    Title Artist

    Romanza Andrea Bocelli

    One night only Bee Gees

    Empire Burlesque Bob Dylan

    Hide your heart Bonnie Tyler

    The very best of Cat StevensGreatest Hits Dolly Parton

    Sylvias Mother Dr.Hook

    Eros Eros Ramazzotti

    Still got the blues Gary Moore

    Unchain my heart Joe Cocker

    Soulsville Jorn Hoel

    For the good times Kenny Rogers

    Midt om natten Kim Larsen

    Pavarotti Gala Concert Luciano Pavarotti

    1999 Grammy Nominees Many

    The dock of the bay Otis Redding

    When a man loves a woman Percy Sledge

    Maggie May Rod Stewart

    Stop Sam Brown

    Black angel Savage Rose

    Picture book Simply Red

    Bridge of Spies T`Pau

    18

  • 8/7/2019 XSL Languages tutorial

    19/43

    Red The Communards

    Private Dancer Tina Turner

    Tupelo Honey Van Morrison

    Big Willie style Will Smith

    View the XML file,View the XSL file, and View the result

    9 XSLT - The Element

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

    9.1 The Element

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

    9.1.1 Syntax

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

    9.2 Where to Put the Element

    To add a conditional test, add the element inside the element in the XSLfile:

    My CD CollectionTitleArtist

    19

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_sort.xslhttp://www.w3schools.com/xsl/cdcatalog_sort.xslhttp://www.w3schools.com/xsl/cdcatalog_sort.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_sort.xslhttp://www.w3schools.com/xsl/cdcatalog_sort.xml
  • 8/7/2019 XSL Languages tutorial

    20/43

    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

    higher than 10.

    The result of the transformation above will look like this:

    9.3 My CD Collection

    Title Artist

    Empire Burlesque Bob Dylan

    Still got the blues Gary Moore

    One night only Bee Gees

    Romanza Andrea Bocelli

    Black Angel Savage Rose

    1999 Grammy Nominees Many

    View the XML file,View the XSL file, and View the result

    10 XSLT - The Element

    The element is used in conjunction with and to express multiple conditional tests.

    10.1 The Element

    To express a multiple conditional test against the content of the XML file, add an element to the XSL document.

    10.1.1 Syntax

    ... some output ...

    ... some output ....

    10.2 Where to put the Choose Condition

    To insert a conditional choose test against the content of the XML file, add the ,, and elements to the XSL file:

  • 8/7/2019 XSL Languages tutorial

    21/43

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">My CD Collection

    TitleArtist

    The code above will add a pink background-color to the "Artist" column WHEN the price of the CD ishigher than 10.

    The result of the transformation will look like this:

    10.3 My CD Collection

    Title Artist

    Empire Burlesque Bob Dylan

    Hide your heart Bonnie Tyler

    Greatest Hits Dolly Parton

    Still got the blues Gary Moore

    Eros Eros Ramazzotti

    One night only Bee Gees

    Sylvias Mother Dr.Hook

    Maggie May Rod Stewart

    Romanza Andrea Bocelli

    When a man loves a woman Percy Sledge

    Black angel Savage Rose

    1999 Grammy Nominees Many

    For the good times Kenny Rogers

    Big Willie style Will Smith

    Tupelo Honey Van Morrison

    21

  • 8/7/2019 XSL Languages tutorial

    22/43

    Soulsville Jorn Hoel

    The very best of Cat Stevens

    Stop Sam Brown

    Bridge of Spies T`Pau

    Private Dancer Tina Turner

    Midt om natten Kim LarsenPavarotti Gala Concert Luciano Pavarotti

    The dock of the bay Otis Redding

    Picture book Simply Red

    Red The Communards

    Unchain my heart Joe Cocker

    View the XML file,View the XSL file, and View the result

    10.4 Another Example

    Here is another example that contains two elements:

    My CD CollectionTitleArtist

    22

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_choose.xslhttp://www.w3schools.com/xsl/cdcatalog_choose.xslhttp://www.w3schools.com/xsl/cdcatalog_choose.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_choose.xslhttp://www.w3schools.com/xsl/cdcatalog_choose.xml
  • 8/7/2019 XSL Languages tutorial

    23/43

    The code above will add a pink background color to the "Artist" column WHEN the price of the CD ishigher than 10, and a grey background-color WHEN the price of the CD is higher than 9 and loweror equal to 10.

    The result of the transformation will look like this:

    10.5 My CD CollectionTitle Artist

    Empire Burlesque Bob Dylan

    Hide your heart Bonnie Tyler

    Greatest Hits Dolly Parton

    Still got the blues Gary Moore

    Eros Eros Ramazzotti

    One night only Bee Gees

    Sylvias Mother Dr.Hook

    Maggie May Rod Stewart

    Romanza Andrea Bocelli

    When a man loves a woman Percy Sledge

    Black angel Savage Rose

    1999 Grammy Nominees Many

    For the good times Kenny Rogers

    Big Willie style Will Smith

    Tupelo Honey Van Morrison

    Soulsville Jorn Hoel

    The very best of Cat Stevens

    Stop Sam Brown

    Bridge of Spies T`Pau

    Private Dancer Tina TurnerMidt om natten Kim Larsen

    Pavarotti Gala Concert Luciano Pavarotti

    The dock of the bay Otis Redding

    Picture book Simply Red

    Red The Communards

    Unchain my heart Joe Cocker

    View the XML file,View the XSL file, and View the result

    11 XSLT - The Element

    The element applies a template to the current element or to thecurrent element's child nodes.

    23

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_choose2.xslhttp://www.w3schools.com/xsl/cdcatalog_choose2.xslhttp://www.w3schools.com/xsl/cdcatalog_choose2.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_choose2.xslhttp://www.w3schools.com/xsl/cdcatalog_choose2.xml
  • 8/7/2019 XSL Languages tutorial

    24/43

    11.1 The Element

    The element applies a template to the current element or to the currentelement's child nodes.

    If we add a select attribute to the element it will process only the child

    element that matches the value of the attribute. We can use the select attribute to specify the orderin which the child nodes are processed.

    Look at the following XSL style sheet:

    My CD Collection

    Title:

    Artist:

    The result of the transformation will look like this:

    11.2 My CD Collection

    Title: Empire BurlesqueArtist: Bob Dylan

    Title: Hide your heart

    Artist: Bonnie Tyler

    . . . . .

    12 XSLT - On the Client

    If your browser supports it, XSLT can be used to transform the document to XHTML inyour browser.

    24

  • 8/7/2019 XSL Languages tutorial

    25/43

    12.1 A JavaScript Solution

    In the previous chapters we have explained how XSLT can be used to transform a document fromXML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do thetransformation.

    Even if this works fine, it is not always desirable to include a style sheet reference in an XML file(e.g. it will not work in a non XSLT aware browser.)

    A more versatile solution would be to use a JavaScript to do the transformation.

    By using a JavaScript, we can:

    do browser-specific testing

    use different style sheets according to browser and user needs

    That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transformdata from one format to another, supporting different browsers and different user needs.

    XSLT transformation on the client side is bound to be a major part of the browsers work tasks in thefuture, as we will see a growth in the specialized browser market (Braille, aural browsers, Webprinters, handheld devices, etc.)

    12.2 The XML File and the XSL File

    Look at the XML document that you have seen in the previous chapters:

    Empire Burlesque

    Bob DylanUSAColumbia10.901985

    ...

    View the XML file.

    And the accompanying XSL style sheet:

    My CD Collection

    25

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog.xml
  • 8/7/2019 XSL Languages tutorial

    26/43

    TitleArtist

    View the XSL file.

    Notice that the XML file does not have a reference to the XSL file!

    IMPORTANT: The above sentence indicates that an XML file could be transformed using manydifferent XSL style sheets!

    12.3 Transforming XML to XHTML in the Browser

    Here is the source code needed to transform the XML file to XHTML on the client:

    // Load XMLvar xml = new ActiveXObject("Microsoft.XMLDOM")

    xml.async = falsexml.load("cdcatalog.xml")// Load XSLvar xsl = new ActiveXObject("Microsoft.XMLDOM")xsl.async = falsexsl.load("cdcatalog.xsl")// Transformdocument.write(xml.transformNode(xsl))

    Tip: If you don't know how to write JavaScript, you can study ourJavaScript tutorial.

    The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads theXML file into memory. The second block of code creates another instance of the parser and loadsthe XSL file into memory. The last line of code transforms the XML document using the XSLdocument, and displays the result as XHTML in your browser. Nice!

    See how it works in IE.

    Title Artist

    Empire Burlesque Bob Dylan

    26

    http://www.w3schools.com/xsl/cdcatalog.xslhttp://www.w3schools.com/js/default.asphttp://www.w3schools.com/js/default.asphttp://www.w3schools.com/js/default.asphttp://www.w3schools.com/xsl/tryit.asp?filename=cdcataloghttp://www.w3schools.com/xsl/cdcatalog.xslhttp://www.w3schools.com/js/default.asphttp://www.w3schools.com/xsl/tryit.asp?filename=cdcatalog
  • 8/7/2019 XSL Languages tutorial

    27/43

    Hide your heart Bonnie Tyler

    . .

    . .

    13 XSLT - On the ServerSince not all browsers support XSLT, one solution is to transform the XML to XHTML onthe server.

    13.1 A Cross Browser Solution

    In the previous chapter we explained how XSLT can be used to transform a document from XML toXHTML in the browser. We created a JavaScript that used an XML parser to do the transformation.The JavaScript solution will not work in a browser that doesn't have an XML parser.

    To make XML data available to all kind of browsers, we must transform the XML document on theSERVER and send it as XHTML back to the browser.

    That's another beauty of XSLT. One of the design goals for XSLT was to make it possible totransform data from one format to another on a server, returning readable data to all kinds ofbrowsers.

    13.2 The XML File and the XSLT File

    Look at the XML document that you have seen in the previous chapters:

    Empire BurlesqueBob DylanUSAColumbia10.901985

    ...

    View the XML file.

    And the accompanying XSL style sheet:

    My CD Collection

    27

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog.xml
  • 8/7/2019 XSL Languages tutorial

    28/43

    TitleArtist

    View the XSL file.

    Notice that the XML file does not have a reference to the XSL file!

    IMPORTANT: The above sentence indicates that an XML file could be transformed using manydifferent XSL style sheets!

    13.3 Transforming XML to XHTML on the Server

    Here is the ASP source code needed to transform the XML file to XHTML on the server:

  • 8/7/2019 XSL Languages tutorial

    29/43

    Data stored in XML files can be edited from an Internet browser.

    14.1 Open, Edit and Save XML

    Now, we will show how to open, edit, and save an XML file that is stored on the server.

    We will use XSL to transform the XML document into an HTML form. The values of the XML elementswill be written to HTML input fields in an HTML form. The HTML form is editable. After editing thedata, the data is going to be submitted back to the server and the XML file will be updated (this partis done with ASP).

    14.2 The XML File and the XSL File

    First, look at the XML document that will be used ("tool.xml"):

    HAMMER HG2606

    32456240

    $30.00

    View the XML file.

    Then, take a look at the following style sheet ("tool.xsl"):

    Tool Information (edit):

    29

    http://www.w3schools.com/xsl/tool.xmlhttp://www.w3schools.com/xsl/tool.xml
  • 8/7/2019 XSL Languages tutorial

    30/43


    View the XSL file.

    The XSL file above loops through the elements in the XML file and creates one input field for eachXML "field" element. The value of the XML "field" element's "id" attribute is added to both the "id"

    and "name" attributes of each HTML input field. The value of each XML "value" element is added tothe "value" attribute of each HTML input field. The result is an editable HTML form that contains thevalues from the XML file.

    Then, we have a second style sheet: "tool_updated.xsl". This is the XSL file that will be used todisplay the updated XML data. This style sheet will not result in an editable HTML form, but a staticHTML table:

    Updated Tool Information:

    View the XSL file.

    14.3 The ASP File

    The HTML form in the "tool.xsl" file above has an action attribute with a value of "edittool.asp".

    The "edittool.asp" page contains two functions: The loadFile() function loads and transforms theXML file for display and the updateFile() function applies the changes to the XML file:

    30

    http://www.w3schools.com/xsl/tool.xslhttp://www.w3schools.com/xsl/tool_updated.xslhttp://www.w3schools.com/xsl/tool.xslhttp://www.w3schools.com/xsl/tool_updated.xsl
  • 8/7/2019 XSL Languages tutorial

    31/43

  • 8/7/2019 XSL Languages tutorial

    32/43

    Tip: If you don't know how to write ASP, you can study ourASP tutorial.

    Note: We are doing the transformation and applying the changes to the XML file on the server. Thisis a cross-browser solution. The client will only get HTML back from the server - which will work inany browser!

    15 XML Editors

    If you are serious about XML, you will benefit from using a professional XML Editor.

    15.1 XML is Text-based

    XML is a text-based markup language.

    One great thing about XML is that XML files can be created and edited using a simple text-editor likeNotepad.

    However, when you start working with XML, you will soon find that it is better to edit XMLdocuments using a professional XML editor.

    15.2 Why Not Notepad?

    Many web developers use Notepad to edit both HTML and XML documents because Notepad isincluded with the most common OS and it is simple to use. Personally I often use Notepad for quickediting of simple HTML, CSS, and XML files.

    But, if you use Notepad for XML editing, you will soon run into problems.

    Notepad does not know that you are writing XML, so it will not be able to assist you.

    15.3 Why an XML Editor?

    Today XML is an important technology, and development projects use XML-based technologies like:

    XML Schema to define XML structures and data types

    XSLT to transform XML data

    SOAP to exchange XML data between applications

    WSDL to describe web services

    RDF to describe web resources

    XPath and XQuery to access XML data

    SMIL to define graphics

    To be able to write error-free XML documents, you will need an intelligent XML editor!

    15.4 XML Editors

    Professional XML editors will help you to write error-free XML documents, validate your XML againsta DTD or a schema, and force you to stick to a valid XML structure.

    32

    http://www.w3schools.com/asp/default.asphttp://www.w3schools.com/asp/default.asphttp://www.w3schools.com/asp/default.asp
  • 8/7/2019 XSL Languages tutorial

    33/43

    An XML editor should be able to:

    Add closing tags to your opening tags automatically

    Force you to write valid XML

    Verify your XML against a DTD

    Verify your XML against a Schema

    Color code your XML syntax

    15.5 Altova's XMLSPY

    At W3Schools we have been using XMLSPY for many years. XMLSPY is our favorite XML editor.These are some of the features we especially like:

    Easy to use

    Syntax coloring

    Automatic tag completion

    Automatic well-formed check

    Easy switching between text view and grid view

    Built in DTD and / or Schema validation

    Built in graphical XML Schema designer

    Powerful conversion utilities

    Database import and export

    Built in templates for most XML document types

    Built in XPath analyzer

    Full SOAP and WSDL capabilities

    Powerful project management

    Read more about XMLSPY

    16 XSLT Elements Reference

    The XSLT elements from the W3C Recommendation (XSLT Version 1.0).

    16.1 XSLT Elements

    The links in the "Element" column point to attributes and more useful information about eachspecific element.

    NN: indicates the earliest version of Netscape that supports the tag IE: indicates the earliest version of Internet Explorer that supports the tag

    Note: Elements supported in IE 5.x may have NON-standard behavior, because IE 5.x was releasedbefore XSLT became an official W3C Recommendation. However, IE 6.x has full XML / XSLT 1.0 /XPath 1.0 support!

    Element Description IE NN

    apply-imports Applies a template rule from an imported style sheet 6.0

    33

    http://www.altova.com/ref/?s=w3schools&q=xmlhttp://www.w3schools.com/xsl/el_apply-imports.asphttp://www.altova.com/ref/?s=w3schools&q=xmlhttp://www.w3schools.com/xsl/el_apply-imports.asp
  • 8/7/2019 XSL Languages tutorial

    34/43

    apply-templates Applies a template rule to the current element or tothe current element's child nodes

    5.0 6.0

    attribute Adds an attribute 5.0 6.0

    attribute-set Defines a named set of attributes 6.0 6.0

    call-template Calls a named template 6.0 6.0

    choose Used in conjunction with and to

    express multiple conditional tests

    5.0 6.0

    comment Creates a comment node in the result tree 5.0 6.0

    copy Creates a copy of the current node (without childnodes and attributes)

    5.0 6.0

    copy-of Creates a copy of the current node (with child nodesand attributes)

    6.0 6.0

    decimal-format Defines the characters and symbols to be used whenconverting numbers into strings, with the format-number() function

    6.0

    element Creates an element node in the output document 5.0 6.0

    fallback Specifies an alternate code to run if the processordoes not support an XSLT element

    6.0

    for-each Loops through each node in a specified node set 5.0 6.0

    if Contains a template that will be applied only if aspecified condition is true

    5.0 6.0

    import Imports the contents of one style sheet into another.Note: An imported style sheet has lower precedencethan the importing style sheet

    6.0 6.0

    include Includes the contents of one style sheet into another.Note: An included style sheet has the sameprecedence as the including style sheet

    6.0 6.0

    key Declares a named key that can be used in the stylesheet with the key() function

    6.0 6.0

    message Writes a message to the output (used to report errors) 6.0 6.0

    namespace-alias Replaces a namespace in the style sheet to a differentnamespace in the output

    6.0

    number Determines the integer position of the current nodeand formats a number

    6.0 6.0

    otherwise Specifies a default action for the element 5.0 6.0

    output Defines the format of the output document 6.0 6.0

    param Declares a local or global parameter 6.0 6.0

    preserve-space Defines the elements for which white space should bepreserved

    6.0 6.0

    processing-instruction Writes a processing instruction to the output 5.0 6.0

    sort Sorts the output 6.0 6.0

    strip-space Defines the elements for which white space should beremoved

    6.0 6.0

    stylesheet Defines the root element of a style sheet 5.0 6.0

    template Rules to apply when a specified node is matched 5.0 6.0

    text Writes literal text to the output 5.0 6.0

    transform Defines the root element of a style sheet 6.0 6.0

    value-of Extracts the value of a selected node 5.0 6.0

    variable Declares a local or global variable 6.0 6.0

    when Specifies an action for the element 5.0 6.0

    with-param Defines the value of a parameter to be passed into atemplate

    6.0 6.0

    34

    http://www.w3schools.com/xsl/el_apply-templates.asphttp://www.w3schools.com/xsl/el_attribute.asphttp://www.w3schools.com/xsl/el_attributeset.asphttp://www.w3schools.com/xsl/el_call-template.asphttp://www.w3schools.com/xsl/el_choose.asphttp://www.w3schools.com/xsl/el_comment.asphttp://www.w3schools.com/xsl/el_copy.asphttp://www.w3schools.com/xsl/el_copy-of.asphttp://www.w3schools.com/xsl/el_decimal-format.asphttp://www.w3schools.com/xsl/el_element.asphttp://www.w3schools.com/xsl/el_fallback.asphttp://www.w3schools.com/xsl/el_for-each.asphttp://www.w3schools.com/xsl/el_if.asphttp://www.w3schools.com/xsl/el_import.asphttp://www.w3schools.com/xsl/el_include.asphttp://www.w3schools.com/xsl/el_key.asphttp://www.w3schools.com/xsl/el_message.asphttp://www.w3schools.com/xsl/el_namespace-alias.asphttp://www.w3schools.com/xsl/el_number.asphttp://www.w3schools.com/xsl/el_otherwise.asphttp://www.w3schools.com/xsl/el_output.asphttp://www.w3schools.com/xsl/el_param.asphttp://www.w3schools.com/xsl/el_preserve-space.asphttp://www.w3schools.com/xsl/el_processing-instruction.asphttp://www.w3schools.com/xsl/el_sort.asphttp://www.w3schools.com/xsl/el_preserve-space.asphttp://www.w3schools.com/xsl/el_stylesheet.asphttp://www.w3schools.com/xsl/el_template.asphttp://www.w3schools.com/xsl/el_text.asphttp://www.w3schools.com/xsl/el_stylesheet.asphttp://www.w3schools.com/xsl/el_value-of.asphttp://www.w3schools.com/xsl/el_variable.asphttp://www.w3schools.com/xsl/el_when.asphttp://www.w3schools.com/xsl/el_with-param.asphttp://www.w3schools.com/xsl/el_apply-templates.asphttp://www.w3schools.com/xsl/el_attribute.asphttp://www.w3schools.com/xsl/el_attributeset.asphttp://www.w3schools.com/xsl/el_call-template.asphttp://www.w3schools.com/xsl/el_choose.asphttp://www.w3schools.com/xsl/el_comment.asphttp://www.w3schools.com/xsl/el_copy.asphttp://www.w3schools.com/xsl/el_copy-of.asphttp://www.w3schools.com/xsl/el_decimal-format.asphttp://www.w3schools.com/xsl/el_element.asphttp://www.w3schools.com/xsl/el_fallback.asphttp://www.w3schools.com/xsl/el_for-each.asphttp://www.w3schools.com/xsl/el_if.asphttp://www.w3schools.com/xsl/el_import.asphttp://www.w3schools.com/xsl/el_include.asphttp://www.w3schools.com/xsl/el_key.asphttp://www.w3schools.com/xsl/el_message.asphttp://www.w3schools.com/xsl/el_namespace-alias.asphttp://www.w3schools.com/xsl/el_number.asphttp://www.w3schools.com/xsl/el_otherwise.asphttp://www.w3schools.com/xsl/el_output.asphttp://www.w3schools.com/xsl/el_param.asphttp://www.w3schools.com/xsl/el_preserve-space.asphttp://www.w3schools.com/xsl/el_processing-instruction.asphttp://www.w3schools.com/xsl/el_sort.asphttp://www.w3schools.com/xsl/el_preserve-space.asphttp://www.w3schools.com/xsl/el_stylesheet.asphttp://www.w3schools.com/xsl/el_template.asphttp://www.w3schools.com/xsl/el_text.asphttp://www.w3schools.com/xsl/el_stylesheet.asphttp://www.w3schools.com/xsl/el_value-of.asphttp://www.w3schools.com/xsl/el_variable.asphttp://www.w3schools.com/xsl/el_when.asphttp://www.w3schools.com/xsl/el_with-param.asp
  • 8/7/2019 XSL Languages tutorial

    35/43

    17 XSLT Functions

    XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.

    17.1 XSLT Functions

    XSLT 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.

    The URI of the XSLT function namespace is:http://www.w3.org/2005/02/xpath-functions

    The default prefix for the function namespace is fn:.

    Tip: Functions are often called with the fn: prefix, such as fn:string(). However, since fn: is thedefault prefix of the namespace, the function names do not need to be prefixed when called.

    The reference of all the built-in XSLT 2.0 functions is located in our XPath tutorial.

    In addition, there are the following built-in XSLT functions:

    Name Description

    current() Returns the current node

    document() Used to access the nodes in an external XML document

    element-available() Tests whether the element specified is supported by the XSLT processor

    format-number() Converts a number into a string

    function-available() Tests whether the function specified is supported by the XSLT processor

    generate-id() Returns a string value that uniquely identifies a specified node

    key() Returns a node-set using the index specified by an element

    system-property() Returns the value of the system propertiesunparsed-entity-uri() Returns the URI of an unparsed entity

    18 The current() Function

    18.1 Definition and Usage

    The current() function returns a node-set that contains only the current node. Usually the currentnode and the context node are the same.

    is equal to

    However, there is one difference. Look at the following XPath expression: "catalog/cd". Thisexpression selects the child nodes of the current node, and then it selects the childnodes of the nodes. This means that on each step of evaluation, the "." has a differentmeaning.

    35

    http://www.w3schools.com/xpath/xpath_functions.asphttp://www.w3schools.com/xsl/func_current.asphttp://www.w3schools.com/xsl/func_document.asphttp://www.w3schools.com/xsl/func_elementavailable.asphttp://www.w3schools.com/xsl/func_formatnumber.asphttp://www.w3schools.com/xsl/func_functionavailable.asphttp://www.w3schools.com/xsl/func_generateid.asphttp://www.w3schools.com/xsl/func_key.asphttp://www.w3schools.com/xsl/func_systemproperty.asphttp://www.w3schools.com/xsl/func_unparsedentityuri.asphttp://www.w3schools.com/xpath/xpath_functions.asphttp://www.w3schools.com/xsl/func_current.asphttp://www.w3schools.com/xsl/func_document.asphttp://www.w3schools.com/xsl/func_elementavailable.asphttp://www.w3schools.com/xsl/func_formatnumber.asphttp://www.w3schools.com/xsl/func_functionavailable.asphttp://www.w3schools.com/xsl/func_generateid.asphttp://www.w3schools.com/xsl/func_key.asphttp://www.w3schools.com/xsl/func_systemproperty.asphttp://www.w3schools.com/xsl/func_unparsedentityuri.asp
  • 8/7/2019 XSL Languages tutorial

    36/43

    The following line:

    will process all cd elements that have a title attribute with value equal to the value of the currentnode's ref attribute.

    This is different from

    that will process all cd elements that have a title attribute and a ref attribute with the same value.

    18.2 Syntax

    node-set current()

    18.2.1 Example 1

    Current node:

    View the XML file,View the XSL file, and View the result.

    Current node: Bob DylanCurrent node: Bonnie Tyler

    19 The document() Function

    19.1 Definition and Usage

    The document() function is used to access nodes in an external XML document. The external XMLdocument must be valid and parsable.

    One way to use this function is to look up data in an external document. For example we want tofind the Celsius value from a Fahrenheit value and we refer to a document that contains some pre-computed results:

    19.2 Syntax

    36

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_current.xslhttp://www.w3schools.com/xsl/cdcatalog_current.xslhttp://www.w3schools.com/xsl/cdcatalog_current.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_current.xslhttp://www.w3schools.com/xsl/cdcatalog_current.xml
  • 8/7/2019 XSL Languages tutorial

    37/43

    node-set document(object,node-set?)

    19.3 Parameters

    Parameter Description

    object Required. Defines an URI to an external XML document

    node-set Optional. Used to resolve relative URI

    20 The element-available() Function

    20.1 Definition and Usage

    The element-available() function returns a Boolean value that indicates whether the elementspecified is supported by the XSLT processor.

    This function can only be used to test elements that can occur in a template body. These elementsare:

    xsl:apply-imports xsl:apply-templates

    xsl:attributes

    xsl:call-template

    xsl:choose

    xsl:comment

    xsl:copy

    xsl:copy-of

    xsl:element

    xsl:fallback

    xsl:for-each

    xsl:if

    xsl:message

    xsl:number

    xsl:processing instruction

    xsl:text

    xsl:value-of

    xsl:variable

    20.2 Syntax

    boolean element-available(string)

    20.3 Parameters

    Parameter Descriptionstring Required. Specifies the element to test

    20.3.1 Example 1

    37

  • 8/7/2019 XSL Languages tutorial

    38/43

    xsl:comment is supported.

    xsl:comment is not supported.

    xsl:delete is supported.

    xsl:delete is not supported.

    View the XSL file and View the result.

    xsl:comment is supported.

    xsl:delete is not supported.

    21 The format-number() Function

    21.1 Definition and Usage

    The format-number() function is used to convert a number into a string.

    21.2 Syntax

    string format-number(number,format,[decimalformat])

    21.3 Parameters

    Parameter Description

    number Required. Specifies the number to be formatted

    format Required. Specifies the format pattern. Here are some of the characters used in theformatting pattern:

    # (Denotes a digit. Example: ####) 0 (Denotes leading and following zeros. Example: 0000.00)

    . (The position of the decimal point Example: ###.##)

    , (The group separator for thousands. Example: ###,###.##)

    % (Displays the number as a percentage. Example: ##%)

    ; (Pattern separator. The first pattern will be used for positive numbers andthe second for negative numbers)

    decimalformatOptional.

    38

    http://www.w3schools.com/xsl/elementavailable.xslhttp://www.w3schools.com/xsl/elementavailable.xmlhttp://www.w3schools.com/xsl/elementavailable.xslhttp://www.w3schools.com/xsl/elementavailable.xml
  • 8/7/2019 XSL Languages tutorial

    39/43

    21.3.1 Example 1





    View the XSL file and View the result.

    500100.00500100.0500,100.0023%500100

    22 The function-available() Function

    22.1 Definition and Usage

    The function-available() function returns a Boolean value that indicates whether the functionspecified is supported by the XSLT processor.

    You may test the XSLT functions and the inherited XPath functions.

    22.2 Syntax

    boolean function-available(string)

    22.3 Parameters

    Parameter Descriptionstring Required. Specifies the function to test

    22.3.1 Example 1

    39

    http://www.w3schools.com/xsl/formatnumber.xslhttp://www.w3schools.com/xsl/formatnumber.xmlhttp://www.w3schools.com/xsl/formatnumber.xslhttp://www.w3schools.com/xsl/formatnumber.xml
  • 8/7/2019 XSL Languages tutorial

    40/43

    sum() is supported.

    sum() is not supported.

    current() is supported.

    current() is not supported.

    View the XSL file and View the result

    sum() is supported.

    current() is supported.

    23 The generate-id() Function

    23.1 Definition and Usage

    The generate-id() function returns a string value that uniquely identifies a specified node.

    If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter,it defaults to the current node.

    23.2 Syntax

    string generate-id(node-set?)

    23.3 Parameters

    Parameter Description

    node-set Optional. Specifies on which node-set to generate a unique id

    23.3.1 Example 1

    Artists:

    40

    http://www.w3schools.com/xsl/functionavailable.xslhttp://www.w3schools.com/xsl/functionavailable.xmlhttp://www.w3schools.com/xsl/functionavailable.xslhttp://www.w3schools.com/xsl/functionavailable.xml
  • 8/7/2019 XSL Languages tutorial

    41/43

    Artist:
    Title:
    Price:

    View the XML file,View the XSL file, and View the result.

    24 The key() Function

    24.1 Definition and Usage

    The key() function returns a node-set from the document, using the index specified by an element.

    24.2 Syntax

    node-set key(string, object)

    24.3 Parameters

    Parameter Description

    string Required. Specifies the name of an xsl:key element

    object Required. A string to search for

    24.3.1 Example 1

    Title:

    41

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_generateid.xslhttp://www.w3schools.com/xsl/cdcatalog_generateid.xslhttp://www.w3schools.com/xsl/cdcatalog_generateid.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_generateid.xslhttp://www.w3schools.com/xsl/cdcatalog_generateid.xml
  • 8/7/2019 XSL Languages tutorial

    42/43


    Artist:
    Price:

    View the XML file,View the XSL file, and View the result.

    Title: Empire BurlesqueArtist: Bob Dylan

    Price: 10.90

    25 The system-property() Function

    25.1 Definition and Usage

    The system-property() function returns the value of the system property specified by the name.

    System properties in the XSLT namespace:

    xsl:version - The version of XSLT implemented by the processor

    xsl:vendor - The vendor of the XSLT processor

    xsl:vendor-url - The URL identifying the vendor of the XSLT processor

    25.2 Syntax

    object system-property(string)

    25.3 Parameters

    Parameter Description

    string Required. Specifies the system property to return the value of

    25.3.1 Example 1

    Version:
    Vendor:
    Vendor URL:

    42

    http://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_key.xslhttp://www.w3schools.com/xsl/cdcatalog_key.xslhttp://www.w3schools.com/xsl/cdcatalog_key.xmlhttp://www.w3schools.com/xsl/cdcatalog.xmlhttp://www.w3schools.com/xsl/cdcatalog_key.xslhttp://www.w3schools.com/xsl/cdcatalog_key.xml
  • 8/7/2019 XSL Languages tutorial

    43/43

    View the XSL file and View the result

    Version: 1

    Vendor: MicrosoftVendor URL: http://www.microsoft.com

    26 The unparsed-entity-uri() Function

    26.1 Definition and Usage

    The unparsed-entity-uri() function returns the URI of an unparsed entity. The name of the entitymust match the passed argument. If there is no such entity an empty string is returned.

    If the DTD contains the following declaration:

    the following expression:

    unparsed-entity-uri('pic')

    will return the URI for the file "picture.jpg".

    26.2 Syntax

    string unparsed-entity-uri(string)

    26.3 Parameters

    Parameter Description

    string Required. Specifies the name of an unparsed entity

    http://www.w3schools.com/xsl/systemproperty.xslhttp://www.w3schools.com/xsl/systemproperty.xmlhttp://www.w3schools.com/xsl/systemproperty.xslhttp://www.w3schools.com/xsl/systemproperty.xml