【问题标题】:Step numbering in XSL-FOXSL-FO 中的步骤编号
【发布时间】:2015-09-28 13:06:03
【问题描述】:

为此苦苦挣扎了一会儿,现在要寻求帮助。在使用 XSL-FO 时在 PDF 输出中引用它时,我正在尝试获取正确的步骤编号。有了以前在这里得到的帮助,我有其他参考资料可以按需要工作。这些特殊的标签给我带来了麻烦。

这是用于 S1000D 航空出版规范中的故障隔离树:

<isolationStep id="step4"><isolationStepQuestion>Check for burnt-out lamps. Are lamps good?</isolationStepQuestion><isolationStepAnswer><yesNoAnswer><yesAnswer nextActionRefId="step6"/><noAnswer nextActionRefId="step8"/></yesNoAnswer></isolationStepAnswer></isolationStep>

如您所见,该问题有一个答案部分。这些答案对其他步骤有参考。在 PDF 中,我得到了正确编号的步骤,但是在呈现答案时,我得到了这个:

用于问题、答案和尝试编号以回答引用步骤的 XSL-FO:

<xsl:template match="isolationStep | isolationProcedureEnd">
    <xsl:for-each select="note">
        <fo:block font-weight="bold">Note</fo:block>
        <xsl:apply-templates/>
    </xsl:for-each>
    <xsl:for-each select="caution">
        <fo:block font-weight="bold" text-align="center">CAUTION</fo:block>
        <xsl:apply-templates/>
    </xsl:for-each>
    <xsl:for-each select="warning">
        <fo:block font-weight="bold" text-align="center">WARNING</fo:block>
        <xsl:apply-templates/>
    </xsl:for-each>
    <fo:block id="{@id}" padding="5pt" font-size="10pt">
        <!-- border-style="solid"
              border-width=".1mm"-->
        <!--<xsl:apply-templates/>-->
        <fo:list-block space-after="2mm">
            <fo:list-item>
                <fo:list-item-label end-indent="label-end()">
                    <fo:block>
                        <xsl:number level="multiple" count="isolationMainProcedure/isolationStep | isolationProcedureEnd" format="1."/>
                    </fo:block>
                </fo:list-item-label>
                <fo:list-item-body start-indent="body-start()">
                    <fo:block>
                        <!--<xsl:apply-templates select="title"/>-->
                        <xsl:choose>
                            <xsl:when test="count(action) &gt; 1">
                                <xsl:for-each select="action">
                                    <fo:list-block space-after="2mm">
                                        <fo:list-item>
                                            <fo:list-item-label end-indent="label-end()">
                                            <fo:block>
                                            <xsl:number format="a."/>
                                            </fo:block>
                                            </fo:list-item-label>
                                            <fo:list-item-body start-indent="body-start()">
                                            <fo:block>
                                            <xsl:apply-templates/>
                                            </fo:block>
                                            </fo:list-item-body>
                                        </fo:list-item>
                                    </fo:list-block>
                                    <!--<xsl:apply-templates/>-->
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:for-each select="action">
                                    <fo:block>
                                        <xsl:apply-templates/>
                                    </fo:block>
                                    <!--<xsl:apply-templates/>-->
                                </xsl:for-each>
                            </xsl:otherwise>
                        </xsl:choose>
                        
                        <xsl:apply-templates select="isolationStepQuestion"/>
                        <xsl:apply-templates select="isolationStepAnswer"/>
                    </fo:block>
                </fo:list-item-body>
            </fo:list-item>
        </fo:list-block>
    </fo:block>
</xsl:template>


<xsl:template match="isolationStep | isolationProcedureEnd" mode="nbr">
    <xsl:variable name="origNbr">
        <xsl:number level="multiple" format="1"/>            
    </xsl:variable>
    <xsl:value-of select="$origNbr"/>
</xsl:template>

<xsl:template match="isolationStepQuestion">
    <fo:block font-weight="bold">
        <xsl:value-of select="."/>
    </fo:block>
</xsl:template>

