xsl:fo Increment a variable inside page sequences?

501 views Asked by At

I have a several pages sequences in my xsl file. An xsl-template is called inside each page sequence. Inside each template I have a block that contains a variable that I need to be incremented if the block is executed....I tried to use a global variable but I found in many posts here we cannot increment a global variable in xsl-fo...Can SomeOne please guides me How to do that ?

My xsl-file is something like this:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0"
        xmlns:pdf="http://xmlgraphics.apache.org/fop/extensions/pdf">

        <xsl:output encoding="UTF-8" indent="yes" method="xml"
            standalone="no" omit-xml-declaration="no" />


        <xsl:template match="analyseData">

        <fo:page-sequence master-reference="simpleA6">
                        <fo:flow flow-name="xsl-region-body" border-collapse="collapse">
                            <xsl:call-template name="template1" />
                        </fo:flow>
        </fo:page-sequence>

        <fo:page-sequence master-reference="simpleA6">
                        <fo:flow flow-name="xsl-region-body" border-collapse="collapse">
                            <xsl:call-template name="template2" />
                        </fo:flow>
        </fo:page-sequence>
        <fo:page-sequence master-reference="simpleA6">
                        <fo:flow flow-name="xsl-region-body" border-collapse="collapse">
                            <xsl:call-template name="template3" />
                        </fo:flow>
        </fo:page-sequence>

        </xsl:template>
2

There are 2 answers

1
Kevin Brown On BEST ANSWER

Well, there is very limited information here but I could guess.

Nothing stops you from using XSL and an identity-transform to modify some interim result. So you could do what you are doing. Whenever you need to output this counter, why not write to the output <counter/>. Nothing more, just an empty tag that represents the counter.

Then write an identity-translate XSL that outputs the resulting file as is, except for a match on <counter>. This template would replace it with:

 <fo:inline><xsl:value-of select="count(preceding::counter) + 1"/></fo:inline>

Note: you could maybe also use <xsl:number> here.

So you would do:

XML+XSL -> XSL FO with counters + XSL identity change counters -> XSL FO -> format with your formatter

0
Tony Graham On

If you can come up with a pattern for all of the nodes in the source that need to be numbered and the numbering sequence is in the document order of the nodes in the source document, then you could use <xsl:number match="any" count="..." /> to do the counting. See https://www.w3.org/TR/xslt20/#numbering-based-on-position

If the count sequence doesn't match your source document or you can't find a pattern, then you're probably back to post-processing, as @kevin-brown suggests.