Question

I need to do this table:

       nome  idade 
1006    …    …
1007    …    …
1008    …    …
1009    …    …
1010    …    …
1011    …    …
1012    …    …
1013    …    …

And the problem is to get that counting(1006,1007..) because in the xml i get something like this:

<class>
       <refclass>10</refclass>
       <begin>6</begin>
       <part>
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados>
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados> 
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados> 
       </part>
       <part>
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados>
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados> 
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados> 
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados> 
       </part>
        <part>
            <dados>
                  <nome>...</nome>
                  <idade>---</idade>
            </dados>
       </part>
<class>

So:

  <fo:table-cell border-collapse="collapse" border-color="gray" 
font-family="Helvetica" font-size="8pt" border="solid 1pt gray" 
padding="1pt" display-align="before">
        <fo:block text-align="center">

Inside here i need the code to take refclass number and concat with the count that starts at in this case 6 (<begin>)... counting inside the first tag <part> is easy because i can just sum to the position() of each <dados>, but i dont know how to save the value from the first part and keep counting in next <part> tag... So the idea is how i can keep counting the <dados> tag.

        </fo:block>
<fo:table-cell>

Please i realy need a solution, the rest is not a problem... but i cant change variables, and the number of dados tag are variable... I cant change XML output. I think in a solution to write the value in xml docment and take it to keep counting, i dont know if it is possible.

Was it helpful?

Solution

Instead of using position(), try using xsl:number.

Here's an example to get you started...

XML Input

<class>
    <refclass>10</refclass>
    <begin>6</begin>
    <part>
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados>
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados> 
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados> 
    </part>
    <part>
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados>
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados> 
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados> 
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados> 
    </part>
    <part>
        <dados>
            <nome>...</nome>
            <idade>---</idade>
        </dados>
    </part>
</class>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                    <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="my-page">
                <fo:flow flow-name="xsl-region-body"> 
                    <fo:table>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block/>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block>nome</fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block>idade</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <xsl:apply-templates select="part/dados"/>
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template match="dados">
        <xsl:variable name="level">
            <xsl:number level="any"/>
        </xsl:variable>
        <fo:table-row>
            <fo:table-cell>
                <fo:block><xsl:value-of select="concat(/*/refclass,format-number(($level+/*/begin)-1,'00'))"/></fo:block>
            </fo:table-cell>
            <fo:table-cell>
                <fo:block><xsl:value-of select="nome"/></fo:block>
            </fo:table-cell>
            <fo:table-cell>
                <fo:block><xsl:value-of select="idade"/></fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:template>

</xsl:stylesheet>

XSL-FO Output

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:table>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block/>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>nome</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>idade</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1006</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1007</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1008</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1009</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1010</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1011</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1012</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block>1013</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>...</fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                     <fo:block>---</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

PDF Representation

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top