Akash
Akash

Reputation: 45

produce consecutive numbers based on node value in xslt1.0

In the below XML for first line ref=1 , sum of name elements equals to 5(2+3) and for second line ref=2, it is 3. I need to produce 1,2,3,4,5 for first line and 1,2,3 for second line depending on the total of each name under each line.

Expected o/p

line 1
number 1
number 2
number 3
number 4
number 5

line 2
number 1
number 2
number 3

In my code i have to generate separate page based on the value in name element so i have used node . In total we need to have 8 pages and numbers should be consecutive (sum of name) for each line.

<?xml version="1.0" encoding="UTF-8"?>
<root parent="a">
   <line ref="1">
    <child line="1">
        
        <name id="school">2</name>
       
    </child>
    <child line="2">
        <name id="school">3</name>
    </child>
  </line>
    
    <line ref="2">
        <child line="1">
            <name id="school">1</name>
        </child>
        <child line="2">
            <name id="school">2</name>
        </child>
    </line>
 </root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    
    <xsl:decimal-format name="generalFormat" grouping-separator="," decimal-separator="." />
    
    <xsl:template match="/" name="Barcode">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="ManufacturLabelSize-first" page-height="100mm" page-width="100mm"  >
                    <fo:region-body margin-top="15mm" />
                    <fo:region-before />
                    <fo:region-after />
                </fo:simple-page-master>
                <fo:simple-page-master master-name="ManufacturLabelSize-rest" page-height="100mm" page-width="100mm"   >
                    <fo:region-body margin-top="15mm"/>
                    <fo:region-before />
                    <fo:region-after />
                </fo:simple-page-master>
                <fo:page-sequence-master master-name="ManufacturLabelSize-portrait">
                    <fo:repeatable-page-master-alternatives>
                        <fo:conditional-page-master-reference master-reference="ManufacturLabelSize-first"
                            page-position="first"/>
                        <fo:conditional-page-master-reference master-reference="ManufacturLabelSize-rest"
                            page-position="rest"/>
                    </fo:repeatable-page-master-alternatives>
                </fo:page-sequence-master>
            </fo:layout-master-set>
            
            <fo:page-sequence master-reference="ManufacturLabelSize-portrait" id="pSeqID">
                <fo:flow flow-name="xsl-region-body">
                    <fo:table>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell><fo:block>name:</fo:block></fo:table-cell>
                                
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block></fo:block>
                                    <fo:block>
                                        <xsl:call-template name="line"></xsl:call-template>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    
    <xsl:template name="line">
        <fo:table>
            <fo:table-body>
                <xsl:apply-templates select="root/line"/>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    
    <xsl:template match="line" name="match">
        <xsl:for-each select="child">
            
            <xsl:variable name="pack">
              <xsl:value-of select="name[@id=&quot;school&quot;]"/>
            </xsl:variable>
         
            <xsl:for-each select="(//node())[position() &lt;= $pack]">
            <fo:table-row>
                <fo:table-cell>
                    <fo:block>
                       number
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell start-indent="25mm">
                    
                    <fo:block>
                        <xsl:value-of select="position()"/>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
                
                
        </xsl:for-each>
            <fo:table-row>
                
                <fo:table-cell>
                    <fo:block>
                        
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block page-break-after="always"/>
                    
                </fo:table-cell>
            </fo:table-row>
        </xsl:for-each>
    </xsl:template>
    
    
</xsl:stylesheet>

Any help is much appreciated!

Upvotes: 1

Views: 44

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

If you are limited to XSLT 1.0 you will need to use a recursive named template to generate the pages with consecutive numbers. Something along the lines of:

<xsl:template match="line">
    <something>
        <xsl:call-template name="generate-pages">
            <xsl:with-param name="n" select="sum(child/name)"/>
        </xsl:call-template>
    </something>
</xsl:template>

<xsl:template name="generate-pages">
    <xsl:param name="n"/>
    <xsl:if test="$n > 0">
        <!-- recursive call -->
        <xsl:call-template name="generate-pages">
            <xsl:with-param name="n" select="$n - 1"/>
        </xsl:call-template>
        <!-- output -->
        <page>
            <xsl:value-of select="$n"/>
        </page>
    </xsl:if>   
</xsl:template>

Upvotes: 0

Related Questions