cryotorched
cryotorched

Reputation: 19

Displaying data from XML in a select field using an XSLT

I'm sure this is another XSLT question with a fairly simple solution but again I haven't seen any helpful thread around the net on the topic that I have been able to use. I have an XSLT form containing a bunch of select fields that I need to set to show the data contained within XML from our database, when a query is sent. I am currently using small example codes for the XML and XSL testing. The test XML is as follows

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test2.xsl"?>
<root>
<radiobuttons>
    <radio1>Y</radio1>
    <blurb>blahblahblah</blurb>
    <hidden>N</hidden>
    <check1>Y</check1>
    <select1>3</select1>
</radiobuttons>
</root>

Below is the XSLT I am using with this XML. I have set up both the checkboxes and the radio buttons to check themselves dependant upon the data within the XML using xsl:if commands. The test hidden div also displays dependant on XML data using the same xsl:if. The input field just displays the XML data it is targeted at.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" omit-xml-declaration="no"
            encoding="UTF-8"/>
<xsl:template match="/">
<HTML>
<BODY>
<form>
<xsl:apply-templates select="root"/>
</form>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="root">

<input type="radio" name="radio1" value="Y" >
<xsl:if test="radiobuttons/radio1='Y'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Radio Button 1

<input type="radio" name="radio1" value="N" >
<xsl:if test="radiobuttons/radio1='N'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Radio Button 2

<div> <xsl:if test="radiobuttons/hidden='N'">
<xsl:attribute name="style">display:none</xsl:attribute></xsl:if> Yaddah Yaddah Yaddah
</div>

<input type="checkbox" name="check1" value="Y">
<xsl:if test="radiobuttons/check1='Y'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Checkbox test

<label for="select1">select1:</label>
<select id="select1" ><xsl:value-of select="radiobuttons/select1"></xsl:value-of>
  <option value="1">1</option>  <option value="2">2</option>  <option value="3">3</option>  <option value="4">4</option>
  </select>
  <br></br>

<br/>
<input name="blurb" type="text" id="blurb" value="{./radiobuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>

However I don't think I can make a select field function correctly using xsl:if because it is Boolean. I think I could probably write an xsl:if command for every option which would produce the desired result, however that seems like a very ham handed method of making this work and I'm sure there must be a much more elegant solution to this. I also haven't had any success making it work using the value method for the input field.

From the example codes above I would like the select field to show the 3rd option as noted in the XML. Can anyone suggest, or even better give me an example of, how I could code a select field into my XSLT that will behave in the manner described above? Any help would be really gratefully received!

Upvotes: 0

Views: 9383

Answers (1)

Tim C
Tim C

Reputation: 70608

What you could do, is put all four options for your select statement in your XSLT to act as a sort of look-up table (where the my namespace would have to be defined at the top of your XSLT)

<my:options> 
  <option>1</option> 
  <option>2</option> 
  <option>3</option> 
  <option>4</option> 
</my:options> 

Then, to access these options in the XSLT, you can create a variable, like so...

<xsl:variable name="options" select="document('')//my:options/*"/> 

Then, to render your select statement, it is just a case of looping through the options. Although an xsl:if is still used, you only have to code it once.

  <select id="select1">
     <xsl:for-each select="$options">
        <option value="{.}">
           <xsl:if test=". = $select1">
              <xsl:attribute name="selected">true</xsl:attribute>
           </xsl:if>
           <xsl:value-of select="."/>
        </option>
     </xsl:for-each>
  </select>

Here is the abridged XSLT (containing only the code for the select)

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="mynamespace"
 exclude-result-prefixes="my">

   <xsl:output method="html" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/>

   <my:options>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
   </my:options>
   <xsl:variable name="options" select="document('')//my:options/*"/>

   <xsl:template match="/">
      <HTML>
         <BODY>
            <form>
               <xsl:apply-templates select="root"/>
            </form>
         </BODY>
      </HTML>
   </xsl:template>

   <xsl:template match="root">
      <xsl:variable name="select1" select="radiobuttons/select1"/>
      <select id="select1">
         <xsl:for-each select="$options">
            <option value="{.}">
               <xsl:if test=". = $select1">
                  <xsl:attribute name="selected">true</xsl:attribute>
               </xsl:if>
               <xsl:value-of select="."/>
            </option>
         </xsl:for-each>
      </select>
   </xsl:template>
</xsl:stylesheet>

When applied to your sample XML, the following is output

<HTML>
<BODY>
<form>
<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="true">3</option>
<option value="4">4</option>
</select>
</form>
</BODY>
</HTML>

Upvotes: 0

Related Questions