Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

XSLT and XPath - without the pain!

Slides of my presentations at ApacheCon EU 07, see, and at the Cocoon GetTogether 2007, see

Related Books

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

XSLT and XPath - without the pain!

  1. 1. XSLT and XPath without the pain! Bertrand Delacrétaz, Senior R&D Developer, (blog) slides revision: 2007-11-10
  2. 2. Goal Learning to learn XPath and XSLT because it takes time... also applies to teaching it and includes some patterns
  3. 3. What is XML? Procedural code does not apply here... photo:aameris on
  4. 4. XPath First things first...but do we use XPath?
  5. 5. 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!
  6. 6. XPath works on trees wikipedia:Tree_structure
  7. 7. The XPath data model Seven types of nodes in our tree: 4 root node element nodes text nodes attribute nodes 3 comment nodes namespace nodes processing instruction nodes ...adressed with various non-obvious notations
  8. 8. Common node types r e <document> a 4 <paragraph style=quot;headingquot;> <!-- some comment here c--> Once upon a time... t </paragraph> </document> r “root” is the root of the XPath tree, *not* the root element a attributes are “under” elements in the tree: p/@id
  9. 9. XPath: relative and absolute “paths” /document/section //section ./section .//section //section/para/text() paths are actually queries in the tree! the dot “.” is the all-important “context node”
  10. 10. XPath predicates /document[@id=’12’] section[para] section[para]//div[style=’abc’] para[contains(. , ’help’)] *[self::para and @style] The “where clause” of XPath
  11. 11. XPath axes parent::section[1] ancestor::section[@style] following-sibling::para preceding-sibling::section/para following::para[@id=’42’] following::*[1]/parent::p And more axes: hierarchy, flat list, siblings
  12. 12. are we painless enough?
  13. 13. XPath is the most important part of XSLT! it is.
  14. 14. XPath summary The Tree Model Relative and absolute paths Expressions Predicates Axes (some) functions REQUIRED knowledge for painless XSLT!
  15. 15. XSLT It gets easier...
  16. 16. XPath strikes again... <xsl:template match=quot;section[para[normalize-space(.)]]quot;> <div class=quot;{@style}quot;> <h1><xsl:value-of select=quot;titlequot;/></h1> <xsl:apply-templates select=quot;para[@ok='true']quot;/> </div> </xsl:template> not much XSLT in this...
  17. 17. XSLT learning subset XPath and the tree model Namespaces (basic knowledge) The XSLT Processor Scenario xsl:template xsl:apply-templates xsl:value-of, dynamic attributes And later: xsl:variable and xsl:param xsl:call-template, xsl:key, modes xsl:include and xsl:import
  18. 18. Forget about... xsl:if xsl:choose xsl:for-each For as long as you can: those don’t climb trees well...
  19. 19. important stuff ahead WAKE UP! photo:nasirkhan on
  20. 20. The XSLT Processor 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? XPath! -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=”/” /> ?
  21. 21. XSLT Default Rules If no template applies... The root node is visited Element nodes are visited Text nodes are copied All other nodes are ignored <xsl:transform xmlns:xsl=quot;http://...Transformquot; version=quot;1.0quot;/>
  22. 22. XSLT summary XPath Learning subset XSLT Processor Scenario Default Rules are we painless enough?
  23. 23. XSLT 2.0 ? Regular Expressions Built-in grouping xsl:function Sequences Sorting with collations Schema support, typed variables And more... It’s all good news, but the basic principles remain!
  24. 24. Patterns (and anti-patterns) symbols:ppdigital and o0o0xmods0o0oon
  25. 25. Dynamic attributes <xsl:template match=quot;somethingquot;> <output href=quot;{@url}quot;/> </xsl:template> xsl:attribute is most often not needed!
  26. 26. The Useless Template <xsl:template match=quot;somethingquot;> <xsl:apply-templates/> </xsl:template> <xsl:template match=quot;text()quot;> <xsl:copy-of select=quot;.quot;/> </xsl:template> no need to recreate the default rules!
  27. 27. Don’t choose: use more templates! <xsl:template match=quot;somethingquot;> <xsl:choose> <xsl:when test=quot;@idquot;> 1 </xsl:when> <xsl:otherwise> 2 </xsl:otherwise> </xsl:choose> <xsl:template match=quot;something[@id]quot;> </xsl:template> 1 </xsl:template> <xsl:template match=quot;somethingquot;> 2 </xsl:template>
  28. 28. Don’t loop: apply-templates! <xsl:for-each select=quot;somethingquot;> ..process something </xsl:for-each> <xsl:apply-templates select=quot;somethingquot;/> <xsl:template match=quot;somethingquot;> ..process something </xsl:template> in the general case at least...
  29. 29. I’ll trade an xsl:if... <xsl:template match=quot;divquot;> ...process div <xsl:if test=quot;@class = 'SPECIAL'quot;> ...process div having SPECIAL class </xsl:if> </xsl:template it’s not *that* bad with just one xsl:if...
  30. 30. ...for a modal template <xsl:template match=quot;divquot;> ...process div <xsl:apply-templates select=quot;.quot; mode=quot;div.extraquot;/> </xsl:template> <xsl:template match=quot;divquot; mode=quot;div.extraquot;/> <xsl:template match=quot;div[@class='SPECIAL']quot; mode=quot;div.extraquot;> ...process div having SPECIAL class </xsl:template> much more extensible!
  31. 31. Named templates as reusable blocks <xsl:template match=quot;somethingquot;> <xsl:call-template name=quot;somePartOnequot;/> <xsl:call-template name=quot;somePartTwoquot;/> </xsl:template> <xsl:template name=quot;somePartOnequot;> ... </xsl:template> <xsl:template name=quot;somePartTwoquot;> ... </xsl:template>
  32. 32. Match that root! <xsl:template match=quot;/quot;> <xsl:call-template name=quot;input.sanity.checkquot;/> <output-root-element> <xsl:apply-templates/> </output-root-element> </xsl:template> <xsl:template name=quot;input.sanity.checkquot;> <xsl:if test=quot;not(/something)quot;> <xsl:message terminate=quot;yesquot;> Error: expected quot;somethingquot; as the root element </xsl:message> </xsl:if> </xsl:template>
  33. 33. So, where’s the pain?
  34. 34. Where’s the pain? a.k.a “conclusion” Not knowing XPath well Not understanding the Processor Scenario Not creating enough templates Abusing “procedural” constructs WDYT? See you at the booth! Read my blog at Michael Kay, my favorite XSLT book

    Be the first to comment

    Login to see the comments

  • lars3loff

    Oct. 7, 2007
  • hubslides

    Oct. 25, 2007
  • tmahesh

    Feb. 3, 2008
  • prasadbcs

    Apr. 14, 2008
  • anboss

    Sep. 10, 2008
  • elfpavlik

    Oct. 10, 2010
  • deathy

    Nov. 7, 2010
  • andrea.mele

    Dec. 14, 2010
  • lapistano

    Jan. 10, 2011
  • ViniciusPaluch

    Mar. 15, 2011
  • vanthubeloved

    Mar. 24, 2011
  • edouardlopez

    Oct. 10, 2011
  • EmmanuelleMorlock

    Dec. 6, 2011
  • geoffreymouton9

    Jun. 3, 2014
  • danielheidecke

    Jun. 17, 2014
  • slkn8276

    Nov. 5, 2014
  • gcycopy

    Apr. 13, 2016
  • EvanLiu11

    Apr. 10, 2017
  • mohashems

    Sep. 12, 2017

Slides of my presentations at ApacheCon EU 07, see, and at the Cocoon GetTogether 2007, see


Total views


On Slideshare


From embeds


Number of embeds