在XSL中引用数字

时间:2015-01-14 22:00:59

标签: xml xslt

问题:我正在尝试引用HTML文本中的数字,这是一个指向图形标题的内部链接。

图形和引用的XML代码是:

(...) As it can be seen in the Figure <p:iref intref="f1"/> (...)

<float:figure id="f1">
     <float:graphic path="f1.jpg"/>
     <float:caption>
         <p:p> Some text </p:p>
     </float:caption> 

用于处理数字的XSL代码如下:

<xsl:template match="float:figure">
    <a name="{@id}"></a>
    <br/>
    <xsl:apply-templates select="float:graphic"/>
    <br/>
    <center>        
        <b>Figure <xsl:value-of select="count(preceding::float:figure)+1"/> - </b> <xsl:value-of select="float:caption"/>                                                          
    </center>
    <br/>
</xsl:template>

<xsl:template match="//float:graphic">
    <xsl:variable name="path" select="./@path"/>
    <center>
        <img src="{$path}"/>
    </center>        
</xsl:template>

现在,为了参考数字:

<xsl:template match="p:iref">
    <xsl:variable name="fid" select="@intref"/>
    <xsl:if test="//float:figure/@id=$fid">           
       <a href="#{$fid}"><xsl:value-of select="count(preceding::float:figure)+1"/></a> 
    </xsl:if>
</xsl:template>

事情是:当引用(intref)出现在图之前时,引用工作正常。但是当我想要引用已经传递的图像时,尽管链接工作正常,但它引用了一个不同的数字。

示例:

HTML中的当前结果:

(...) As it can be seen in Figure 1 (...)

       -----------some Figure------------
       Figure 1 - some text

(...) and like we've seen back then, in Figure 2 (...)

HTML中的所需结果:

(...) As it can be seen in Figure 1 (...)

       -----------some Figure------------
       Figure 1 - some text

(...) and like we've seen back then, in Figure 1 (...)

所以看起来虽然引用很好(在第一个输出中,如果我单击链接,这是图2中的数字2,它引用右图1),如图2所示......

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您需要计算引用图中的preceding::float:figure元素,而不是引用本身。怎么样:

<xsl:template match="p:iref">
    <xsl:variable name="fid" select="@intref"/>
    <xsl:variable name="target" select="//float:figure[@id=$fid]"/>
    <xsl:if test="$target">
        <a href="#{$fid}"><xsl:value-of select="count($target/preceding::float:figure)+1"/></a> 
    </xsl:if>
</xsl:template>