AxV
AxV

Reputation: 181

XSLT - xsl choose

I have a problem i need to match two parameters in xsl choose clause, is there a way to achieving this?

for example: xsl:when test= i need to check two parameters, so then i can check for the same price but without the ordertype lower.

<xsl:choose>
    <xsl:when test="price = 10" && "OrderType='P' ">
      <td bgcolor="#ff00ff">
      <xsl:value-of select="artist"/></td>
    </xsl:when>
    <xsl:when test="price = 10">
      <td bgcolor="#cccccc">
      <xsl:value-of select="artist"/></td>
    </xsl:when>
    <xsl:otherwise>
      <td><xsl:value-of select="artist"/></td>
    </xsl:otherwise>
  </xsl:choose>

Upvotes: 2

Views: 7735

Answers (3)

user764357
user764357

Reputation:

Extending the previous answers, I'd also suggest putting your styling information into CSS, incase you want more than just the background to change at a later stage. Also, you don't need the <xsl:text> elements, they just keep whitespace to a minimum, but this shouldn't be as much of an issue in attributes as your think.

Also, where possible I like using attribute value templates to keep the XSL as close to what the output would be as possible, but that is purely a stylistic choice.

<xsl:variable name="cellClass">
    <xsl:choose>
        <xsl:when test="price = 10 and OrderType='P' ">
            cellPriceTenAndP
        </xsl:when>
        <xsl:when test="price = 10">
            cellPriceTen
        </xsl:when>
    </xsl:choose>
</xsl:variable>
<td class="{$cellClass}">
    <xsl:value-of select="artist"/>
</td>

Upvotes: 1

Kevin Brown
Kevin Brown

Reputation: 8877

On my comment above, I would do that this way to save myself future efforts of changing "artist" or something like that. The choose relates solely to bgcolor and should be applied to only that (eliminating the otherwise condition by the way):

    <td>
        <xsl:attribute name="bgcolor">
            <xsl:choose>
                <xsl:when test="price = 10 and OrderType='P' ">
                    <xsl:text>#ff00ff</xsl:text>
                </xsl:when>
                <xsl:when test="price = 10">
                    <xsl:text>#cccccc</xsl:text>
                </xsl:when>
            </xsl:choose>
        </xsl:attribute>
        <xsl:value-of select="artist"/>
    </td>

Upvotes: 1

Max Toro
Max Toro

Reputation: 28618

<xsl:choose>
    <xsl:when test="price = 10 and OrderType='P' ">
      <td bgcolor="#ff00ff">
      <xsl:value-of select="artist"/></td>
    </xsl:when>
    <xsl:when test="price = 10">
      <td bgcolor="#cccccc">
      <xsl:value-of select="artist"/></td>
    </xsl:when>
    <xsl:otherwise>
      <td><xsl:value-of select="artist"/></td>
    </xsl:otherwise>
  </xsl:choose>

Upvotes: 5

Related Questions