XML包含多个PrintSection,并且它们有多个文本和条形码部分。我需要在第一个 PrintSection(或至少每个 PrintSection)之后输出一个空行,而不更改它们在 XML 中出现的顺序。
.XML
<Document>
<PrintSection>
<Text/>
<Text/>
</PrintSection>
<PrintSection>
<Text/>
<Barcode/>
</PrintSection>
<PrintSection>
<Text/>
<Text/>
</PrintSection>
</Document>
XSL
<xsl:template match="/">
.....
</xsl:template>
<xsl:template match="Text">
<fo:block>
<xsl:apply-templates select="Text1" />
</fo:block>
</xsl:template>
<xsl:template match="Barcode">
<fo:block>
<xsl:apply-templates select="Barcode1" />
</fo:block>
</xsl:template>
我尝试添加这个
<xsl:template match="PrintSection">
<fo:block>
<xsl:apply-templates select="Text" />
<xsl:apply-templates select="Barcode" />
<fo:leader/>
</fo:block>
</xsl:template>
这将插入一个空行,但它在最后打印条形码,改变了自然顺序。
在除第一个PrintSection
之外的所有上添加一个space-before
属性 (https://www.w3.org/TR/xsl11/#space-before):
<fo:template match="Document">
<xsl:apply-templates select="PrintSection" />
</fo:template>
<fo:template match="PrintSection">
<fo:block>
<xsl:if test="position() > 1">
<xsl:attribute name="space-before">1.4em</xsl:attribute>
</xsl:attribute>
<xsl:apply-templates />
</fo:block>
</xsl:template>
Document
的模板只是为了显示应一次性选择所有PrintSection
,以便test
中的position()
按预期工作。 如果不是示例中PrintSection
之间的空白文本节点,无论如何都会发生这种情况,并且为Document
显示的模板将是多余的。
如何改变
<xsl:template match="PrintSection">
<fo:block>
<xsl:apply-templates select="Text" />
<xsl:apply-templates select="Barcode" />
<fo:leader/>
</fo:block>
</xsl:template>
自
<xsl:template match="PrintSection">
<fo:block>
<xsl:apply-templates/>
<fo:leader/>
</fo:block>
</xsl:template>