orion ifland 2008 11 04
Download
Skip this Video
Download Presentation
Introduction to XSLT

Loading in 2 Seconds...

play fullscreen
1 / 69

Introduction to XSLT - PowerPoint PPT Presentation


  • 335 Views
  • Uploaded on

Orion Ifland / 2008-11-04. Introduction to XSLT. Introduction to XSLT. Agenda The X* Ecosystem (XSL, XPath, XQuery...) XML Refresh/Terminology XPath: Addresses for XML documents XSLT Basics: template and value-of XSLT Loops: apply-templates and for-each

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to XSLT' - victoria


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to xslt
Introduction to XSLT
  • Agenda

The X* Ecosystem (XSL, XPath, XQuery...)

XML Refresh/Terminology

XPath: Addresses for XML documents

XSLT Basics: template and value-of

XSLT Loops: apply-templates and for-each

XSLT Decisions: if and choose

XSLT Variables: variable and param

XSLT Extras: sort, copy-of, document…

Why/When XSLT?

the x ecosystem
The X* Ecosystem
  • XSL(T): Extensible Stylesheet Language (Transforms)‏
    • Like a giant function with a domain of XML and a range of XML, HTML and Text
    • XSL-FO (Formatting Objects) is a related, graphics/print-oriented language
  • XPath: XML Path (addressing)‏
    • Like a filesystem path in an XML document
    • Also like RegEx
the x ecosystem4
The X* Ecosystem
  • XQuery / XPath 2.0 / XSLT 2.0
    • Kind of jumbled together
    • XQuery looks more like a mix of XSLT, SQL, and Javascript (including lots of XPath)‏
  • XMLNS (XML Namespaces)‏
    • Like programming language namespaces, distinguishes between elements/attributes with the same name
    • Used by XSLT and many other XML dialects
xml refresh terminology
XML Refresh/Terminology
  • XML Nodes
    • Processing instruction:
    • Element: or
    • Attribute:
    • Comment:
    • Entity: &
    • Text node (just plain text)
xml refresh terminology6
XML Refresh/Terminology
  • XML Syntax Rules
    • Escape < > & (turn them into entities < > &)
    • Every document has exactly one root element
    • Attribute values are always quoted
    • Elements are case-sensitive (and are typically lowercase)
    • You don't have to put an xml processing instruction at the top of your document
xml namespaces
XML Namespaces
  • Reason
    • Gives context to the meaning of elements
    • Disambiguates between elements with the same name
  • Challenges
    • Easy to forget to specify namespaces
    • Non-forgiving
xml namespaces8
XML Namespaces
  • Syntax
    • Default namespace, e.g. xmlns="http://www.w3.org/xhtml/1999"
    • Prefixed namespace, e.g. xmlns:dc="http://purl.org/dc/elements/1.1/"
xml namespaces9
XML Namespaces
  • Example: XHTML
xml namespaces10
XML Namespaces
  • Example: XSLT
xpath addresses for xml
XPath: Addresses for XML

XPath Expressions

/library

/library/book

/library/book/@name

/library/book[@name="The Fourth Civilization"]

/library/book[1]

//book[2]

xpath addresses for xml12
XPath: Addresses for XML

XPath Expressions

/library

/library/book

/library/book/@name

/library/book[@name="The Fourth Civilization"]

/library/book[1]

//book[2]

xpath addresses for xml13
XPath: Addresses for XML

XPath Expressions

/library

/library/book

/library/book/@name

/library/book[@name="The Fourth Civilization"]

/library/book[1]

//book[2]

xpath addresses for xml14
XPath: Addresses for XML

XPath Expressions

/library

/library/book

/library/book/@name

/library/book[@name="The Fourth Civilization"]

/library/book[1]

//book[2]

Predicate

xpath addresses for xml15
XPath: Addresses for XML

XPath Expressions

/library

/library/book

/library/book/@name

/library/book[@name="The Fourth Civilization"]

