2011-08-17 16 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

この投稿を編集して、単一引用符をバックティックに切り替える必要があります。そのため、コードスニッピの上の説明でコードが消えないようにします。バックティックはティルダと同じキーにあります。 –

+0

良い質問、+1。私の答えは、簡単で簡単で簡単な解決策を見てください。 :) –

答えて

1

<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 DIV 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」代わりに、長い<xsl:choose>命令、使用のライリーXSLTクックブック