Download - XSLT and XPath - without the pain!
![Page 1: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/1.jpg)
XSLT and XPathwithout the pain!
Bertrand Delacrétaz, [email protected] Senior R&D Developer, www.day.com
www.codeconsult.ch/bertrand (blog)slides revision: 2007-11-10
![Page 2: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/2.jpg)
Goal
Learning to learnXPath and XSLT
also applies to teaching it
because it takes time...
and includes some patterns
![Page 3: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/3.jpg)
What is XML?
Procedural code does not apply here...
photo:aameris on morguefile.com
![Page 4: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/4.jpg)
XPath
First things first...but do we use XPath?
![Page 5: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/5.jpg)
XPath
http://www.w3.org/TR/xpath says:
...The purpose of XPath is to address parts of an XML document...
...XPath operates on the abstract, logical structure of an XML document,
rather than its surface syntax....
divide et impera!
![Page 6: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/6.jpg)
XPath works on trees
wikipedia:Tree_structure
![Page 7: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/7.jpg)
The XPath data model
Seven types of nodes in our tree:
root nodeelement nodestext nodesattribute nodes
comment nodes namespace nodesprocessing instruction nodes
...adressed with various non-obvious notations
4
3
![Page 8: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/8.jpg)
Common node types
<document> <paragraph style="heading"> <!-- some comment here --> Once upon a time... </paragraph></document>
e
a
c
t
r
r
a
“root” is the root of the XPath tree, *not* the root element
attributes are “under” elements in the tree: p/@id
4
![Page 9: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/9.jpg)
XPath: relative and absolute “paths”
/document/section//section./section.//section//section/para/text()
the dot “.” is the all-important “context node”
paths are actually queries in the tree!
![Page 10: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/10.jpg)
XPath predicates
The “where clause” of XPath
/document[@id=’12’]section[para]section[para]//div[style=’abc’]para[contains(. , ’help’)]*[self::para and @style]
![Page 11: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/11.jpg)
XPath axes
parent::section[1]ancestor::section[@style]following-sibling::parapreceding-sibling::section/parafollowing::para[@id=’42’]following::*[1]/parent::p
And more axes: hierarchy, flat list, siblings
![Page 12: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/12.jpg)
are wepainlessenough?
![Page 13: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/13.jpg)
XPath is the most
important partof XSLT!
it is.
![Page 14: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/14.jpg)
XPath summary
The Tree ModelRelative and absolute pathsExpressionsPredicatesAxes(some) functions
REQUIRED knowledge for painless XSLT!
![Page 15: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/15.jpg)
XSLT
It gets easier...
![Page 16: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/16.jpg)
XPath strikes again...
<xsl:template match="section[para[normalize-space(.)]]"> <div class="{@style}"> <h1><xsl:value-of select="title"/></h1>
<xsl:apply-templates select="para[@ok='true']"/> </div>
</xsl:template>
not much XSLT in this...
![Page 17: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/17.jpg)
XSLT learning subset
XPath and the tree modelNamespaces (basic knowledge)The XSLT Processor Scenario
xsl:template
xsl:apply-templatesxsl:value-of, dynamic attributes
And later:xsl:variable and xsl:paramxsl:call-template, xsl:key, modesxsl:include and xsl:import
![Page 18: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/18.jpg)
Forget about...
xsl:ifxsl:choosexsl:for-each
For as long as you can: those don’t climb trees well...
![Page 19: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/19.jpg)
WAKE UP!
important stuff ahead
photo:nasirkhan on morguefile.com
![Page 20: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/20.jpg)
The XSLTProcessor Scenario
Start by visiting the root node.
For each node that is visited: - Set the node as the “context node” - Do we have a matching template? -YES: execute the template, all done. -NO: apply the default rules.
If YES causes nodes to be visited, repeat. But in the YES case, default rules don’t apply.
How about <xsl:template match=”/” /> ?
XPath!
![Page 21: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/21.jpg)
XSLT Default Rules
If no template applies...
The root node is visitedElement nodes are visitedText nodes are copiedAll other nodes are ignored
<xsl:transform xmlns:xsl="http://...Transform" version="1.0"/>
![Page 22: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/22.jpg)
XSLT summary
XPathLearning subsetXSLT Processor ScenarioDefault Rules
are we painless enough?
![Page 23: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/23.jpg)
XSLT 2.0 ?
And more...
Sorting with collations
Sequences
Schema support, typed variables
xsl:function
Regular ExpressionsBuilt-in grouping
It’s all good news, but the basic principles remain!
![Page 24: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/24.jpg)
Patterns(and anti-patterns)
symbols:ppdigital and o0o0xmods0o0oon morguefile.com
![Page 25: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/25.jpg)
Dynamic attributes
<xsl:template match="something"> <output href="{@url}"/> </xsl:template>
xsl:attribute is most often not needed!
![Page 26: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/26.jpg)
The Useless Template
<xsl:template match="something"> <xsl:apply-templates/></xsl:template> <xsl:template match="text()"> <xsl:copy-of select="."/> </xsl:template>
no need to recreate the default rules!
![Page 27: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/27.jpg)
Don’t choose:use more templates!
<xsl:template match="something"> <xsl:choose> <xsl:when test="@id"> ...case 1 </xsl:when> <xsl:otherwise> ...case 2 </xsl:otherwise> </xsl:choose> </xsl:template>
<xsl:template match="something[@id]"> ...case 1
</xsl:template>
<xsl:template match="something"> ...case 2
</xsl:template>
![Page 28: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/28.jpg)
Don’t loop:apply-templates!
<xsl:for-each select="something"> ..process something </xsl:for-each>
<xsl:apply-templates select="something"/> <xsl:template match="something"> ..process something </xsl:template>
in the general case at least...
![Page 29: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/29.jpg)
I’ll trade an xsl:if...
<xsl:template match="div"> ...process div <xsl:if test="@class = 'SPECIAL'"> ...process div having SPECIAL class </xsl:if> </xsl:template
it’s not *that* bad with just one xsl:if...
![Page 30: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/30.jpg)
...for a modal template
<xsl:template match="div"> ...process div <xsl:apply-templates select="." mode="div.extra"/> </xsl:template> <xsl:template match="div" mode="div.extra"/> <xsl:template match="div[@class='SPECIAL']" mode="div.extra"> ...process div having SPECIAL class </xsl:template>
much moreextensible!
![Page 31: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/31.jpg)
Named templatesas reusable blocks
<xsl:template match="something"> <xsl:call-template name="somePartOne"/>
<xsl:call-template name="somePartTwo"/> </xsl:template>
<xsl:template name="somePartOne">
... </xsl:template>
<xsl:template name="somePartTwo">
... </xsl:template>
![Page 32: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/32.jpg)
Match that root!
<xsl:template match="/"> <xsl:call-template name="input.sanity.check"/> <output-root-element> <xsl:apply-templates/> </output-root-element> </xsl:template> <xsl:template name="input.sanity.check"> <xsl:if test="not(/something)"> <xsl:message terminate="yes"> Error: expected "something" as the root element </xsl:message> </xsl:if> </xsl:template>
![Page 33: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/33.jpg)
So, where’s the pain?
![Page 34: XSLT and XPath - without the pain!](https://reader034.vdocuments.us/reader034/viewer/2022042714/54be79164a795913778b46d5/html5/thumbnails/34.jpg)
Where’s the pain?a.k.a “conclusion”
Not knowing XPath wellNot understanding the Processor Scenario
Not creating enough templatesAbusing “procedural” constructs
WDYT?
Read my blog at www.codeconsult.ch/bertrand
Michael Kay,my favorite XSLT book
See you at the booth!