<xsl:template match="yesNoAnswer">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="yesAnswer">
    <xsl:variable name="ref" select="@nextActionRefId"/>
    <fo:block space-before="4pt" space-after="4pt" keep-with-previous="always">
        <fo:inline><xsl:text>Yes: Go to </xsl:text>
            <fo:basic-link internal-destination="{@nextActionRefId}" color="blue">Step  <!--<xsl:value-of select="$yesref2"/>-->
                <!--ancestor::isolationMainProcedure-->
                <!--<xsl:apply-templates select="an../../../.././isolationStep | isolationProcedureEnd[@id=$yesref]" mode="nbr"/> ancestor::isolationMainProcedure[1]/-->
                <xsl:apply-templates select="//isolationStep | isolationProcedureEnd[@id=$ref]" mode="nbr"/>
            </fo:basic-link></fo:inline>
    </fo:block>
</xsl:template>

<xsl:template match="noAnswer">
    <xsl:variable name="ref" select="@nextActionRefId"/>
    <fo:block keep-with-previous="always">
        <fo:inline>
            <xsl:text>No: Go to </xsl:text>
            <fo:basic-link internal-destination="{@nextActionRefId}" color="blue">Step 
                <xsl:apply-templates select="//isolationStep | isolationProcedureEnd[@id=$ref]" mode="nbr"/>
            </fo:basic-link></fo:inline>
    </fo:block>
</xsl:template>

<xsl:template match="isolationStepAnswer">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="isolationStep | isolationProcedureEnd" mode="nbr">
    <xsl:variable name="origNbr">
        <xsl:number level="multiple" format="1"/>            
    </xsl:variable>
    <xsl:value-of select="$origNbr"/>
</xsl:template>

此外,这些链接可以正常工作,并将您带到正确的位置。我只是在正确编号时遇到问题。

这是一个 XSL 2.0 样式表。任何帮助表示赞赏,在此先感谢。

【问题讨论】:

    标签: xml xslt xsl-fo


    【解决方案1】:

    //isolationStep 正在选择文档中的每个 isolationStep,因此您将获得每个 isolationStep 的数量。

    您可能一直在尝试获得“//isolationStep[@id=$ref] | //isolationProcedureEnd[@id=$ref]”的效果,但您可能最好使用密钥通过它们的 ID 来查找 isolationStepisolationProcedureEnd


    密钥定义在http://www.w3.org/TR/xslt20/#key

    您可以通过 @id 将查找 isolationStep 元素的键定义为样式表中的顶级元素:

    <xsl:key name="isolationSteps"
             match="isolationStep"
             use="@id" />
    

    并在您的模板中,使用key() 函数查找具有匹配@id 值的isolationStep

    <xsl:template match="yesAnswer">
        <xsl:variable name="ref" select="@nextActionRefId"/>
        <fo:block space-before="4pt" space-after="4pt" keep-with-previous="always">
            <fo:inline><xsl:text>Yes: Go to </xsl:text>
                <fo:basic-link internal-destination="{@nextActionRefId}" color="blue">Step  <!--<xsl:value-of select="$yesref2"/>-->
                    <!--ancestor::isolationMainProcedure-->
                    <!--<xsl:apply-templates select="an../../../.././isolationStep | isolationProcedureEnd[@id=$yesref]" mode="nbr"/> ancestor::isolationMainProcedure[1]/-->
                    <xsl:apply-templates select="key('isolationSteps', $ref)" mode="nbr"/>
                </fo:basic-link></fo:inline>
        </fo:block>
    </xsl:template>
    

    【讨论】:

    • 我不熟悉如何使用密钥,可以告诉我如何实现吗?
    • 我添加了一个(未经测试的)示例。
    • 没想到真的这么简单。一个问题,isolationStep 和isolationProcedureEnd 元素需要同时计算,因为它们是同级的。密钥可以匹配吗?我会在这里测试一下。谢谢。
    • 我想说你只需要&lt;xsl:number count="*"/&gt;(或者如果有其他兄弟元素,可能是&lt;xsl:number count="isolationStep | isolationProcedureEnd"/&gt;)。事实上,这应该是您作为xsl:template 的内容所需要的全部内容:您不需要xsl:variablexsl:value-of,因为xsl:number 无论如何都会返回一个文本节点。见w3.org/TR/xslt20/#numbering-based-on-position
    • yesAnswernoAnswer 是否也引用 isolationProcedureEnd 元素?如果是这样,您需要将xsl:key/@match 更改为match="isolationStep | isolationProcedureEnd",以便您还可以查找isolationProcedureEnd 元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多