SlideShare a Scribd company logo
1 of 74
Download to read offline
XSLT for Web Developers 
(in 30 minutes or less) 
Sanders Kleinfeld
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
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 
! 
(http://www.w3.org/TR/xslt-30) 
D: All of the above
XSLT is a tool for global, 
programmatic markup 
manipulation
 XSLT  
<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>!
Rhetorical Question #1: 
! 
! 
“Um, can’t I just use 
regular expressions 
for this?”
Example: 
Converting <b> to <em> with 
regex 
your_markup.replace(/<b>/, '<em>')
But what about: 
! 
• Closing tags (</b>) 
• Attributes (<b class="term">) 
• Extra Whitespace (<b >) 
your_markup.replace(/<(/)?b(s*[^>]*)>/g, '<$1em$2>')
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)
Rhetorical Question #2: 
! 
! 
“OK, how about a 
library like jQuery?”
This jQuery 
WILL NOT work: 
$('b').tag.prop("tagName") = "em" 
because tagName is 
read-only
However, there are jQuery workarounds… 
(http://stackoverflow.com/questions/3435871/jquery-how-to-change-tag-name)
…And other JS XML parsers 
(https://github.com/nfarina/xmldoc)
Rhetorical Question #3: 
! 
! 
“But for complex 
transforms, why not 
go ahead and learn 
XSLT?”
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.
Functional Paradigm 
var input; 
in JS 
function(input) {! 
// Some stuff! 
// happens here! 
return output;! 
} 
output
Functional Paradigm 
<markup>! 
<!--Input-->! 
</markup> 
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>
Identity Function* 
in JS 
function(a) {! 
return a;! 
} 
* Output of function is identical to input
Identity Stylesheet* 
in XSLT 
<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> 
* Output of stylesheet is identical to input
Rhetorical Question #4: 
! 
! 
! 
“Say What?”
Identity Stylesheet 
in XSLT: Explained 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/ 
XSL/Transform" version="1.0">! 
! 
<xsl:template match="@*|node()">! 
BEGIN matching function Match any node (element, attribute, text) 
<xsl:copy>! 
<xsl:apply-templates select="@*|node()"/>! 
</xsl:copy>! 
</xsl:template>! 
! 
</xsl:stylesheet> 
BEGIN stylesheet 
BEGIN copy matched node (OPEN elem) 
END Copy matched node (CLOSE elem) 
END stylesheet 
Select any node 
END matching function 
Run stylesheet against specified children of matched node
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p>! 
! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p>! 
! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p>! 
! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p class="greet">! 
! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p class="greet">! 
! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p class="greet">! 
! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body>
Identity Stylesheet in XSLT: How it Works 
Stylesheet Input XHTML Output 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:stylesheet> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body> 
<body>! 
<p class="greet">! 
Hello World! 
</p>! 
</body>
Rhetorical Question #5: 
! 
! 
“OK, so how do you 
actually transform 
the output”
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>! 
!
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>! 
XPath matching all p elements with a class of “greet” 
! 
<xsl:template match="p[@class='greet']">! 
<h1>! 
<xsl:apply-templates select="@*|node()"/>! 
</h1>! 
</xsl:template>! 
</xsl:stylesheet>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1>! 
! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1>! 
! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1>! 
! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
<p></p>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
<p></p>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
<p></p>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
<p>What’s up?</p>! 
</body>
Our first transform: Convert all 
<p class="greet"> elements to <h1>s 
Stylesheet Input XHTML Output 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=“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> 
<body>! 
<h1 class="greet">! 
Hello World! 
</h1>! 
<p>What’s up?</p>! 
</body>
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>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p></p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p></p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p></p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p>Learning</p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p>Learning</p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p>Learning</p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p>Learning</p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p>Learning is 
awesome!</p>
Pop Quiz #2 Solution 
Stylesheet Input XHTML Output 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> 
<p>Learning is 
awesome!</p>
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>
Pop Quiz #3 
(Sandbox at: http://www.xsltcake.com/slices/8uJRj2)
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
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)
My Favorite XSLT Reference Book
Thank You! 
! 
Contact Me: 
@sandersk

More Related Content

What's hot

The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)
ungerik
 
SPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentialsSPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentials
Mark Rackley
 

What's hot (19)

The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)
 
