xsl:attribute tag not working

robad
robad used Ask the Experts™
on
This is driving me crazy ...  Here is a fragement of xsl from an xsl style sheet.

         <TR>
              <TD align="center" width="82%" valign="middle">
                   <xsl:variable name="SortOrder" select="/Notifications/Sort/@Order"/>
                   <B><font color="#FFFFFF" face="arial">Sort by </font></B>
                   <SELECT id="select1" style="FONT-SIZE: x-small; WIDTH: 150px" name="sortby" onchange="javascript:newSort(value);">
                         <OPTION value="1">3 Month Reservations</OPTION>
                              <xsl:if test="$SortOrder[. = 1]">
                                   <xsl:attribute name="SELECTED"></xsl:attribute>          
                              </xsl:if>
                         <OPTION value="2">Property Name</OPTION>
                              <xsl:if test="$SortOrder[. = 2]">
                                   <xsl:attribute name="SELECTED"></xsl:attribute>          
                              </xsl:if>                        
                         <OPTION value="3">Property Id</OPTION>
                              <xsl:if test="$SortOrder[. = 3]">
                                   <xsl:attribute name="SELECTED"></xsl:attribute>          
                              </xsl:if>                        
                    </SELECT>
                              <xsl:if test="$SortOrder[. = 2]">
                                   <xsl:text>TEST WAS PASSED</xsl:text>
                              </xsl:if>
               </TD>
         </TR>

All I want to do is to set the SELECTED attribute.

Here is the HTML I get:

<TR>
<TD align="center" width="82%" valign="middle"><B><font color="#FFFFFF" face="arial">Sort by </font></B><SELECT id="select1" style="FONT-SIZE: x-small; WIDTH: 150px" name="sortby" onchange="javascript:newSort(value);"><OPTION value="1">3 Month Reservations</OPTION>
<OPTION value="2">Property Name</OPTION>
<OPTION value="3">Property Id</OPTION></SELECT>TEST WAS PASSED</TD>
</TR>

Please note that the last text string indicates that I am picking up the desired node value and testing it properly.  The only issue is that the SELECTED attribute is, never-the-less NOT set.

Can anyone give me a clue as to what might be going on?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Robad,

Although I don't think that this is your problem, you should give the selected attribute a value. In XHTML this should be the name of the attribute.

e.g.
<xsl:attribute name="selected">selected</xsl:attribute>

Apart from that I can't see any problem with the XSL code. I would however need to see the XML that you are tranforming. What does 'select="/Notifications/Sort/@Order"/' actually match? If this is not 1, 2 or 3, then you will not get the select attribute in the output.

>S'Plug<

You could also try:
<xsl:if test="number($SortOrder) = 2]">

>S'Plug<

Commented:
As sparky said, give the attribute a value, and ensure that the xsl:output method is set to html. Then the tree flattener will produce the correct HTML.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Author

Commented:
I should clarify that the following (from my original question) is debugging code to make sure that the xsl:if test = is properly structured:
                             <xsl:if test="$SortOrder[. = 2]">
                                  <xsl:text>TEST WAS PASSED</xsl:text>
                             </xsl:if>

When the source xml contains <Sort Order="2"/>, the html output contains

<OPTION value="3">Property Id</OPTION></SELECT>TEST WAS PASSED</TD>

This tells me that the problem is NOT with the if test.  Also, I have already tried giving the SELECTED attribute a value along the lines of

<xsl:attribute name="SELECTED">SELECTED</xsl:attribute>    

OR

<xsl:attribute name="SELECTED"><xsl:text></xsl:text></xsl:attribute>

Neither makes a difference.  Further, I am using Microsoft XSL 4.0 which creates an attribute

SELECTED = ""

with no value assigned in a different style sheet.    
Hmm... Yep, missed the 'TEST WAS PASSED' bit.

Also missed the incorrect syntax:  

Instead of:
<OPTION value="2">Property Name</OPTION>
<xsl:if test="$SortOrder[. = 2]">
 <xsl:attribute name="SELECTED"></xsl:attribute>          
</xsl:if>                

Try:

<OPTION value="2">
<xsl:if test="$SortOrder[. = 2]">
 <xsl:attribute name="SELECTED"></xsl:attribute>          
</xsl:if>        
Property Name</OPTION>

The attribute must be applied just after the start element tag.

>S'Plug<
       

Author

Commented:
Thanks much!  

Sometimes I can't see for seeing with my own code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial