XSLT避免重复模板

时间:2014-02-07 14:07:17

标签: xslt xslt-2.0

在XSLT中,我有这个部分要执行。我的问题是,在输出中,我有重复的数据,它重复了图11.1和11.2之后的图标题11.3。你能帮我避免这种重复吗?请。我在这里坚持了很久。

<xsl:template match="par[@class='image'][preceding-sibling::par[@class='figurecaption'][1]]" exclude-result-prefixes="html">
 <p class="image" style="border: 2pt solid red">
 <xsl:variable name="n1" select="preceding-sibling::par[@class='figurecaption'][1]"/>
 <xsl:attribute name="id">
 <xsl:if test="matches($n1, '(Figure)\s(\d+|[A-Z])(\.)(\d+)')">
 <xsl:variable name="y1" select="replace($n1, '(Figure)\s(\d+|[A-Z])(\.)(\d+)', '$4')"/>Fig<xsl:value-of select="normalize-space(substring($y1, 1, 2))"/></xsl:if>
 <xsl:if test="not(matches($n1, '(Figure)\s(\d+|[A-Z])(\.)(\d+)'))">
 <xsl:value-of select="'Forward'"/>
 </xsl:if> </xsl:attribute> <img> <xsl:attribute name="src">
 <xsl:text>../images/</xsl:text>
 <xsl:value-of select="."/>
 <xsl:text>.jpg</xsl:text>
 </xsl:attribute> <xsl:attribute name="alt">
 <xsl:value-of select="."/>
 </xsl:attribute> </img> </p>
 <p class="caption"> <strong> <em> <!-- <xsl:copy-of select="./preceding-sibling::par[@class='figurecaption'][position()=1]"/> --> <xsl:for-each select="./preceding-sibling::par[@class='figurecaption'][position()=1]">
 <xsl:value-of select="."/> </xsl:for-each> </em> </strong> </p> </xsl:template>

并输入

<par class="figurecaption">Figure 11.3  Relationship between processes, activities and actions</par>
        <par class="image">gr000032</par>

        <par class="para">A diagram is provided for each <inline style="font-weight: bold;">activity</inline> showing the inputs and <inline class="glossaryrefmain">output</inline>s, including those products that are created or updated by that activity. The recommended actions to be taken to achieve the objectives of the activity are described.</par>
        <par class="para">Each <inline class="glossaryrefmain">activity</inline> is concluded by a table showing the responsibilities for each <inline class="glossaryrefmain">product</inline> created or updated during the activity, as illustrated in Table 11.1.</par>

        <par class="tablecaption">Table 11.1  An example of a table of responsibilities</par>
        <par class="image">gr000033</par>

        <par class="para">Note that <inline class="glossaryrefmain">management product</inline>s created during one <inline class="glossaryrefmain">process</inline> may be approved in another (e.g. a <inline class="glossaryrefmain">Stage Plan</inline> is created in the Managing a Stage Boundary process but is approved in the Directing a Project process). However, the complete set of responsibilities is shown, and those covered by another process are indicated by being shown in parentheses, e.g. (A).</par>

        <par class="tablecaption">Table 11.2  Key to process diagrams</par>
        <par class="image">gr000034</par>

1 个答案:

答案 0 :(得分:2)

match="par[@class='image'][preceding-sibling::par[@class='figurecaption'][1]]"

将匹配所有par元素class="image"和至少一个前figurecaption元素,其中包含示例中的所有三个图片。如果您只想匹配那些紧接前面有figurecaption的图片,那么您需要撤消谓词:

match="par[@class='image'][preceding-sibling::par[1][@class='figurecaption']]"

谓词从左到右解释,因此这只与par元素匹配class="image",其中最接近的par是一个数字标题。

使用此约束,您可以将n1变量声明简化为

<xsl:variable name="n1" select="preceding-sibling::par[1]"/>

如您所知par 必须 class="figurecaption"(或者它与此模板不匹配)。