XSLT
XSLTXSLT
XSLT
 
Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)
 
Styling Components with JavaScript: MelbCSS Edition
Styling Components with JavaScript: MelbCSS EditionStyling Components with JavaScript: MelbCSS Edition
Styling Components with JavaScript: MelbCSS Edition
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
SPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentialsSPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentials
 
2001: Bridging the Gap between RSS and Java Old School Style
2001: Bridging the Gap between RSS and Java Old School Style2001: Bridging the Gap between RSS and Java Old School Style
2001: Bridging the Gap between RSS and Java Old School Style
 
CSS 201
CSS 201CSS 201
CSS 201
 
User Interface Development with jQuery
User Interface Development with jQueryUser Interface Development with jQuery
User Interface Development with jQuery
 
Nothing Hard Baked: Designing the Inclusive Web
Nothing Hard Baked: Designing the Inclusive WebNothing Hard Baked: Designing the Inclusive Web
Nothing Hard Baked: Designing the Inclusive Web
 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
 
DIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningDIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web Designing
 
SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery Essentials
 
Unit iv xml dom
Unit iv xml domUnit iv xml dom
Unit iv xml dom
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handling
 
The Inclusive Web: hands-on with HTML5 and jQuery
The Inclusive Web: hands-on with HTML5 and jQueryThe Inclusive Web: hands-on with HTML5 and jQuery
The Inclusive Web: hands-on with HTML5 and jQuery
 
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
2/15/2012 - Wrapping Your Head Around the SharePoint Beast2/15/2012 - Wrapping Your Head Around the SharePoint Beast
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
 
Cascading style sheets
Cascading style sheetsCascading style sheets
Cascading style sheets
 

Viewers also liked

Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
Katrien Verbert
 
Unleashing the Power of XSLT: Catalog Records in Batch
Unleashing the Power of XSLT: Catalog Records in BatchUnleashing the Power of XSLT: Catalog Records in Batch
Unleashing the Power of XSLT: Catalog Records in Batch
c7002593
 

Viewers also liked (20)

Is HTML5 the "Magic Bullet"?
Is HTML5 the "Magic Bullet"?Is HTML5 the "Magic Bullet"?
Is HTML5 the "Magic Bullet"?
 
Open Source for Publishing
Open Source for PublishingOpen Source for Publishing
Open Source for Publishing
 
XQuery - a technical overview
XQuery -  a technical overviewXQuery -  a technical overview
XQuery - a technical overview
 
HTML5 Is the Future of Book Authorship
HTML5 Is the Future of Book AuthorshipHTML5 Is the Future of Book Authorship
HTML5 Is the Future of Book Authorship
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
 
Jms deep dive [con4864]
Jms deep dive [con4864]Jms deep dive [con4864]
Jms deep dive [con4864]
 
Web Services
Web ServicesWeb Services
Web Services
 
Unleashing the Power of XSLT: Catalog Records in Batch
Unleashing the Power of XSLT: Catalog Records in BatchUnleashing the Power of XSLT: Catalog Records in Batch
Unleashing the Power of XSLT: Catalog Records in Batch
 
The Mystical Principles of XSLT: Enlightenment through Software Visualization
The Mystical Principles of XSLT: Enlightenment through Software VisualizationThe Mystical Principles of XSLT: Enlightenment through Software Visualization
The Mystical Principles of XSLT: Enlightenment through Software Visualization
 
Applying an IBM SOA Approach to Manual Processes Automation
Applying an IBM SOA Approach to Manual Processes AutomationApplying an IBM SOA Approach to Manual Processes Automation
Applying an IBM SOA Approach to Manual Processes Automation
 
