Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL - creating drop down list

Status
Not open for further replies.

MrPeds

Programmer
Jan 7, 2003
219
GB
Hi,

I'm a newbie to XSL, so was hoping that somebody could easily see the answer to this:

I am trying to write an XSL style sheet to build an html dropdown list. The list contains simple name-value pairs. The only extra requirement i have is to set one of the options to be 'selected' based on an attribute further up the tree.

this is the xml fragment:

<MetricGroup>

<MetricTargets SelectedMetric="26">

<MetricTarget>
<MetricTargetID>25</MetricTargetID>
<Name>100M</Name>
</MetricTarget>

<MetricTarget>
<MetricTargetID>26</MetricTargetID>
<Name>50M</Name>
</MetricTarget>

</MetricTargets>

</MetricGroup>

As you can see, there are two MetricTargets that I want to list in my drop down. I want to set the 2nd dropdown to be selected. This is my XSL code:

<xsl:element name="Select">
<xsl:attribute name="id">myDropDown</xsl:attribute>

<xsl:for-each select="MetricGroup/MetricTargets/MetricTarget">

<xsl:element name="Option">
<xsl:attribute name="value">
<xsl:value-of select="MetricTargetID"/>
</xsl:attribute>
<xsl:value-of select="Name"/>

<xsl:if test="MetricTargetID = ../@SelectedMetric">
<xsl:attribute name="selected"/>
</xsl:if>
</xsl:element>

</xsl:for-each>


</xsl:element>

I am stuck with how to compare the value of the MetricTargetID to the value of the SelectedMetric attribute. I know I need to use an IF statement to compare them, but I am unsure of how to get the SelectedMetric based on the current context.

Any ideas are appreciated.

MrPeds
 
><xsl:attribute name="selected"/>
[tt]<xsl:attribute name="selected"><xsl:value-of select="'selected'" /></xsl:attribute>[/tt]
 
Thanks for that.

Another solution to this which i implemented was to set up a variable that held the SelectedMetricID and tested if the MetricTargetID equalled the variable.

If the two were equal then i set the attribute.

regards,

MrPeds
 
But the main thing is that the selected attribute has to be set as shown, not empty tag.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top