首页 > 解决方案 > XSLT 转换输出仅显示第一页并且图形被截断

问题描述

所以我将 XSLT 应用到 XML 代码并让它显示我需要的输出,

显示了完整的 XML 代码,但是当我将其打印为 html/pdf 时,仅显示第一页并且底部被切断。

有谁知道为什么不显示?

供参考,这是我的 XSLT 代码

<xsl:stylesheet xmlns="http://www.w3.org/2000/svg" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0">

    <xsl:output indent="yes"/>

    <xsl:variable name="svg-width" select="1200"/>
    <xsl:variable name="svg-height" select="900"/>
    <xsl:variable name="max-bar-length" select="$svg-width - 400"/>

    <xsl:variable name="bar-height" select="20"/>
    <xsl:variable name="bar-spacing" select="50"/>
    <xsl:variable name="bar-start" select="200"/>

    <xsl:variable name="bar-width1" select="gdp_agri"/>
    <xsl:variable name="bar-width2" select="gdp_ind"/>
    <xsl:variable name="bar-width3" select="gdp_serv"/>
    <xsl:variable name="gdp_agri" select="gdp_agri"/>
    <xsl:variable name="gdp_ind" select="gdp_ind"/>
    <xsl:variable name="gdp_serv" select="gdp_serv"/>

    <xsl:template match="/">
        <html>
            <body>
                <svg viewBox="0 0 {$svg-width} {$svg-height}" width="{$svg-width}px" height="{$svg-height}px">
                    <g id="bar-chart" font-size="16" transform="translate(20,100)">
                        <xsl:apply-templates select="child::mondial/child::country[child::encompassed[attribute::continent='europe']]">
                         <xsl:sort order="ascending" select="name"/>
                        </xsl:apply-templates>
                    </g>
                </svg>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="country">
        <xsl:variable name="bar-width" select="gdp_agri"/>
        <g id="bar_{position()}" transform="translate(0, {(position() - 1) * ($bar-height + $bar-spacing)})">
            <text x="0" y="{($bar-height + $bar-spacing) div 2}">
                <xsl:number format="1. " value="position()"/>
                <xsl:value-of select="name"/>
            </text>
            <rect x="{$bar-start}" y="0" width="{$bar-width}" height="{$bar-height}" fill="green"/>
            <text x="{$bar-width +$bar-start + 5}" y="{0.2*($bar-height + $bar-spacing) div 2}">Agri GDP: <xsl:value-of select="gdp_agri"/>%</text>
        </g>
        
         <xsl:variable name="bar-width2" select="gdp_ind"/>
        <g id="bar_{position()}" transform="translate(0, {(position() - 1) * ($bar-height + $bar-spacing)})">
            <text x="0" y="{($bar-height + $bar-spacing) div 2}">
                <xsl:number format="1. " value="position()"/>
                <xsl:value-of select="name"/>
            </text>
            <rect x="{$bar-start}" y="20" width="{$bar-width2}" height="{$bar-height}" fill="brown"/>
            <text x="{$bar-width2 +$bar-start + 5}" y="{($bar-height + $bar-spacing) div 2}">Ind GDP: <xsl:value-of select="gdp_ind"/>%</text>
        </g>
        
        <xsl:variable name="bar-width3" select="gdp_serv"/>
        <g id="bar_{position()}" transform="translate(0, {(position() - 1) * ($bar-height + $bar-spacing)})">
            <text x="0" y="{($bar-height + $bar-spacing) div 2}">
                <xsl:number format="1. " value="position()"/>
                <xsl:value-of select="name"/>
            </text>
            <rect x="{$bar-start}" y="40" width="{$bar-width3}" height="{$bar-height}" fill="yellow"/>
            <text x="{$bar-width3 +$bar-start + 5}" y="{($bar-height + $bar-spacing) div 1.2}">Serv. GDP: <xsl:value-of select="gdp_serv"/>%</text>
        </g>
    </xsl:template>

</xsl:stylesheet>

当我稍后应用转换时,它会运行,但是只显示输出的第一页。我正在运行的其他国家/地区没有出现(这是 mondial 数据库);它们出现在 XML 输出中,但当我打印为 html / pdf 甚至在存在数据库输出中时不会出现 可以看出下面有更多的国家,但是 html 输出没有显示它,尽管完整的 XML 输出在 ExistDB 上显示

标签: htmlxmlxslt

解决方案


您正在构建一个大图像,尝试将其拆分,以便可以应用分页符。我只能猜测这将如何工作,因为您没有共享 xml 数据。应该是这样的:

<xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates select="child::mondial/child::country[child::encompassed[attribute::continent='europe']]">
                <xsl:sort order="ascending" select="name"/>
            </xsl:apply-templates>
        </body>
    </html>
</xsl:template>

<xsl:template match="country">
    <svg viewBox="0 0 {$svg-width} {$svg-height}" width="{$svg-width}px" height="{$svg-height}px">
        <g id="bar-chart" font-size="16" transform="translate(20,100)">
            <!-- place adapted code of existing template match="country" here -->
        </g>
    </svg>
</xsl:template>

这样您就可以为每个国家/地区生成不同的 svg-image。你可能不得不摆弄 $svg-width、$svg-height 和坐标。另一方面,管理坐标变得更容易,因为每个国家的图表总是从图像的顶部开始。这不是开箱即用的,但它为您提供了如何解决问题的想法。


推荐阅读