2011-08-17 22 views
0

我有以下XSLT代碼,列出了指定節點中的文件夾及其文件項。XSLT xsl:apply-templates有條件語法

這一切工作正常,但我想參數化頁面,並有選擇地過濾其標籤值的輸出。

作爲一個XLST numpty我對於有條件的語法難以置信,我應該在<xsl:when test="$tag">條款下 - 可有人請幫忙嗎?

<xsl:variable name="tag" select="umbraco.library:Request('tag')" /> 

     <xsl:template match="/"> 
      <!-- Root folder in Media that holds the folders to output --> 
      <xsl:variable name="mediaRootFolderId" select="5948" /> 

      <!-- Pass in true() to get XML for all nodes below --> 
      <xsl:variable name="mediaRootNode" select="umbraco.library:GetMedia($mediaRootFolderId, true())" /> 

      <xsl:choose> 
      <xsl:when test="$tag"> 

      </xsl:when> 

      <xsl:otherwise> 
       <!-- If we didn't get an error, output Folder elements that contain Image elements --> 
       <xsl:apply-templates select="$mediaRootNode[not(error)]/Folder[File]" > 
        <xsl:sort select="@nodeName"/> 
       </xsl:apply-templates> 

      </xsl:otherwise> 
      </xsl:choose> 

      </xsl:template> 

     <!-- Template for folders --> 
     <xsl:template match="Folder"> 
       <div class="folder"> 
         <h2>Folder: <xsl:value-of select="@nodeName" /></h2> 
         <div class="images">         
          <xsl:apply-templates select="File"> 
          <xsl:sort select="@nodeName"/> 
          </xsl:apply-templates> 
         </div> 
       </div> 
     </xsl:template> 

     <!-- Template for files --> 
     <xsl:template match="File"> 
      File: <a href="{umbracoFile}" alt="{@nodeName}" ><xsl:value-of select="@nodeName" /></a> <br/> 
     </xsl:template> 
+0

您需要編輯這篇文章並將您的單引號切換爲備份,以便代碼在您的代碼片段上方的解釋中不會消失。 back-ticks與tilda相同。 –

+0

好問題,+1。看到我的答案是一個簡單,簡單和簡單的解決方案。 :) –

回答

1

取而代之的是長<xsl:choose>指令,使用:

<xsl:apply-templates select= 
    "$mediaRootNode[not($tag)][not(error)] 
           /Folder[File]" > 

說明:對於上述select屬性中的XPath表達式來選擇非空集節點,需要boolean($tag)true()。因此,上述單個<xsl:apply-templates>指令相當於問題中的長整數<xsl:choose>

0

您可以測試$ tag是否如此設置。

<xsl:param name="tag"> 
    <xsl:message terminate="yes"> 
     $tag has not been set 
    </xsl:message> 
</xsl:param> 

雖然這不是標準的,但它可以在大多數XSLT處理器上工作。

如果你想絕對保存,你可以將該值設置爲非法值(如1分格0),並測試其在模板的正文:

<xsl:param name="tag" select="1 div 0" /> 

<xsl:if test="$tag = 1 div 0"> 
    <xsl:message terminate="yes"> 
     $tag has not been set, or has been set to Infinity, which is invalid. 
    </xsl:message> 
</xsl:if> 

來源:O」賴利XSLT食譜