24/02/2010 Analyse of a XPath query

The version 6 of G-Truc Creation was a great opportunity for to refresh my knowledge of XSLT and XPath.

It's amazing how these two languages are powerfull, a good solution always end up with few code.

  • $Post[./tag[contains(., 'gtc-tech-comment')]][$Count>=position()]

Select $Count first elements of $Post which contains a child element 'tag' which value contains the string-value 'gtc-tech-comment'.

In my real code I prefer to use variable as much as I can so that for example 'gtc-tech-comment' would be replace by one. When an XPath expression is too long I would cut it in several parts. Here $Post[./tag[contains(., 'gtc-tech-comment')]] could be place in a variable. It will also make the code easier to understand in lot of case.

An example of XML code where this XPath code can be applied. $Post is a variable that contains all the 'post' element.

XML example
  • <list>
  • <post>
  • <tag>gtc-g-truc-source</tag>
  • <tag>gtc-tech-comment-opengl</tag>
  • </post>
  • <post>
  • <tag>gtc-opencl</tag>
  • </post>
  • <post>
  • <tag>gtc-tech-content</tag>
  • </post>
  • </list>

The result of this query is a set of 2 elements: 'post' elements from line 1 and 5 for every value of $Count greater than 1.

On our way to OpenGL 3.3 and OpenGL 4.0? >
< G-Truc Creation 6.3 source released
Copyright Christophe Riccio 2002-2016 all rights reserved
Designed for Chrome 9, Firefox 4, Opera 11 and Safari 5