2011-08-29 92 views
1

這裏是我使用的代碼:XSLT中是否有任何一種模板「節點的功能」?

<xsl:template match="Row[position() = 1]"> 
    <li style="width: 650px; float: left; list-style: none outside none;"> 
    <ul class="liste1"> 
     <xsl:if test="@Style='NewsCustomTemplate'"> 
     <li> 
      <div style="width:640px; color:#40494f; font-size:12"> 
      <b style="color:black"> 
       <xsl:value-of select="./@Title"/> 
      </b> 
      <xsl:choose> 
       <xsl:when test="string-length(./@Description)&gt;300"> 
       <xsl:value-of disable-output-escaping="yes" select="substring(./@Description,1,300)"/>... 
       </xsl:when> 
       <xsl:otherwise> 
       <xsl:value-of disable-output-escaping="yes" select="./@Description"/> 
       </xsl:otherwise> 
      </xsl:choose> 
      </div> 
      <div> 
      <a class="" href="#" target="" title=""> 
       read more 
      </a> 
      </div> 
     </li> 
     <xsl:if test="following-sibling::*[1]"> 
      <li> 
      <div style="width:640px; color:#40494f; font-size:12"> 
       <b style="color:black"> 
       <xsl:value-of select="following-sibling::*[1]/@Title"/> 
       </b> 
       <xsl:choose> 
       <xsl:when test="string-length(following-sibling::*[1]/@Description)&gt;300"> 
        <xsl:value-of disable-output-escaping="yes" select="substring(following-sibling::*[1]/@Description,1,300)"/>... 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of disable-output-escaping="yes" select="following-sibling::*[1]/@Description"/> 
       </xsl:otherwise> 
       </xsl:choose> 
      </div> 
      <div> 
       <a class="" href="#" target="" title=""> 
       read more 
       </a> 
      </div> 
      </li> 
     </xsl:if> 
     <xsl:if test="following-sibling::*[2]"> 
      <li> 
      <div style="width:640px; color:#40494f; font-size:12"> 
       <b style="color:black"> 
       <xsl:value-of select="following-sibling::*[2]/@Title"/> 
       </b> 
       <xsl:choose> 
       <xsl:when test="string-length(following-sibling::*[2]/@Description)&gt;300"> 
        <xsl:value-of disable-output-escaping="yes" select="substring(following-sibling::*[2]/@Description,1,300)"/>... 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of disable-output-escaping="yes" select="following-sibling::*[2]/@Description"/> 
       </xsl:otherwise> 
       </xsl:choose> 
      </div> 
      <div> 
       <a class="" href="#" target="" title=""> 
       read more 
       </a> 
      </div> 
      </li> 
     </xsl:if> 
     </xsl:if> 
     <xsl:if test="@Style='AgendaCustomTemplate'"> 
     </xsl:if> 
    </ul> 
    </li> 
</xsl:template> 

我的代碼的問題是,我幾乎重複使用完全相同的代碼三次:「」

  • 一個當前項目
  • 一個用於第一兄弟「以下同胞:: * [1]」
  • 一個用於第二兄弟「以下同胞:: * [2]」

我想有排序通用模板來執行這部分節點LOCATIONX:

<li> 
    <div style="width:640px; color:#40494f; font-size:12"> 
    <b style="color:black"> 
     <xsl:value-of select="LOCATIONX/@Title"/> 
    </b> 
    <xsl:choose> 
     <xsl:when test="string-length(LOCATIONX/@Description)&gt;300"> 
     <xsl:value-of disable-output-escaping="yes" select="substring(LOCATIONX/@Description,1,300)"/>... 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of disable-output-escaping="yes" select="LOCATIONX/@Description"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </div> 
    <div> 
    <a class="" href="#" target="" title=""> 
     read more 
    </a> 
    </div> 
</li> 

有誰知道,如果這樣的事情在XSLT是可能的嗎?或者我必須保持我的代碼重複?

回答

3

首先擺脫disable-output-escaping =「yes」。它可能被那些不知道它意味着什麼的人放在那裏,成爲魔法仙塵。機會是不需要的,在這種情況下刪除它並沒有什麼壞處;如果沒有它,轉換不能正常工作,那麼設計就會出現嚴重錯誤。

至於你的問題,把公共代碼裏面

<xsl:template match="Row" mode="m">...</xsl:template> 

,然後做

<xsl:template match="Row[position()=1]"> 
    <xsl:apply-templates select="." mode="m"/> 
    <xsl:apply-templates select="following-sibling::Row[1]" mode="m"/> 
    <xsl:apply-templates select="following-sibling::Row[2]" mode="m"/> 
</xsl:template> 

或者更簡單地說

<xsl:template match="Row[position()=1]"> 
    <xsl:apply-templates select=".|following-sibling::Row[3 > position()]" mode="m"/> 
</xsl:template> 
+0

非常感謝你,我的代碼是乾淨多了現在! – Holden

相關問題