xslt for web developers

74
XSLT for Web Developers Sanders Kleinfeld (in 30 minutes or less)

Upload: sanders-kleinfeld

Post on 08-Jul-2015

458 views

Category:

Software


0 download

DESCRIPTION

A 30-minute introduction to the basics of document transformation with XSLT for Web developers comfortable working with HTML/CSS/JS.

TRANSCRIPT

Page 1: XSLT for Web Developers

XSLT for Web Developers

Sanders Kleinfeld

(in 30 minutes or less)

Page 2: XSLT for Web Developers

Pop Quiz #1: XSLT is… !

A: An acronym for “Extensible Stylesheet Language Transformations” !B: A programming language written in XML syntax !C: An official W3C Specification !D: All of the above

Page 3: XSLT for Web Developers

Pop Quiz #1: XSLT is… !

A: An acronym for “Extensible Stylesheet Language Transformations” !B: A programming language written in XML syntax !C: An official W3C Specification !D: All of the above

(http://www.w3.org/TR/xslt-30)

Page 4: XSLT for Web Developers

XSLT is a tool for global, programmatic markup

manipulation

Page 5: XSLT for Web Developers

<body>! <div class="section">! <p><b>Chapter 1</b></p>!HTML5 is really great, !because there are lots of new elements to facilitate meaningful!tagging of content.!<br/><br/>!Also, they deprecated a lot of yucky <font color="green">non-semantic stuff.</font>! </div>!</body>!

<body>! <section>! <h1>Chapter 1<h1>!<p>HTML5 is really great, !because there are lots of new elements to facilitate meaningful tagging of content.</p>!<p>Also, they deprecated a lot of yucky <span style="color: green;">non-semantic stuff.</span></p>! </section>!</body>!

XSLT

😢 😎

Page 6: XSLT for Web Developers

Rhetorical Question #1: !

!

“Um, can’t I just use regular expressions

for this?”

Page 7: XSLT for Web Developers

Example: Converting <b> to <em> with

regex

your_markup.replace(/<b>/, '<em>')

Page 8: XSLT for Web Developers

But what about: !

• Closing tags (</b>)

• Attributes (<b class="term">)

• Extra Whitespace (<b >)

your_markup.replace(/<(\/)?b(\s*[^>]*)>/g, '<$1em$2>')

Page 9: XSLT for Web Developers

A Stack Overflow Classic: “You can’t parse [X]HTML with regex”

(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)

Page 10: XSLT for Web Developers

Rhetorical Question #2: !

!

“OK, how about a library like jQuery?”

Page 11: XSLT for Web Developers

This jQuery WILL NOT work:

$('b').tag.prop("tagName") = "em"

because tagName is read-only

Page 12: XSLT for Web Developers

However, there are jQuery workarounds…

(http://stackoverflow.com/questions/3435871/jquery-how-to-change-tag-name)

Page 13: XSLT for Web Developers

…And other JS XML parsers

(https://github.com/nfarina/xmldoc)

Page 14: XSLT for Web Developers

Rhetorical Question #3: !

!

“But for complex transforms, why not go ahead and learn

XSLT?”

Page 15: XSLT for Web Developers

XSLT leverages a functional* paradigm

* Many folks have salient objections to calling XSLT a functional programming language (e.g., http://www.snoyman.com/blog/2012/04/xslt-rant.html), but document processing with XSLT still embodies the spirit of functional programming, and it feels pedantic to me to deny that.

Page 16: XSLT for Web Developers

var input;

function(input) {! // Some stuff! // happens here! return output;!}

Functional Paradigm in JS

output

Page 17: XSLT for Web Developers

<markup>! <!--Input-->!</markup>

Functional Paradigm in XSLT

<markup>! <!--Output-->!</markup>

<xsl:stylesheet>!! <xsl:template match="...">! <!—Stuff happens here-->! </xsl:template>!! <xsl:template match="...">! <!—Stuff happens here-->! </xsl:template>!!</xsl:stylesheet>

Page 18: XSLT for Web Developers

function(a) {! return a;!}

Identity Function* in JS

* Output of function is identical to input

Page 19: XSLT for Web Developers

Identity Stylesheet* in XSLT

* Output of stylesheet is identical to input

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

Page 20: XSLT for Web Developers

Rhetorical Question #4: !

!

!

“Say What?”

Page 21: XSLT for Web Developers

Identity Stylesheet in XSLT: Explained

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

BEGIN stylesheet

END stylesheet

BEGIN matching function Match any node (element, attribute, text)

BEGIN copy matched node (OPEN elem)

END Copy matched node (CLOSE elem)

Select any node

END matching function

Run stylesheet against specified children of matched node

Page 22: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

Page 23: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

Page 24: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!!</body>

Page 25: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!!</body>

Page 26: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!!</body>

Page 27: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p>!!</p>!</body>

Page 28: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p>!!</p>!</body>

Page 29: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p>!!</p>!</body>

Page 30: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p class="greet">!!</p>!</body>

Page 31: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p class="greet">!!</p>!</body>

Page 32: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p class="greet">!!</p>!</body>

Page 33: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p class="greet">! Hello World!</p>!</body>

Page 34: XSLT for Web Developers

Identity Stylesheet in XSLT: How it Works

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!<p class="greet">! Hello World!</p>!</body>

Page 35: XSLT for Web Developers

Rhetorical Question #5: !

!

“OK, so how do you actually transform

the output”

Page 36: XSLT for Web Developers

You can override the identity templates with other, more specific matching templates (just as

you override rules with other rules in CSS)

CSS XSLT

* {! font-size: 10px;!}!!h1 {! /* Custom Handling */! font-size: 20px;!}

<xsl:template match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>!</xsl:template>!!<xsl:template match="h1">! <xsl:copy>! <!--Custom handling-->! <xsl:apply-templates/>! </xsl:copy>!</xsl:template>!!

Page 37: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="p[@class='greet']">! <h1>! <xsl:apply-templates select="@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

XPath matching all p elements with a class of “greet”

Page 38: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

Page 39: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

Page 40: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!!</body>

Page 41: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!!</body>

Page 42: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>!!</body>

Page 43: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1>!! </h1>!</body>

Page 44: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1>!! </h1>!</body>

Page 45: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1>!! </h1>!</body>

Page 46: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">!! </h1>!</body>

Page 47: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">!! </h1>!</body>

Page 48: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">!! </h1>!</body>

Page 49: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>!</body>

Page 50: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>!</body>

Page 51: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>!</body>

Page 52: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>! <p></p>!</body>

Page 53: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>! <p></p>!</body>

Page 54: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>! <p></p>!</body>

Page 55: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>! <p>What’s up?</p>!</body>

Page 56: XSLT for Web Developers

Our first transform: Convert all <p class="greet"> elements to <h1>s

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match=“p[@class=‘greet’]”>! <h1>! <xsl:apply-templates select=“@*|node()"/>! </h1>! </xsl:template>!</xsl:stylesheet>

<body>! <p class="greet">! Hello World! </p>! <p>What’s up?</p>!</body>

Stylesheet Input XHTML Output XHTML

<body>! <h1 class="greet">! Hello World! </h1>! <p>What’s up?</p>!</body>

Page 57: XSLT for Web Developers

Pop Quiz #2: What does this transform do?

XSLT INPUT XHTML

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Page 58: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

Page 59: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

Page 60: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p></p>

Page 61: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p></p>

Page 62: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p></p>

Page 63: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p>Learning</p>

Page 64: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p>Learning</p>

Page 65: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p>Learning</p>

Page 66: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p>Learning</p>

Page 67: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p>Learning is awesome!</p>

Page 68: XSLT for Web Developers

Pop Quiz #2 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">! <xsl:template ! match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong"/>!</xsl:stylesheet>

<p>Learning !<strong>XSLT</strong> is awesome!</p>

Stylesheet Input XHTML Output XHTML

<p>Learning is awesome!</p>

Page 69: XSLT for Web Developers

Pop Quiz #3: Write XSLT that drops <strong> tags from the HTML below, but

preserves the text content inside the tags

INPUT XHTML

<p>Learning !<strong>XSLT</strong> is awesome!</p>

DESIRED OUTPUT XHTML

<p>Learning XSLT is awesome!</p>

Page 70: XSLT for Web Developers

Pop Quiz #3

(Sandbox at: http://www.xsltcake.com/slices/8uJRj2)

Page 71: XSLT for Web Developers

Pop Quiz #3 Solution

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">!! <xsl:template match="@*|node()">! <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>!! <xsl:template match="strong">! <xsl:apply-templates/>! </xsl:template>!!</xsl:stylesheet>

BEGIN match “strong” element

Select child nodes (except attributes) of matched “strong” element

End match “strong” element

Page 72: XSLT for Web Developers

Next Steps: Intermediate Topics

•XPath expressions !•Conditionals (xsl:if, xsl:choose/xsl:when/xsl:otherwise)

!•Looping/Grouping(xsl:for-each, xsl:for-each-group)

!•Numbering/Labeling (xsl:number)

!•Includes/Imports (xsl:include/xsl:import)

Page 73: XSLT for Web Developers

My Favorite XSLT Reference Book

Page 74: XSLT for Web Developers

Thank You! !

Contact Me: @sandersk