SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
XSLT and XPath
  without the pain!
Bertrand Delacrétaz, bdelacretaz@apache.org
    Senior R&D Developer, www.day.com


    www.codeconsult.ch/bertrand (blog)
              slides revision: 2007-11-10
Goal
Learning to learn
XPath and XSLT

     because it takes time...
    also applies to teaching it
   and includes some patterns
What is XML?




Procedural code does not apply here...
                                photo:aameris on morguefile.com
XPath
First things first...but do we use XPath?
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!
XPath works on trees




                  wikipedia:Tree_structure
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
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
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”
XPath predicates

/document[@id=’12’]
section[para]
section[para]//div[style=’abc’]
para[contains(. , ’help’)]
*[self::para and @style]

       The “where clause” of XPath
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
are we
painless
enough?
XPath is the
    most
important part
  of XSLT!
      it is.
XPath summary

The Tree Model
Relative and absolute paths
Expressions
Predicates
Axes
(some) functions

REQUIRED knowledge for painless XSLT!
XSLT
It gets easier...
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...
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
Forget about...

            xsl:if
            xsl:choose
            xsl:for-each




For as long as you can: those don’t climb trees well...
important stuff ahead




WAKE UP!
                               photo:nasirkhan on morguefile.com
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=”/” /> ?
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;/>
XSLT summary

XPath
Learning subset
XSLT Processor Scenario
Default Rules


     are we painless enough?
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!
Patterns
(and anti-patterns)

         symbols:ppdigital and o0o0xmods0o0oon morguefile.com
Dynamic attributes


<xsl:template match=quot;somethingquot;>
  <output href=quot;{@url}quot;/>
 </xsl:template>




xsl:attribute is most often not needed!
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!
Don’t choose:
          use more templates!
<xsl:template match=quot;somethingquot;>
 <xsl:choose>
  <xsl:when test=quot;@idquot;>
   ...case 1
  </xsl:when>
  <xsl:otherwise>
   ...case 2
  </xsl:otherwise>
 </xsl:choose>        <xsl:template match=quot;something[@id]quot;>
</xsl:template>
                     ...case 1
                   </xsl:template>

                   <xsl:template match=quot;somethingquot;>
                     ...case 2
                   </xsl:template>
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...
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...
...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!
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>
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>
So, where’s the pain?
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,
www.codeconsult.ch/bertrand            my favorite XSLT book

Contenu connexe

Tendances (20)

Xpath
XpathXpath
Xpath
 
XSLT. Basic.
XSLT. Basic.XSLT. Basic.
XSLT. Basic.
 
Querring xml with xpath
Querring xml with xpath Querring xml with xpath
Querring xml with xpath
 
XML and XSLT
XML and XSLTXML and XSLT
XML and XSLT
 
transforming xml using xsl and xslt
transforming xml using xsl and xslttransforming xml using xsl and xslt
transforming xml using xsl and xslt
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
XSLT
XSLTXSLT
XSLT
 
Xslt
XsltXslt
Xslt
 
XSLT
XSLTXSLT
XSLT
 
X FILES
X FILESX FILES
X FILES
 
Session 4
Session 4Session 4
Session 4
 
XML XSLT
XML XSLTXML XSLT
XML XSLT
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Xslt
XsltXslt
Xslt
 
XMLT
XMLTXMLT
XMLT
 
XPATH
XPATHXPATH
XPATH
 
XML and XPath details
XML and XPath detailsXML and XPath details
XML and XPath details
 
XPath Introduction
XPath IntroductionXPath Introduction
XPath Introduction
 
Rendering XML Document
Rendering XML DocumentRendering XML Document
Rendering XML Document
 
Xpath
XpathXpath
Xpath
 

En vedette

Break My Site
Break My SiteBreak My Site
Break My Sitemr.quinn
 
