Technical note: XSLT to number elements consecutively across parent elements

What follows is a solution to an XSLT problem.

The problem: in an XML document, we want to number consecutively elements which share a grandparent element, but not parent. The document contains multiple grandparents. In each we want to restart the counter (imagine a book with chapters which have section; we want to number paragraphs of each chapter consecutively, but across sections: Chapter 1 / Section 1 / Para 1 , Para 2 / Section 2 / Para 3, Para 4 / Chapter 2 / Section 1 / Para 1, Para 2 / Section 2 / Para 3 , Para 4 /).

We want to accomplish this with an XSLT stylesheet.

Short answer: add counts of all preceding paras in sections and preceding paras in current section; subtract the count of all preceding paras in other chapters, and add 1. Like this:

count(preceding::section/para) +
count(preceding-sibling::para) -
count(preceding::chapter/section/para) + 1

For a long answer, read more here: z:xslt-number.



Cite this blog post
Neven Jovanović (2015, May 22). Technical note: XSLT to number elements consecutively across parent elements. Croatica et Tyrolensia. Retrieved March 29, 2024, from https://doi.org/10.58079/ne7n

Search OpenEdition Search

You will be redirected to OpenEdition Search