Retaining drop down list value in html using xslt?

3.5k Views Asked by At

Not sure if I am answering the question right, but I will try :-

I have a html page(xml+xslt) that has a drop down list containing 10 values. When I select a value, ex. ABC, I can perform add or delete or search. Now on every other pages(add, delete, search), i have a home button. When I click it, it goes to home, but the value of drop down is resetted.

How do I retain the value selected by user?

I have the following piece of code, but it is not working not sure why.

I am working on TIBCO BusinessWorks.

 <tr > <td>
 <select name= "GetRelationCombo">
        <xsl:for-each select="resultSet/Record">
             <xsl:sort select="REL_NAME" />

                  <option> 
                    <xsl:attribute name="value">
            <xsl:value-of select="REL_NAME" />
               </xsl:attribute>
                   <xsl:value-of select="REL_NAME" />
    <xsl:if test="REL_NAME = 'resultSet/RelationshipName'">
    <xsl:attribute name="selected">true</xsl:attribute>
    </xsl:if>

                 </option>            
      </xsl:for-each>
 </select>
</td></tr>

This is the input XML:

<?xml version = "1.0" encoding = "UTF-8"?>
<resultSet>
  <Record>
    <REL_NAME>ShapeID</REL_NAME>
  </Record>
  <Record>
    <REL_NAME>eMPSQPType</REL_NAME>
  </Record>
  <Record>
    <REL_NAME>GERSGLAccount</REL_NAME>
  </Record>
  <Record>
    <REL_NAME>WageType</REL_NAME>
  </Record>
  <RelationshipName>PLANT</RelationshipName>
</resultSet>

Please help!! I am in middle of something big.

1

There are 1 best solutions below

5
On

Firstly, any attributes you use need to occur before anything inside an element, so this:

<xsl:if test="REL_NAME = 'resultSet/RelationshipName'">
    <xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>

should be above this:

<xsl:value-of select="REL_NAME" />

this isn't correct, because it will only be true when REL_NAME actually has the value "resultSet/RelationshipName":

REL_NAME = 'resultSet/RelationshipName'

This is the actual condition you need:

REL_NAME = ../RelationshipName

Modified XSL:

  <option>
    <xsl:attribute name="value">
      <xsl:value-of select="REL_NAME" />
    </xsl:attribute>
    <xsl:if test="REL_NAME = ../RelationshipName">
      <xsl:attribute name="selected">true</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="REL_NAME" />
  </option>

As a side note, I think the correct value for the selected attribute is "selected", not "true", even though "true" might work anyway.

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

http://reference.sitepoint.com/html/option/selected