tags:

views:

666

answers:

2

Hi,

I have a list of items like how its listed below, and I'm trying to get that option selected by default which has a certain tag within the "listing" tag.

<select>
<xsl:attribute name="name"><xsl:value-of select="@id"/>_type</xsl:attribute>
<option><xsl:attribute name="value"><xsl:if test="listing/Chat"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>Chat</xsl:attribute>Chat</option>
...

Am I placing the xsl:if incorrectly?

+1  A: 

It's hard to tell from the code you've provided, but I'm guessing you want results that look like this:

<select name="000_type">
<option value="chat" selected="selected"/>
<option value="chat"/>
</select>

In which case you want XSL that looks like this:

<xsl:element name="select">
    <xsl:attribute name="name" select="concat(@id,'_type'))"/>
</xsl:element>
<xsl:element name="option">
    <xsl:attribute name="value">
       <xsl:value-of select="chat"/>
    </xsl:attribute>
    <xsl:if test="listing/Chat">
       <xsl:attribute name="selected">
          <xsl:text>selected</xsl:text>
       </xsl:attribute>
    </xsl:if>
</xsl:element>
Jweede
+1  A: 

Your code isn't valid because you've nested one <xsl:attribute> tag inside another. I believe you want something like one of the following:

<select name="{@id}_type">
    <option value="Chat">
        <xsl:if test="listing/Chat">
            <xsl:attribute name="selected">selected</xsl:attribute>
        </xsl:if>

        <xsl:text>Chat</xsl:text>
    </option>
</select>

or

<select name="{@id}_type">
    <xsl:if test="listing/Chat">
        <option value="Chat" selected="selected">Chat</option>
    </xsl:if>
</select>

The former always displays the "Chat" option, but only selects it if the condition is met. The latter doesn't display "Chat" at all unless the condition is met.

Ben Blank
Thanks guys! I based it off Ben's code to get it to work. Apologies for not posting it clearly above.Final code looks like this:<select name="{@id}_type"><option value="Chat"><xsl:if test="listing/Chat"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if><xsl:text>Chat</xsl:text></option>......
Steve