XSL-FO布局条件信息

时间:2016-11-23 10:16:32

标签: xml xslt xsl-fo

我正在使用XSL-FO和Antenna House Formatter v6.3一起制作飞行手册。

很多信息属于"如果条件X执行此操作,则执行其他操作" 。在目前的情况下,我有以下XML:

<crewDrill>
    <case>
        <caseCond>Hot inside:</caseCond>
        <if>
            <caseCond>Yes</caseCond>
            <crewDrillStep>
                <para>Adjust thermostat</para>
            </crewDrillStep>
            <crewDrillStep>
                <para>Open the window</para>
            </crewDrillStep>
        </if>
    </case>
    <crewDrillStep>
        <para>Enjoy life</para>
    </crewDrillStep>
</crewDrill>

此XML的所需输出为:

desired output for conditional statements

编辑:所以我关心的是,使用XSLT将XML转换为XSL-FO(包括Antenna House Formatter中的功能)所需的输出是否可行,如果是这样,那么解决问题的正确方法是什么?鉴于我对XSL-FO的了解,我能做到这一点的唯一方法就是使用表格。也许有更好的方法。

编辑2:正如@Tomalak指出它真的有两个问题 - XSL-FO结构的外观以及XSLT转换的外观。我主要关心的是表示所需输出的XSL-FO结构如何。鉴于目标结构,我可能能够找出转换。对于不清楚的初步问题表示抱歉,感谢@Tomalak澄清我的担忧。

使用的XML架构源于S1000D 4.1 Crew架构

1 个答案:

答案 0 :(得分:1)

这里有一些思考的XSL代码。它并不完美,但调整表中的一些边框和列可以让你到达那里。

注意:我添加了一个单独的文档元素,以便我可以测试各种情况。

这个XSL:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        version="1.0">
        <xsl:template match="/">
            <xsl:apply-templates/>
        </xsl:template>
        <xsl:template match="document">
            <fo:root font-family="Arial">
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="page"
                        page-height="11in" page-width="8in"
                        margin-top=".5in" margin-left="1in" margin-right="1in" margin-bottom=".5in">
                        <fo:region-body/>
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="page">
                    <fo:flow flow-name="xsl-region-body" >
                        <xsl:apply-templates/>
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
        <xsl:template match="crewdrill">
            <!-- whole diagram, put into a block -->
            <fo:block>
                <xsl:apply-templates/>
            </fo:block>
        </xsl:template>
        <xsl:template match="case">
            <!-- map to a table 2-row table, one for heading and one for the step(s) -->
            <fo:table>
                <fo:table-column column-width="12pt"/>
                <fo:table-column column-width="12pt"/>
                <fo:table-column column-width="12pt"/>
                <fo:table-column/>
                <fo:table-column column-width="prrportional-column-width(100)"/>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell number-columns-spanned="6">
                            <fo:block font-weight="bold">
                                <xsl:value-of select="caseCond"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <xsl:apply-templates/>
                </fo:table-body>
            </fo:table>
        </xsl:template>
        <xsl:template match="if">
            <fo:table-row>
                <fo:table-cell border-right="0.5pt solid black">
                    <fo:block text-align="right" margin-right="-1.5pt">◄</fo:block>
                </fo:table-cell>
                <fo:table-cell padding-left="-1.5pt">
                    <fo:block>►</fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block>─</fo:block>
                </fo:table-cell>
                <fo:table-cell border="0.5pt solid black" text-align="center">
                    <fo:block font-weight="bold">
                        <xsl:value-of select="caseCond"/>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block><fo:leader/></fo:block>
                </fo:table-cell>
            </fo:table-row>
            <fo:table-row>
                <fo:table-cell border-right="0.5pt solid black">
                    <fo:block><fo:leader/></fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block><fo:leader/></fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block><fo:leader/></fo:block>
                </fo:table-cell>
                <fo:table-cell number-columns-spanned="2">
                    <xsl:apply-templates/>
                </fo:table-cell>
            </fo:table-row>
            <fo:table-row>
                <fo:table-cell border-right="0.5pt solid black">
                    <fo:block><fo:leader/></fo:block>
                </fo:table-cell>
                <fo:table-cell font-weight="bold" text-align="center" number-columns-spanned="4">
                    <fo:block>- END -</fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
        <xsl:template match="crewDrillStep[not(parent::if)]">
            <xsl:variable name="num">
                <xsl:value-of select="count(preceding-sibling::crewDrillStep) + 1"/>
            </xsl:variable>
            <fo:table>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell border="0.5pt solid black" text-align="center">
                            <fo:block font-weight="bold">No</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block><fo:leader></fo:leader></fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell number-columns-spanned="2">
                            <xsl:apply-templates>
                                <xsl:with-param name="num" select="$num"/>
                            </xsl:apply-templates>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell font-weight="bold" text-align="center" number-columns-spanned="5">
                            <fo:block>------ END ------</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </xsl:template>
        <xsl:template match="crewDrillStep">
            <xsl:variable name="num">
                <xsl:value-of select="count(preceding-sibling::crewDrillStep) + 1"/>
            </xsl:variable>
            <xsl:apply-templates>
                <xsl:with-param name="num" select="$num"/>
            </xsl:apply-templates>
        </xsl:template>
        <xsl:template match="para">
            <xsl:param name="num"/>
            <fo:block>
                <xsl:text>(</xsl:text><xsl:value-of select="$num"/><xsl:text>) </xsl:text><xsl:value-of select="."/>
            </fo:block>
        </xsl:template>
        <xsl:template match="caseCond"/>
    </xsl:stylesheet>

产生此输出:

enter image description here

相关问题