在下面的 XML 中,第一行 ref=1 的名称元素之和等于 5(2+3),第二行 ref=2 的名称元素之和为 3。 我需要为第一行生成 1,2,3,4,5,为第二行生成 1,2,3,具体取决于每行下每个名称的总数。
预期o/p
line 1
number 1
number 2
number 3
number 4
number 5
line 2
number 1
number 2
number 3
在我的代码中,我必须根据 name 元素中的值生成单独的页面,因此我使用了 node 。我们总共需要 8 页,每行的数字应该是连续的(名称之和)。
<?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="school"]"/>
</xsl:variable>
<xsl:for-each select="(//node())[position() <= $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>
非常感谢任何帮助!
如果您仅限于 XSLT 1.0,您将需要使用递归命名模板来生成具有连续编号的页面。大致如下:
<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>
<page>
<xsl:value-of select="$n"/>
</page>
</xsl:if>
</xsl:template>