XML - Displaying Data ith XSLT
XML - Displaying Data ith XSLTXML - Displaying Data ith XSLT
XML - Displaying Data ith XSLT
 
Xml part4
Xml part4Xml part4
Xml part4
 
Xml part5
Xml part5Xml part5
Xml part5
 
SOA Governance and WebSphere Service Registry and Repository
SOA Governance and WebSphere Service Registry and RepositorySOA Governance and WebSphere Service Registry and Repository
SOA Governance and WebSphere Service Registry and Repository
 
Interoperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSInteroperable Web Services with JAX-WS
Interoperable Web Services with JAX-WS
 
Open Id, O Auth And Webservices
Open Id, O Auth And WebservicesOpen Id, O Auth And Webservices
Open Id, O Auth And Webservices
 
Web Services
Web ServicesWeb Services
Web Services
 
Web services
Web servicesWeb services
Web services
 
WebService-Java
WebService-JavaWebService-Java
WebService-Java
 
CTDA Workshop on XSL
CTDA Workshop on XSLCTDA Workshop on XSL
CTDA Workshop on XSL
 

Similar to XSLT for Web Developers

Similar to XSLT for Web Developers (20)

Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit II
 
Transforming xml with XSLT
Transforming  xml with XSLTTransforming  xml with XSLT
Transforming xml with XSLT
 
XSLT. Basic.
XSLT. Basic.XSLT. Basic.
XSLT. Basic.
 
XML XSLT
XML XSLTXML XSLT
XML XSLT
 
Xslt
XsltXslt
Xslt
 
IWMW 2001: XML and XSLT
IWMW 2001: XML and XSLT IWMW 2001: XML and XSLT
IWMW 2001: XML and XSLT
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Xslt
XsltXslt
Xslt
 
Xslt
XsltXslt
Xslt
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
Xhtml Basics
Xhtml BasicsXhtml Basics
Xhtml Basics
 
Xhtml Basics
Xhtml BasicsXhtml Basics
Xhtml Basics
 
XSL- XSLT.pdf
XSL- XSLT.pdfXSL- XSLT.pdf
XSL- XSLT.pdf
 
Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4
 
XSLT and XPath - without the pain!
XSLT and XPath - without the pain!XSLT and XPath - without the pain!
XSLT and XPath - without the pain!
 
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriyaIntegrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
 
Rancangan Jaringan Komputer
Rancangan Jaringan KomputerRancangan Jaringan Komputer
Rancangan Jaringan Komputer
 
Introduction of xml and xslt
Introduction of xml and xsltIntroduction of xml and xslt
Introduction of xml and xslt
 
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
 
26xslt
26xslt26xslt
26xslt
 

Recently uploaded

Recently uploaded (20)

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 

