xsl-list
[Top] [All Lists]

[xsl] Avoiding multiple "apply-templates" by creating one variable for the clauses. Is it possible?

2009-08-20 05:45:20
Hello,

I'm currently working on creating a blog in asp.net, using XML and XSLT 2.0.

In the past (working with XSLT 1.0) I've used multiple "apply-templates" to 
handle all the possible param combinations, but I am looking for a more 
manageable solution.

Current method:

    <xsl:choose>
        <xsl:when test="$AuthorId">
          <xsl:apply-templates select="//blog[author_id = $AuthorId]"/>
        </xsl:when>
        <xsl:when test="$CategoryId">
          <xsl:apply-templates select="//blog[category_id = $CategoryId]"/>
        </xsl:when>
        <xsl:when test="$AuthorId and $CategoryId">
          <xsl:apply-templates select="//blog[author_id = $AuthorId and 
category_id = $CategoryId]"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="//blog"/>
        </xsl:otherwise>
      </xsl:choose>

Obviously it's not ideal, and that's with just two params, in theory there 
could be many more.

I was playing around with feeding the values to the "apply-template" using a 
variable. Something along the lines of:

<xsl:variable name="SelectVar">
    1=1 <!-- always true -->
    <xsl:if test="$AuthorId">
      and author_id = <xsl:value-of select="$AuthorId"/>
    </xsl:if>
    <xsl:if test="$CategoryId">
      and category_id = <xsl:value-of select="$CategoryId"/>
    </xsl:if>
  </xsl:variable>

and then:

<xsl:apply-templates select="//blog[$SelectVar]"/>

Of course, this doesn't work.

Does anyone have any ideas for a more workable solution?

Many thanks,
Kate


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--