2014-01-09 20 views
0

我有下面的XML代碼獲得第一前同輩和應用模板

<root> 
<title num="1/2"><content-style font-style="bold">Application</content-style> (O 1 r 2)</title> 
<para><content-style font-style="bold">2.</content-style>—(1) Subject to paragraph (2), these Rules apply to all proceedings in-</para> 
<para>(2) These Rules do not have effect in relation to proceedings in respect of which rules have been or may be made under any written law for the specific purpose of such proceedings or in relation to any criminal proceedings.</para> 
<para>(3) Where prior to the coming into operation of these Rules, reference is made in any written law to any Rules of Court that reference shall be to these Rules.</para> 
</root> 

及以下XSLT

<xsl:template name="para" match="para"> 
    <xsl:choose> 
    <xsl:when test="./@align"> 
     <div class="para align-{@align}"> 
     <xsl:apply-templates/> 
     </div> 
    </xsl:when> 
    <xsl:otherwise> 

     <xsl:choose> 
     <xsl:when test="preceding-sibling::title[1]/@num[1]"> 
      <xsl:apply-templates/> 
     </xsl:when> 
     <xsl:otherwise> 
      <div class="para"> 
      <xsl:apply-templates/> 
      </div> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:otherwise> 
    </xsl:choose> 

</xsl:template> 

這裏的時候,我試圖運行此<xsl:when test="preceding-sibling::title[1]/@num[1]">未正常工作預期。對於第一個para模板應該直接適用於第二和第三段,首先應該有一個<div class="para">,並在其內部應用模板(否則條件),但在我的情況下,所有的<xsl:when test="preceding-sibling::title[1]/@num[1]">段越來越滿意,它直接應用模板。請讓我知道我哪裏錯了。

感謝

回答

2

嘗試

<xsl:when test="preceding-sibling::*[1][name()='title']/@num"> 
3

更換

<xsl:when test="preceding-sibling::title[1]/@num[1]"> 

preceding-sibling::*軸前面的所有兄弟姐妹,而不僅僅是眼前的兄弟相匹配,所以你首先要選擇立即兄弟姐妹,然後檢查它是否是title元素:

preceding-sibling::*[1]/self::title 

我建議使用更XSLT上下的方法和更具體的模板替換的嵌套<xsl:choose>

<xsl:template match="para[@align]" priority="2"> 
    <div class="para align-{@align}"> 
    <xsl:apply-templates/> 
    </div> 
</xsl:template> 

<xsl:template match="para[preceding-sibling::*[1]/self::title/@num]" priority="1"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="para"> 
    <div class="para"> 
    <xsl:apply-templates/> 
    </div> 
</xsl:template> 

如果你堅持要用<xsl:choose>,只使用一個:

<xsl:template match="para"> 
    <xsl:choose> 
    <xsl:when test="./@align"> 
     <div class="para align-{@align}"> 
     <xsl:apply-templates/> 
     </div> 
    </xsl:when> 
    <xsl:when test="preceding-sibling::*[1]/self::title/@num"> 
     <xsl:apply-templates/> 
    </xsl:when> 
    <xsl:otherwise> 
     <div class="para"> 
     <xsl:apply-templates/> 
     </div> 
    </xsl:otherwise> 
    </xsl:choose>  
</xsl:template>