XSLT for Web Developers

  • 1. XSLT for Web Developers (in 30 minutes or less) Sanders Kleinfeld
  • 2. 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
  • 3. 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 ! (http://www.w3.org/TR/xslt-30) D: All of the above
  • 4. XSLT is a tool for global, programmatic markup manipulation
  • 5.  XSLT  <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>!
  • 6. Rhetorical Question #1: ! ! “Um, can’t I just use regular expressions for this?”
  • 7. Example: Converting <b> to <em> with regex your_markup.replace(/<b>/, '<em>')
  • 8. But what about: ! • Closing tags (</b>) • Attributes (<b class="term">) • Extra Whitespace (<b >) your_markup.replace(/<(/)?b(s*[^>]*)>/g, '<$1em$2>')
  • 9. 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)
  • 10. Rhetorical Question #2: ! ! “OK, how about a library like jQuery?”
  • 11. This jQuery WILL NOT work: $('b').tag.prop("tagName") = "em" because tagName is read-only
  • 12. However, there are jQuery workarounds… (http://stackoverflow.com/questions/3435871/jquery-how-to-change-tag-name)
  • 13. …And other JS XML parsers (https://github.com/nfarina/xmldoc)
  • 14. Rhetorical Question #3: ! ! “But for complex transforms, why not go ahead and learn XSLT?”
  • 15. 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.
  • 16. Functional Paradigm var input; in JS function(input) {! // Some stuff! // happens here! return output;! } output
  • 17. Functional Paradigm <markup>! <!--Input-->! </markup> 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>
  • 18. Identity Function* in JS function(a) {! return a;! } * Output of function is identical to input
  • 19. Identity Stylesheet* in XSLT <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> * Output of stylesheet is identical to input
  • 20. Rhetorical Question #4: ! ! ! “Say What?”
  • 21. Identity Stylesheet in XSLT: Explained <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/ XSL/Transform" version="1.0">! ! <xsl:template match="@*|node()">! BEGIN matching function Match any node (element, attribute, text) <xsl:copy>! <xsl:apply-templates select="@*|node()"/>! </xsl:copy>! </xsl:template>! ! </xsl:stylesheet> BEGIN stylesheet BEGIN copy matched node (OPEN elem) END Copy matched node (CLOSE elem) END stylesheet Select any node END matching function Run stylesheet against specified children of matched node
  • 22. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body>
  • 23. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body>
  • 24. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! ! </body>
  • 25. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! ! </body>
  • 26. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! ! </body>
  • 27. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p>! ! </p>! </body>
  • 28. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p>! ! </p>! </body>
  • 29. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p>! ! </p>! </body>
  • 30. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p class="greet">! ! </p>! </body>
  • 31. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p class="greet">! ! </p>! </body>
  • 32. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p class="greet">! ! </p>! </body>
  • 33. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p class="greet">! Hello World! </p>! </body>
  • 34. Identity Stylesheet in XSLT: How it Works Stylesheet Input XHTML Output 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:stylesheet> <body>! <p class="greet">! Hello World! </p>! </body> <body>! <p class="greet">! Hello World! </p>! </body>
  • 35. Rhetorical Question #5: ! ! “OK, so how do you actually transform the output”
  • 36. 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>! !
  • 37. 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>! XPath matching all p elements with a class of “greet” ! <xsl:template match="p[@class='greet']">! <h1>! <xsl:apply-templates select="@*|node()"/>! </h1>! </xsl:template>! </xsl:stylesheet>
  • 38. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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>
  • 39. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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>
  • 40. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! ! </body>
  • 41. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! ! </body>
  • 42. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! ! </body>
  • 43. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1>! ! </h1>! </body>
  • 44. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1>! ! </h1>! </body>
  • 45. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1>! ! </h1>! </body>
  • 46. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! ! </h1>! </body>
  • 47. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! ! </h1>! </body>
  • 48. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! ! </h1>! </body>
  • 49. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! </body>
  • 50. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! </body>
  • 51. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! </body>
  • 52. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! <p></p>! </body>
  • 53. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! <p></p>! </body>
  • 54. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! <p></p>! </body>
  • 55. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! <p>What’s up?</p>! </body>
  • 56. Our first transform: Convert all <p class="greet"> elements to <h1>s Stylesheet Input XHTML Output 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=“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> <body>! <h1 class="greet">! Hello World! </h1>! <p>What’s up?</p>! </body>
  • 57. 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>
  • 58. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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>
  • 59. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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>
  • 60. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p></p>
  • 61. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p></p>
  • 62. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p></p>
  • 63. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p>Learning</p>
  • 64. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p>Learning</p>
  • 65. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p>Learning</p>
  • 66. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p>Learning</p>
  • 67. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p>Learning is awesome!</p>
  • 68. Pop Quiz #2 Solution Stylesheet Input XHTML Output 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> <p>Learning is awesome!</p>
  • 69. 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>
  • 70. Pop Quiz #3 (Sandbox at: http://www.xsltcake.com/slices/8uJRj2)
  • 71. 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
  • 72. 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)
  • 73. My Favorite XSLT Reference Book
  • 74. Thank You! ! Contact Me: @sandersk