DITA-OT 2.x: Discover What's New in Toolkit Two
DITA-OT 2.x: Discover What's New in Toolkit TwoDITA-OT 2.x: Discover What's New in Toolkit Two
DITA-OT 2.x: Discover What's New in Toolkit TwoRobert Anderson
 
XSLT 2010-03-03
XSLT 2010-03-03XSLT 2010-03-03
XSLT 2010-03-03kmiyako
 
Down the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM WonderlandDown the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM WonderlandCharles Nutter
 
Idiomatic gradle plugin writing
Idiomatic gradle plugin writingIdiomatic gradle plugin writing
Idiomatic gradle plugin writingSchalk Cronjé
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Gregg Donovan
 
XML Amsterdam 2012 Keynote
XML Amsterdam 2012 KeynoteXML Amsterdam 2012 Keynote
XML Amsterdam 2012 Keynotejimfuller2009
 
Upgrading PDF Plugins to DITA_DITA-OT Day 2016
Upgrading PDF Plugins to DITA_DITA-OT Day 2016Upgrading PDF Plugins to DITA_DITA-OT Day 2016
Upgrading PDF Plugins to DITA_DITA-OT Day 2016IXIASOFT
 

En vedette (10)

Hands On Cocoon
Hands On CocoonHands On Cocoon
Hands On Cocoon
 
Break My Site
Break My SiteBreak My Site
Break My Site
 
DITA-OT 2.x: Discover What's New in Toolkit Two
DITA-OT 2.x: Discover What's New in Toolkit TwoDITA-OT 2.x: Discover What's New in Toolkit Two
DITA-OT 2.x: Discover What's New in Toolkit Two
 
XSLT 2010-03-03
XSLT 2010-03-03XSLT 2010-03-03
XSLT 2010-03-03
 
Down the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM WonderlandDown the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM Wonderland
 
Idiomatic gradle plugin writing
Idiomatic gradle plugin writingIdiomatic gradle plugin writing
Idiomatic gradle plugin writing
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
 
XML Amsterdam 2012 Keynote
XML Amsterdam 2012 KeynoteXML Amsterdam 2012 Keynote
XML Amsterdam 2012 Keynote
 
Upgrading PDF Plugins to DITA_DITA-OT Day 2016
Upgrading PDF Plugins to DITA_DITA-OT Day 2016Upgrading PDF Plugins to DITA_DITA-OT Day 2016
Upgrading PDF Plugins to DITA_DITA-OT Day 2016
 
XML Xpath & XSLT
XML  Xpath & XSLTXML  Xpath & XSLT
XML Xpath & XSLT
 

Similaire à XSLT and XPath - without the pain!

Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIRoselin Mary S
 
SXML: S-expression eXtensible Markup Language
SXML: S-expression eXtensible Markup LanguageSXML: S-expression eXtensible Markup Language
SXML: S-expression eXtensible Markup Languageelliando dias
 
XSLT 3.0 - new features
XSLT 3.0 - new featuresXSLT 3.0 - new features
XSLT 3.0 - new featuresJakub Malý
 
In Pursuit of the Grand Unified Template
In Pursuit of the Grand Unified TemplateIn Pursuit of the Grand Unified Template
In Pursuit of the Grand Unified Templatehannonhill
 
"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slidesRussell Ward
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For XmlLars Trieloff
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)Serhii Kartashov
 
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld BookNet Canada
 
Service Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSLService Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSLRoselin Mary S
 
Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4Stephan Schmidt
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsAnton Keks
 
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. VijiPriyaVijiPriya Jeyamani
 

Similaire à XSLT and XPath - without the pain! (20)

Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit II
 
SXML: S-expression eXtensible Markup Language
SXML: S-expression eXtensible Markup LanguageSXML: S-expression eXtensible Markup Language
SXML: S-expression eXtensible Markup Language
 
XSLT 3.0 - new features
XSLT 3.0 - new featuresXSLT 3.0 - new features
XSLT 3.0 - new features
 
