2014-02-07 96 views
1

在XSLT中,我有這部分執行。我的問題是,在輸出上,我有重複的數據,它重複數字標題11.3到表11.1和11.2之後。你能幫我避免這種重複嗎?請。我在這裏呆了很長時間。XSLT爲了避免重複模板

<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> 
+0

我的問題正在運行您的示例:'exclude-result-prefixes'是'template'元素的無效屬性。另外,您的輸入XML不是有效的XML,它具有多個根元素。請提供SSCCE –

+0

@ThomasW。在模板上使用'exclude-result-prefixes' [XSLT 2.0允許的_is_](http://www.w3.org/TR/xslt20/#standard-attributes),該代碼必須是因爲它使用'matches'功能。 –

回答

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

將匹配具有class="image"和至少一種前述figurecaption,它包括所有的三個在你的例子的圖像的所有par元素。如果你想只匹配那些立即figurecaption前面,那麼你需要扭轉謂詞圖片:

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

謂詞從左至右解釋,所以這只是那些par元素與class="image"其匹配最接近的前面par是一個figurecaption。

在此約束下,您可以簡化您的n1變量聲明只是

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

因爲你知道這par必須已經class="figurecaption"(或它不會匹配此模板)。

+0

@lan Roberts:謝謝你,它的作品!您對XSLT有很深的瞭解!大 – Sakthivel