开发者

XSLT xsl:apply-templates Conditional Syntax

开发者 https://www.devze.com 2023-03-29 03:05 出处:网络
I\'ve got the following XSLT code which lists out the folders and their file items from a specified node.

I've got the following XSLT code which lists out the folders and their file items from a specified node.

This all works fine but I'd like to parameterise the page and optionally filter its output by a tag value.

Being an XLST numpty I'm stumped with the syntax for the cond开发者_如何转开发itional I should be putting in under the <xsl:when test="$tag"> clause - can someone please help ?

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


Instead of the long <xsl:choose> instruction, use:

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

Explanation: For the XPath expression in the select attribute above to select a non-empty set of nodes it is necessary that boolean($tag) is true(). Thus the above single <xsl:apply-templates> instruction is equivalent to the long <xsl:choose> in the question.


you can test if $tag is set like this.

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

This isn't standard though, it'll work on most XSLT processors though.

If you wanted to be absolutely save, you could set the value to an illegal value (such as 1 div 0) and test for it in the body of the template:

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

Source: O'Reilly XSLT Cookbook

0

精彩评论

暂无评论...
验证码 换一张
取 消