1

我有以下 XML:

<sample>
    <value1>This is one</value1>
    <value2>Number two</value2>
    <value4>Last value</value4>
</sample>

使用 Apache FOP/XSL-FO 我想要一个类似于此的 PDF:

Value 1: This is one Value 2: Number two
Value 3:                 Value 4: Last value

注意“Value 3:”和“Value 4:”之间的间距/填充。

以下转换给了我想要的结果。但它似乎过于复杂(并且对于具有许多值的真实 PDF 可能表现不佳)。

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

    <xsl:template match="sample">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="page-layout">
                    <fo:region-body margin="10mm" region-name="body"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="page-layout">
                <fo:flow flow-name="body">
                    <fo:block>
                        <xsl:variable name="pad">                            
                            <xsl:choose>
                                <xsl:when test="value1">5</xsl:when>
                                <xsl:otherwise>25</xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>
                        <fo:inline padding-right="{$pad}mm">Value 1: <xsl:value-of select="value1"/></fo:inline>
                        Value 2: <xsl:value-of select="value2"/>
                    </fo:block>
                    <fo:block>
                        <xsl:variable name="pad">
                            <xsl:choose>
                                <xsl:when test="value3">5</xsl:when>
                                <xsl:otherwise>25</xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>
                        <fo:inline padding-right="{$pad}mm">Value 3: <xsl:value-of select="value3"/></fo:inline>
                        Value 4: <xsl:value-of select="value4"/>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

</xsl:stylesheet>

有没有更简单/更好的方法来实现这个?

更新:

我将以上内容重构为模板“字段”:

<xsl:template name="field">
    <xsl:param name="txt"/>
    <xsl:param name="val"/>
    <xsl:param name="pad"/>
    <xsl:variable name="p">
        <xsl:choose>
            <xsl:when test="$val">5</xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="$pad"><xsl:value-of select="$pad"/></xsl:when>
                    <xsl:otherwise>25</xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <fo:inline padding-right="{$p}mm"><xsl:value-of select="$txt"/>:</fo:inline>
    <fo:inline keep-with-previous="always" padding-right="5mm" font-weight="bold"><xsl:value-of select="$val"/></fo:inline>
</xsl:template>

可以这样使用:

<xsl:call-template name="field">
    <xsl:with-param name="txt">Value 1</xsl:with-param>
    <xsl:with-param name="val"><xsl:value-of select="sample/value1"/></xsl:with-param>
</xsl:call-template>

模板采用第三个可选参数 pad。如果指定,其值将用作填充。

David 的模板(请参阅接受的答案)使用更简单的 if 结构,其中 padding-right 属性在需要时被覆盖。

4

1 回答 1

2

由于这可能没有在浏览器中执行,因此有任何理由不使用 xslt2 (这会使它变得更好),因为无论如何您都在使用 java,所以免费的 saxon 9 实现在这里可以很好地工作。

但这里有一个稍微重构的 XSLT 1 版本。

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

    <xsl:template match="sample">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="page-layout">
                    <fo:region-body margin="10mm" region-name="body"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="page-layout">
                <fo:flow flow-name="body">
         <xsl:call-template name="twoval">
          <xsl:with-param name="a" select="value1"/>
          <xsl:with-param name="ahead" select="'Value 1'"/>
          <xsl:with-param name="b" select="value2"/>
          <xsl:with-param name="bhead" select="'Value 2'"/>
         </xsl:call-template>
         <xsl:call-template name="twoval">
          <xsl:with-param name="a" select="value3"/>
          <xsl:with-param name="ahead" select="'Value 3'"/>
          <xsl:with-param name="b" select="value4"/>
          <xsl:with-param name="bhead" select="'Value 4'"/>
         </xsl:call-template>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>


<xsl:template name="twoval">
<xsl:param name="a"/>
<xsl:param name="ahead"/>
<xsl:param name="b"/>
<xsl:param name="bhead"/>
<fo:block>
 <fo:inline padding-right="25mm">
  <xsl:if test="$a"><xsl:attribute name="padding-right">5mm</xsl:attribute></xsl:if>
  <xsl:value-of select="$ahead"/><xsl:text>: </xsl:text>
  <xsl:value-of select="$a"/>
 </fo:inline>
 <xsl:text> </xsl:text>
 <xsl:value-of select="$bhead"/><xsl:text>: </xsl:text>
 <xsl:value-of select="$b"/>
</fo:block>
</xsl:template>

</xsl:stylesheet>
于 2012-09-15T11:37:47.437 回答