In Pursuit of the Grand Unified Template
In Pursuit of the Grand Unified TemplateIn Pursuit of the Grand Unified Template
In Pursuit of the Grand Unified Template
 
Xslt
XsltXslt
Xslt
 
"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For Xml
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
 
XSLT for Web Developers
XSLT for Web DevelopersXSLT for Web Developers
XSLT for Web Developers
 
Ot performance webinar
Ot performance webinarOt performance webinar
Ot performance webinar
 
Service Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSLService Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSL
 
XPATH_XSLT-1.pptx
XPATH_XSLT-1.pptxXPATH_XSLT-1.pptx
XPATH_XSLT-1.pptx
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4
 
Xml 2
Xml  2 Xml  2
Xml 2
 
Xml p5 Lecture Notes
Xml p5 Lecture NotesXml p5 Lecture Notes
Xml p5 Lecture Notes
 
Day2 xslt x_path_xquery
Day2 xslt x_path_xqueryDay2 xslt x_path_xquery
Day2 xslt x_path_xquery
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & Servlets
 
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
 

Plus de Bertrand Delacretaz

VanillaJS & the Web Platform, a match made in heaven?
VanillaJS & the Web Platform, a match made in heaven?VanillaJS & the Web Platform, a match made in heaven?
VanillaJS & the Web Platform, a match made in heaven?Bertrand Delacretaz
 
Surviving large online communities with conciseness and clarity
Surviving large online communities with conciseness and clarity Surviving large online communities with conciseness and clarity
Surviving large online communities with conciseness and clarity Bertrand Delacretaz
 
Repoinit: a mini-language for content repository initialization
Repoinit: a mini-language for content repository initializationRepoinit: a mini-language for content repository initialization
Repoinit: a mini-language for content repository initializationBertrand Delacretaz
 
The Moving House Model, adhocracy and remote collaboration
The Moving House Model, adhocracy and remote collaborationThe Moving House Model, adhocracy and remote collaboration
The Moving House Model, adhocracy and remote collaborationBertrand Delacretaz
 
GraphQL in Apache Sling - but isn't it the opposite of REST?
GraphQL in Apache Sling - but isn't it the opposite of REST?GraphQL in Apache Sling - but isn't it the opposite of REST?
GraphQL in Apache Sling - but isn't it the opposite of REST?Bertrand Delacretaz
 
How to convince your left brain (or manager) to follow the Open Source path t...
How to convince your left brain (or manager) to follow the Open Source path t...How to convince your left brain (or manager) to follow the Open Source path t...
How to convince your left brain (or manager) to follow the Open Source path t...Bertrand Delacretaz
 
L'Open Source change le Monde - BlendWebMix 2019
L'Open Source change le Monde - BlendWebMix 2019L'Open Source change le Monde - BlendWebMix 2019
L'Open Source change le Monde - BlendWebMix 2019Bertrand Delacretaz
 
Shared Neurons - the Secret Sauce of Open Source communities?
Shared Neurons - the Secret Sauce of Open Source communities?Shared Neurons - the Secret Sauce of Open Source communities?
Shared Neurons - the Secret Sauce of Open Source communities?Bertrand Delacretaz
 
Sling and Serverless, Best Friends Forever?
Sling and Serverless, Best Friends Forever?Sling and Serverless, Best Friends Forever?
Sling and Serverless, Best Friends Forever?Bertrand Delacretaz
 
Serverless - introduction et perspectives concrètes
Serverless - introduction et perspectives concrètesServerless - introduction et perspectives concrètes
Serverless - introduction et perspectives concrètesBertrand Delacretaz
 
State of the Feather - ApacheCon North America 2018
State of the Feather - ApacheCon North America 2018State of the Feather - ApacheCon North America 2018
State of the Feather - ApacheCon North America 2018Bertrand Delacretaz
 
Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Bertrand Delacretaz
 
Open Source at Scale: the Apache Software Foundation (2018)
Open Source at Scale: the Apache Software Foundation (2018)Open Source at Scale: the Apache Software Foundation (2018)
Open Source at Scale: the Apache Software Foundation (2018)Bertrand Delacretaz
 
They don't understand me! Tales from the multi-cultural trenches
They don't understand me! Tales from the multi-cultural trenchesThey don't understand me! Tales from the multi-cultural trenches
They don't understand me! Tales from the multi-cultural trenchesBertrand Delacretaz
 
Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)
Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)
Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)Bertrand Delacretaz
 
Project and Community Services the Apache Way
Project and Community Services the Apache WayProject and Community Services the Apache Way
Project and Community Services the Apache WayBertrand Delacretaz
 
La Fondation Apache - keynote au Paris Open Source Summit 2017
La Fondation Apache - keynote au Paris Open Source Summit 2017La Fondation Apache - keynote au Paris Open Source Summit 2017
La Fondation Apache - keynote au Paris Open Source Summit 2017Bertrand Delacretaz
 
Asynchronous Decision Making - FOSS Backstage 2017
Asynchronous Decision Making - FOSS Backstage 2017Asynchronous Decision Making - FOSS Backstage 2017
Asynchronous Decision Making - FOSS Backstage 2017Bertrand Delacretaz
 
Building an Apache Sling Rendering Farm
Building an Apache Sling Rendering FarmBuilding an Apache Sling Rendering Farm
Building an Apache Sling Rendering FarmBertrand Delacretaz
 

Plus de Bertrand Delacretaz (20)

VanillaJS & the Web Platform, a match made in heaven?
VanillaJS & the Web Platform, a match made in heaven?VanillaJS & the Web Platform, a match made in heaven?
VanillaJS & the Web Platform, a match made in heaven?
 
Surviving large online communities with conciseness and clarity
Surviving large online communities with conciseness and clarity Surviving large online communities with conciseness and clarity
Surviving large online communities with conciseness and clarity
 
Repoinit: a mini-language for content repository initialization
Repoinit: a mini-language for content repository initializationRepoinit: a mini-language for content repository initialization
Repoinit: a mini-language for content repository initialization
 
The Moving House Model, adhocracy and remote collaboration
The Moving House Model, adhocracy and remote collaborationThe Moving House Model, adhocracy and remote collaboration
The Moving House Model, adhocracy and remote collaboration
 
GraphQL in Apache Sling - but isn't it the opposite of REST?
GraphQL in Apache Sling - but isn't it the opposite of REST?GraphQL in Apache Sling - but isn't it the opposite of REST?
GraphQL in Apache Sling - but isn't it the opposite of REST?
 
Open Source Changes the World!
Open Source Changes the World!Open Source Changes the World!
Open Source Changes the World!
 
How to convince your left brain (or manager) to follow the Open Source path t...
How to convince your left brain (or manager) to follow the Open Source path t...How to convince your left brain (or manager) to follow the Open Source path t...
How to convince your left brain (or manager) to follow the Open Source path t...
 
L'Open Source change le Monde - BlendWebMix 2019
L'Open Source change le Monde - BlendWebMix 2019L'Open Source change le Monde - BlendWebMix 2019
L'Open Source change le Monde - BlendWebMix 2019
 
Shared Neurons - the Secret Sauce of Open Source communities?
Shared Neurons - the Secret Sauce of Open Source communities?Shared Neurons - the Secret Sauce of Open Source communities?
Shared Neurons - the Secret Sauce of Open Source communities?
 
Sling and Serverless, Best Friends Forever?
Sling and Serverless, Best Friends Forever?Sling and Serverless, Best Friends Forever?
Sling and Serverless, Best Friends Forever?
 
Serverless - introduction et perspectives concrètes
Serverless - introduction et perspectives concrètesServerless - introduction et perspectives concrètes
Serverless - introduction et perspectives concrètes
 