/library/book[1]

//book[2]

xpath addresses for xml16
XPath: Addresses for XML

XPath Expressions

/library

/library/book

/library/book/@name

/library/book[@name="The Fourth Civilization"]

/library/book[1]

//book[2]

xpath addresses for xml17
XPath: Addresses for XML

XPath Node selectors

/library/*

/library/book[1]/text()

/library/node()[1]

.

The Principles of Computer Hardware

xpath addresses for xml18
XPath: Addresses for XML

XPath Node selectors

/library/*

/library/book[1]/text()

/library/node()[1]

.

ThePrinciples of Computer Hardware

xpath addresses for xml19
XPath: Addresses for XML

XPath Node selectors

/library/*

/library/book[1]/text()

/library/node()[1]

.

The Principles of Computer Hardware

xpath addresses for xml20
XPath: Addresses for XML

XPath Node selectors

/library/*

/library/book[1]/text()

/library/node()[1]

.

The Principles of Computer Hardware

Selects the "current" node

xpath addresses for xml21
XPath: Addresses for XML

XPath Axes

/library/child::book(or /library/book for short)

/descendent-or-self::book(or //book for short)‏

//book[1]/parent::*(or //book[1]/.. for short)

//book[2]/preceding-sibling::book

//book[1]/attribute::name(or //book[1]/@name for short)

xpath addresses for xml22
XPath: Addresses for XML

XPath Axes

/library/child::book(or /library/book for short)

/descendent-or-self::book(or //book for short)

//book[1]/parent::*(or //book[1]/.. for short)

//book[2]/preceding-sibling::book

//book[1]/attribute::name(or //book[1]/@name for short)

xpath addresses for xml23
XPath: Addresses for XML

XPath Axes

/library/child::book(or /library/book for short)

/descendent-or-self::book(or //book for short)

//book[1]/parent::*(or //book[1]/.. for short)

//book[2]/preceding-sibling::book

//book[1]/attribute::name(or //book[1]/@name for short)

xpath addresses for xml24
XPath: Addresses for XML

XPath Axes

/library/child::book(or /library/book for short)

/descendent-or-self::book(or //book for short)

//book[1]/parent::*(or //book[1]/.. for short)

//book[2]/preceding-sibling::book

//book[1]/attribute::name(or //book[1]/@name for short)

xpath addresses for xml25
XPath: Addresses for XML

XPath Axes

/library/child::book(or /library/book for short)

/descendent-or-self::book(or //book for short)‏

//book[1]/parent::*(or //book[1]/.. for short)

//book[2]/preceding-sibling::book

//book[1]/attribute::name(or //book[1]/@name for short)

xpath addresses for xml26
XPath: Addresses for XML

XPath Functions

//book[last()]

count(//book)

name(/*)

//book[contains(@name, "C++")]

//book[not(contains(@name, "C++"))]

xpath addresses for xml27
XPath: Addresses for XML

XPath Functions

//book[last()]

count(//book)

name(/*)

//book[contains(@name, "C++")]

//book[not(contains(@name, "C++"))]

Returns: 2

xpath addresses for xml28
XPath: Addresses for XML

XPath Functions

//book[last()]

count(//book)

name(/*)

//book[contains(@name, "C++")]

//book[not(contains(@name, "C++"))]

Returns: "library"

xpath addresses for xml29
XPath: Addresses for XML

XPath Functions

//book[last()]

count(//book)

name(/*)

//book[contains(@name, "C++")]

//book[not(contains(@name, "C++"))]

xpath addresses for xml30
XPath: Addresses for XML

XPath Functions

//book[last()]

count(//book)

name(/*)

//book[contains(@name, "C++")]

//book[not(contains(@name, "C++"))]

xpath addresses for xml31
XPath: Addresses for XML

Other Useful XPath Functions

position() – provides the position in a list (nice for numbering a sequence of nodes)

sum(xpath) – takes a sequence of nodes and adds up their numerical values – see also avg(), min(), max()

concat(string, string, …) – exactly what you think

string-length(string) – returns the number of characters

substring(string, start[, length]) – the first char is at 1

translate(source-string, find-string, replace-string) – looks for individual chars and replaces themExample: translate("ABCD", "BD", "bd")  "AbCd"

xpath addresses for xml32
XPath: Addresses for XML

XPath Operators

//book[last() or contains(@name, "C++")]

//book[string-length(@name) > 5]

//book[1] | //book[2]

Other operators:

and, =, >, <=, >=, !=, +, -, *, div, mod

Don't forget to escape < >!

xpath addresses for xml33
XPath: Addresses for XML

XPath Operators

//book[last() or contains(@name, "C++")]

//book[string-length(@name) > 5]

//book[1] | //book[2]

Other operators:

and, =, >, <=, >=, !=, +, -, *, div, mod

Don't forget to escape < >!

xpath addresses for xml34
XPath: Addresses for XML

XPath Operators

//book[last() or contains(@name, "C++")]

//book[string-length(@name) > 5]

//book[1] | //book[2]

Other operators:

and, =, >, <=, >=, !=, +, -, *, div, mod

Don't forget to escape < >!

xpath addresses for xml35
XPath: Addresses for XML

XPath Operators

//book[last() or contains(@name, "C++")]

//book[string-length(@name) > 5]

//book[1] | //book[2]

Other operators:

and, =, >, <=, >=, !=, +, -, *, div, mod

Don't forget to escape < >!

xpath addresses for xml36
XPath: Addresses for XML

Namespaces

xmlns:y="http://x"

/y:library

/y:library/y:book

/y:library/y:book/@name

Namespaces must be declared in the XSLT document – prefixes aren't "imported" from the source document

xslt basics
Try it out

Make an XML file

Make an XSLT file

Run the transform and view the transformed data

XSLT Basics

input

transform

(or text)

output

xslt basics38
Sample XML

...one giant leap for mankind.

Sample Output

Neil Armstrong said "...one giant leap for mankind."

Sample XSLT

said "

"

XSLT Basics
xslt basics39
Sample XML

...one giant leap for mankind.

Sample Output

Neil Armstrong said "...one giant leap for mankind."

Sample XSLT

said "

"

XSLT Basics
xslt basics40
Sample XML

...one giant leap for mankind.

Sample Output

Neil Armstrong said "...one giant leap for mankind."

Sample XSLT

said "

"

XSLT Basics
xslt basics41
Sample XML

...one giant leap for mankind.

Sample Output

Neil Armstrong

…one giant leap for mankind.

Sample XSLT  HTML

</p><p><xsl:value-of select="concat(name/@first, ' ', name/@last)" /></p><p>

XSLT Basics

All well-formed

xslt basics42
Sample XML

...one giant leap for mankind.

Sample Output

Neil Armstrong

…one giant leap for mankind.

Sample XSLT  HTML

</p><p><xsl:value-of select="concat(name/@first, ' ', name/@last)" /></p><p>

XSLT Basics
xslt basics43
Sample XML

...one giant leap for mankind.

Sample Output

…one giant leap for mankind.

Sample XSLT  XML

XSLT Basics
xslt basics44
Sample XML

...one giant leap for mankind.

Sample Output

…one giant leap for mankind.

Sample XSLT  XML

XSLT Basics
xslt basics45
Sample XML

...one giant leap for mankind.

Sample Output

…one giant leap for mankind.

Sample XSLT  XML

XSLT Basics
xslt loops for each
Sample XML

Sample XSLT  HTML

Zoo

XSLT Loops: for-each
xslt loops for each47
Sample XML

Result HTML

Zoo

birds

mammals

XSLT Loops: for-each
xslt loops for each48
Sample XML

Sample XSLT  HTML

Zoo

  • ()

XSLT Loops: for-each
xslt loops for each49
Sample XML

Result HTML

Zoo

birds

  • albatross (4)

mammals

  • aardvark (5)

XSLT Loops: for-each
xslt loops apply templates

  • ()

XSLT Loops: apply-templates

Sample XSLT

Zoo

xslt decisions if
XML

XSLT

We have more than 2...

XSLT Decisions: if
xslt decisions if52
Sample XML

Result HTML

Zoo

birds

We have more than 2...

  • albatross
  • chickadee

mammals

We have more than 2...

  • aardvark
  • bat

XSLT Decisions: if
xslt decisions choose
XML

XSLT fragment

  • (

    a couple

    a few

    many

    )

XSLT Decisions: choose
xslt decisions choose54
Sample XML

Result HTML

Zoo

  • albatross (a few)
  • buzzard (a couple)
  • chickadee (many)

  • aardvark (a few)
  • bat (many)
  • cheetah (a couple)

XSLT Decisions: choose
xslt variables variable
XML

XSLT fragment

  • (% of all )

XSLT Variables: variable
xslt variables variable56
Source XML

Result HTML

Zoo

  • albatross (22% of all birds)
  • buzzard (11% of all birds)
  • chickadee (67% of all birds)

  • aardvark (2% of all mammals)
  • bat (97% of all mammals)
  • cheetah (1% of all mammals)

XSLT Variables: variable
xslt variables parameter

  • (%)

  • XSLT Variables: parameter

    XSLT fragments

    xslt variables param

  • (%)

  • XSLT Variables: param

    XSLT fragments

    xslt variables param59
    XML

    XSLT fragment

    • ()

    XSLT Variables: param
    xslt variables param60
    Source XML

    Result HTML

    Zoo

    • chickadee (12)

    • aardvark (5)
    • bat (200)

    XSLT Variables: param
    xslt extras sort
    XML

    XSLT fragment

    • ()

    XSLT Extras: sort
    xslt extras sort62
    Source XML

    Result HTML

    Zoo

    • chickadee (12)
    • albatross (4)
    • buzzard (2)

    • bat (200)
    • aardvark (5)
    • cheetah (2)

    XSLT Extras: sort
    xslt extras copy of
    Source XML

    ...one giant leap for mankind.

    XML Output

    …one giant leap for mankind.

    Sample XSLT  XML

    XSLT Extras: copy-of
    xslt extras more elements
    XSLT Extras: more elements
    • xsl:text – writes literal text to the output (useful for controlling whitespace or forcing exact, unescaped output)
    • xsl:processing-instruction – writes a PI like or to the output
    • xsl:import and xsl:include – used to combine stylesheets (useful for XSLT libraries)
    xslt extras more functions
    XSLT Extras: more functions
    • document(url) – opens an XML document at the given location, returning its nodes as data that can be used by the templateExample: document('zoo.xml')/zoo//*[@pop = 2]
    • current() – similar to "." except that it always refers to the current node, even when used inside predicatesExample://*[@pop = current()/@pop]
    xslt extras xml stylesheet
    XSLT Extras: xml-stylesheet

    To make an XML document show itself in a transformed format, add this to the top:

    This is particularly useful for XML like RSS

    why when xslt
    Why/When XSLT?
    • Web Standard
      • XSLT v 1.0 Recommended by W3C almost 10 years ago (it's stable and well understood)
      • Dozens of implementations and host languages (Java, .NET, PHP, C++…)
      • Wide tool support (Firefox, IE, Visual Studio, many text editors, full IDEs…)
      • No vendor lock-in
    why when xslt68
    Why/When XSLT?
    • Very good at converting XML to XML, XHTML/HTML, or plain text
      • Makes it easy to keep things well-formed
      • Uses XML syntax, so the only real new syntax is XPath (which is also used elsewhere)
      • XPath is far more compact than similar DOM code in JS/Java/C#/etc.
      • Can be interpreted (for quick development) or compiled (for maximum performance)
    ad