SlideShare uma empresa Scribd logo
1 de 34
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais 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
 

Mais 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
 

Último

How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 

Último (20)

How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 

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