State of the Feather - ApacheCon North America 2018
State of the Feather - ApacheCon North America 2018State of the Feather - ApacheCon North America 2018
State of the Feather - ApacheCon North America 2018
 
Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?
 
Open Source at Scale: the Apache Software Foundation (2018)
Open Source at Scale: the Apache Software Foundation (2018)Open Source at Scale: the Apache Software Foundation (2018)
Open Source at Scale: the Apache Software Foundation (2018)
 
They don't understand me! Tales from the multi-cultural trenches
They don't understand me! Tales from the multi-cultural trenchesThey don't understand me! Tales from the multi-cultural trenches
They don't understand me! Tales from the multi-cultural trenches
 
Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)
Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)
Prise de Décisions Asynchrone, Devoxx France 2018 (avec vidéo)
 
Project and Community Services the Apache Way
Project and Community Services the Apache WayProject and Community Services the Apache Way
Project and Community Services the Apache Way
 
La Fondation Apache - keynote au Paris Open Source Summit 2017
La Fondation Apache - keynote au Paris Open Source Summit 2017La Fondation Apache - keynote au Paris Open Source Summit 2017
La Fondation Apache - keynote au Paris Open Source Summit 2017
 
Asynchronous Decision Making - FOSS Backstage 2017
Asynchronous Decision Making - FOSS Backstage 2017Asynchronous Decision Making - FOSS Backstage 2017
Asynchronous Decision Making - FOSS Backstage 2017
 
Building an Apache Sling Rendering Farm
Building an Apache Sling Rendering FarmBuilding an Apache Sling Rendering Farm
Building an Apache Sling Rendering Farm
 

Dernier

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Dernier (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

XSLT and XPath - without the pain!

  • 1. XSLT and XPath without the pain! Bertrand Delacrétaz, bdelacretaz@apache.org Senior R&D Developer, www.day.com www.codeconsult.ch/bertrand (blog) slides revision: 2007-11-10
  • 2. Goal Learning to learn XPath and XSLT because it takes time... also applies to teaching it and includes some patterns
  • 3. What is XML? Procedural code does not apply here... photo:aameris on morguefile.com
  • 5. 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!
  • 6. XPath works on trees wikipedia:Tree_structure
  • 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. 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. 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. XPath predicates /document[@id=’12’] section[para] section[para]//div[style=’abc’] para[contains(. , ’help’)] *[self::para and @style] The “where clause” of XPath
  • 13. XPath is the most important part of XSLT! it is.
  • 14. XPath summary The Tree Model Relative and absolute paths Expressions Predicates Axes (some) functions REQUIRED knowledge for painless XSLT!
  • 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. 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. Forget about... xsl:if xsl:choose xsl:for-each For as long as you can: those don’t climb trees well...
  • 19. important stuff ahead WAKE UP! photo:nasirkhan on morguefile.com
  • 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. 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. XSLT summary XPath Learning subset XSLT Processor Scenario Default Rules are we painless enough?
  • 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. Patterns (and anti-patterns) symbols:ppdigital and o0o0xmods0o0oon morguefile.com
  • 25. Dynamic attributes <xsl:template match=quot;somethingquot;> <output href=quot;{@url}quot;/> </xsl:template> xsl:attribute is most often not needed!
  • 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. Don’t choose: use more templates! <xsl:template match=quot;somethingquot;> <xsl:choose> <xsl:when test=quot;@idquot;> ...case 1 </xsl:when> <xsl:otherwise> ...case 2 </xsl:otherwise> </xsl:choose> <xsl:template match=quot;something[@id]quot;> </xsl:template> ...case 1 </xsl:template> <xsl:template match=quot;somethingquot;> ...case 2 </xsl:template>
  • 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. 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. ...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. 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. 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>
  • 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, www.codeconsult.ch/bertrand my favorite XSLT book