簡體   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