以正确的格式创建xsl-fo

时间:2017-11-13 21:57:48

标签: xml xslt xsl-fo

我需要从xml文档创建一个xsl-fo文档,该文档显示每年以及每个区域内每个区域的导出量(利润)的信息。

为了生成一个表格,其中包含每年的收入以及与上一年度的比较,在显示此类信息的三栏表中。

这个问题似乎让整个班级难以理解,老师目前无法进行咨询。任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

我准备了以下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的情况,如果它是案例输出&#34; - &#34;否则做以下计算:

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

答案 1 :(得分:0)

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

Type t = someGenericObject.GetType();
var p = t.GetProperty("idNumber");
string theStringYouWant = (string)p.GetValue(someGenericObject);

主要功能是

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

样本结果:

Sample Result

相关问题