繁体   English   中英

用正确的格式创建xsl-fo

[英]creating xsl-fo with correct format

我需要从xml文档创建一个xsl-fo文档,该文档显示每年以及每年在每个地区的每个地区的出口金额(利润)的信息。

为了产生一个表格,该表格在显示此信息的三列表格中包含每一年的年度收入以及与上一年的比较方式。

这个问题似乎困扰了全班,并且目前没有老师可以咨询。 任何帮助将不胜感激。 提前致谢。

我准备了以下XSLT,希望对您有所帮助。

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:template match="/">
    <html>
    <body>
    <table style="width:100%" border="1" >
        <tr bgcolor="#9acd32">
            <th>Year</th>
            <th>Total</th>
            <th>Delta</th>
        </tr>
    <xsl:for-each select="exports/year">
        <tr>
          <td><xsl:value-of select="./text()"/></td>
          <td><xsl:value-of select='format-number(./total, "#.0")'/></td>
          <xsl:choose>
          <xsl:when test="position()=1">
            <td><xsl:text>-</xsl:text></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select='format-number((xs:decimal(./total)- xs:decimal(preceding-sibling::*[ 1]/total)) div xs:decimal(preceding-sibling::*[ 1]/total),"#.00%")'/></td>
          </xsl:otherwise>
          </xsl:choose>
        </tr>
    </xsl:for-each>     
    </table>
    </body>
    </html>

  </xsl:template>
</xsl:stylesheet>

它的主要部分是:

  • for-each select="exports/year"将在您每年的输入数据上循环并在输出表中生成一行
  • ./text()获取年份
  • 'format-number(./total, "#.0")'获得总计的小数点后一位精度
  • 然后是最后一部分, choose when/otherwise检查您的元素是否是不使用position()=1的第一个元素position()=1如果是大小写输出“-”),否则请执行以下计算:

    'format-number((xs:decimal(./total)- xs:decimal(preceding-sibling::*[ 1]/total)) div xs:decimal(preceding-sibling::*[ 1]/total),"#.00%")' ,其中div是除数运算符,并且previous preceding-sibling允许您在将结果格式化为带有两位小数的百分比之前,获得DOM树中的前一个兄弟节点。

使用round()函数生成XSL-FO的另一个示例。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
            xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions" xml:lang="en-US"
            id="id_document">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="spm" page-width="6in" page-height="5in">
                    <fo:region-body margin-top="0.5in" margin-bottom="0.5in" margin-left="0.5in"
                        margin-right="0.5in" overflow="error-if-overflow"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="spm" reference-orientation="from-page-master-region()"
                writing-mode="from-page-master-region()">
                <fo:flow flow-name="xsl-region-body" font-family="Arial" font-size="11pt">
                    <xsl:apply-templates/>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template match="exports">
        <fo:table>
            <fo:table-column column-number="1" column-width="5em"/>
            <fo:table-column column-number="2" column-width="7em"/>
            <fo:table-column column-number="3" column-width="5em"/>
            <fo:table-body>
                <xsl:apply-templates/>
            </fo:table-body>
        </fo:table>
    </xsl:template>

    <xsl:template match="year">
        <xsl:variable name="currTotal" as="xs:decimal" select="xs:decimal(total)"/>
        <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    <xsl:value-of select="./text()"/>
                </fo:block>
            </fo:table-cell>
            <fo:table-cell>
                <fo:block>
                    <xsl:value-of select="concat('$',round($currTotal * 10) div 10)"/>
                </fo:block>
            </fo:table-cell>
            <fo:table-cell>
                <fo:block>
                    <xsl:choose>
                        <xsl:when test="empty(preceding-sibling::*)">
                            <xsl:text>-</xsl:text>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:variable name="prevTotal" as="xs:decimal" select="xs:decimal(preceding-sibling::*[1]/total)"/>
                            <xsl:variable name="totalDiff" as="xs:decimal" select="$currTotal - $prevTotal"/>
                            <xsl:variable name="percentage" as="xs:decimal" select="round($totalDiff div $prevTotal * 10000) div 100"/>
                            <xsl:value-of select="concat($percentage,'%')"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:template>

</xsl:stylesheet>

主要特点是

  • match="export"模板生成fo:table。
  • match="year"模板生成每个fo:table-row。
  • 使用round()函数获取值的每个精度。 (format-number()会更容易)
  • 使用empty(preceding-sibling::*)来判断第一year中元素exports

样本结果:

样品结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM