Rikon
Rikon

Reputation: 2706

XPath Count Transformed Lines

I have a need to append filler records to a file that's being generated w/ XSLT. The need to is to round the total number of lines in the file up to the nearest 10 such that if the following exists:

FileHeader
SectionHeader
Detail
Detail
Detail
SectionFooter
FileFooter

The total number of lines would be 7 and I would need to add 3 filler records to make:

FileHeader
SectionHeader
Detail
Detail
Detail
SectionFooter
FileFooter
[Filler Record]
[Filler Record]
[Filler Record]

Should I just increment a variable in the xslt every time I write a line and use it to do the mod math at the end, or is there a way for the xslt/xpath to know how many lines it's currently written, such that there is some more reliable function call I can make that will give me this count?

Upvotes: 0

Views: 299

Answers (1)

LarsH
LarsH

Reputation: 28004

There isn't a way to increment a variable in XSLT. Nor can XSLT tell you how many lines it's written so far (because XSLT is designed to prefer order-of-execution-independence).

Instead, you can either

  1. Do a separate computation (e.g. count) of how many records the stylesheet should produce through other templates based on the input document. This is the easier method, if the number of output records is equal to the number of input records, or at least the one is easily predicted based on the other. Or,

  2. Capture the result of your initial transformation into a variable (using the nodeset extension or using XSLT 2.0), and then run a count() on it to determine the number of records in it. Then do a xsl:copy-of to output the captured result, followed by any padding required.

Upvotes: 2

Related Questions