Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2004, 03:51 PM
Registered User
 
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default HELP: XSL -> HTML <select selected=true>

I'm trying to do an HTML <select> (drop-down box) control from XSL. I get the list of items, but I cannot specify the selected one. Please help!

I have this XML that holds the list of options and the selected option:

<attributes>
    <combobox>
        <name>country</name>
        <label>Country</label>
        <combobox_option>Canada</combobox_option>
        <combobox_option>USA</combobox_option>
        <combobox_option>Great Britain</combobox_option>
        <combobox_selected>Canada</combobox_selected>
    </combobox>
</attributes>


And I have this XSL which displays the drop-down, but how can I modify this XSL so it inserts <select selected="true"> for the item indicated as selected in the XML? Please help -- I'm stuck!

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
<xsl:template match="attributes">
<html>
<body>
<table>
    <xsl:apply-templates select="checkbox"/>
</table>
</body>
</html>
</xsl:template>


<xsl:template match="combobox">
<xsl:apply-templates select="label"/></td>
<select size="1" name="D1">
<xsl:apply-templates select="combobox_option"/>
</select>
</xsl:template>

<xsl:template match="combobox_option">
    <option><xsl:value-of select="."/></option>
</xsl:template>

</xsl:stylesheet>



 
Old October 6th, 2004, 12:37 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

U can insert the values in the combobox-option which is not in the combobox-selected.
Below is the modified code of yours.This code will select the value of the selected option.I have passed value of selected-option as an parameter to the combobox-option template and inserted the values which is not in combobox-selected..

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="attributes">
<html>
<body>
<table>
    <xsl:apply-templates select="combobox"/>
</table>
</body>
</html>
</xsl:template>


<xsl:template match="combobox">
    <xsl:value-of select="label"/>
    <select size="1" name="D1">
        <xsl:apply-templates select="combobox_selected"/>
        <xsl:apply-templates select="combobox_option">
            <xsl:with-param name="selValue" select="combobox_selected"></xsl:with-param>
        </xsl:apply-templates>
    </select>
</xsl:template>

<xsl:template match="combobox_option">
    <xsl:param name="selValue"></xsl:param>
     <xsl:if test="string($selValue) != string(.)">
        <option><xsl:value-of select="."/></option>
     </xsl:if>
</xsl:template>

<xsl:template match="combobox_selected">
    <option selected="true"><xsl:value-of select="."/></option>
</xsl:template>

</xsl:stylesheet>


 
Old October 7th, 2004, 02:54 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

For this XML,to test for Type and print attribute values see the stylesteet code below..
<attributes>
   <attribute>
     <type>email</type>
     <label>email address</label>
     <value>[email protected]</value>
   </attribute>
   <attribute>
     <type>phone</type>
     <label>phone number</label>
     <phonenumber>613 555 1111</phonenumber>
     <phonenumber>613 555 2222</phonenumber>
     <phonenumber>613 555 3333</phonenumber>
   </attribute>
</attributes>

XSL Code snippet:

<xsl:for-each select="attribute">
   <xsl:if test="string(type)='email'">
    <xsl:value-of select="value"></xsl:value-of>
   </xsl:if>
   <xsl:if test="string(type)='phone'">
    <xsl:value-of select="phonenumber"></xsl:value-of>
   </xsl:if>
</xsl:for-each>

 
Old October 7th, 2004, 09:02 AM
Registered User
 
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Excellent -- thank you Santhi! (This is an answer to a question I asked him elsewhere.)

That got me on the right track, here's my finished code which has some modifications to yours:

The XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="phone2.xsl"?>

<attributes>
   <attribute>
     <type>email</type>
     <label>email address</label>
     <value>[email protected]</value>
   </attribute>
   <attribute>
     <type>phone</type>
     <label>phone number</label>
     <phonenumber>613 555 1111</phonenumber>
     <phonenumber>613 555 2222</phonenumber>
     <phonenumber>613 555 3333</phonenumber>
   </attribute>
</attributes>


The XSL:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
<xsl:template match="attributes">


<xsl:for-each select="attribute">
    <xsl:if test="string(type)='email'">
    <table>
    <tr>
      <td><b><xsl:value-of select="label"></xsl:value-of></b></td>
      <td><xsl:value-of select="value"></xsl:value-of></td>
    </tr>
    </table>
    </xsl:if>

    <xsl:if test="string(type)='phone'">
    <table>
    <tr>
      <td><b><xsl:value-of select="label"></xsl:value-of></b></td>
          <td><xsl:apply-templates select="phonenumber"/></td>
        </tr>
    </table>
    </xsl:if>
</xsl:for-each>

</xsl:template>


<xsl:template match="phonenumber">
    <xsl:value-of select="."/><br/>
</xsl:template>


</xsl:stylesheet>


 
Old October 7th, 2004, 11:16 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

Its really good that you have completed..






Similar Threads
Thread Thread Starter Forum Replies Last Post
When <xsl:value-of select="Tooltip" /> = value ismailc XSLT 3 October 17th, 2008 04:43 AM
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 08:06 AM
selected value of nested repeater-build <select> bttrflii ASP.NET 2.0 Professional 2 September 15th, 2006 09:52 AM
<xsl:for-each> with a variable select="attribute" BeneathClouds XSLT 1 August 1st, 2005 03:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.