Re: [xsl] Creating a html <select> dropdown menu in XSL, where the attribute of an XML element is the selected value when page loads

Subject: Re: [xsl] Creating a html <select> dropdown menu in XSL, where the attribute of an XML element is the selected value when page loads
From: Martin Jackson <martsonjackin@xxxxxxxxx>
Date: Mon, 4 Jan 2010 18:36:12 +0100
I'm sorry if I haven't been following the posting guidelines properly.
I'm aware that everybody posting here are volunteers and I'm grateful
for all the help I have been receiving.

This mail is quite long, I hope that in itself doesn't mean I'm
breaking posting guidelines. I don't know how to describe my question
properly in any shorter form.

Regarding the processor and XSLT version, your processor-version.xsl
file tells me the following:

"XSL version: 1

Vendor: Transformiix

Vendor URL: http://www.mozilla.org/projects/xslt/";

So I guess that means I'm using the Transformiix processor and that it
only supports XSLT 1.


> You must have only 37 elements in your document.

> In your original post you mention
> creating <option> elements, but you don't reveal the basis for enumerating
> these.  Perhaps that will give us a clue in suggesting a way that is not a
> simple work-around.

I'm not sure how to explain this better. This is the case:

I have a database with personal data, just names and dates of birth.
When I'm viewing a personal profile, there's a <a href> link called
"change data". The link contains an ID number for the person. This
links takes me to a .php page where I can edit the database post for
the person.

When I arrive at this page it accesses the database and takes out the
data for the person with the corresponding ID number. The result is a
small XML document that looks like this:
<changedata>
<PERSON ID='$id' FIRSTNAME='$firstname' LASTNAME='$lastname'
DAY='$day' MONTH='$month' YEAR='year'></PERSON>
</changedata>

There will only be one <PERSON> node in the document: the one whose
data I want to edit.

This document is then transformed with the help of a .xsl document.
The name data goes into html <form><input> elements, like this:

<form method="post" action="changeperson.php">
	<input type="text" name="firstname" value="{@firstname}"/>
	<input type="text" name="lastname" value="{@lastname}"/>

I then want the date of birth data to be used in three dropdown menus
in the form of <select><option> elements. One for DAY, one for MONTH,
one for YEAR. Like this:

In these menus the user are supposed to be able to change the date to
any date possible.

Therefore the DAY menu should consist of 31 <option> elements, numbered 1-31.

The MONTH menu should consist of 12 <option> elements, labeled
"January", "February" and so on.

The YEAR menu should consist of <option> elements for all possible
years of birth for living persons (circa 1900-2010 = 110 elements).

The attributes in my small XML document are to be used to determine
which <option> element in each one of the three menus that will get
the SELECTED="selected" attribute.

So if a person is born on 2 February 1901, the end result should look like
this:

	<select name="day">
		<option value="1">1</option>
		<option value="2" selected="selected">2</option>
		<option value="3">3</option>

		<!-- and so on...-->

		<option value="31">31</option>
	</select>

	<select name="month">
		<option value="January">January</option>
		<option value="February" selected="selected">January</option>
		<option value="March">March</option>

		<!-- and so on...-->

		<option value="December">December</option>
	</select>

	<select name="year">
		<option value="1900">1900</option>
		<option value="1901" selected="selected">1901</option>
		<option value="1902">1902</option>

		<!-- and so on...-->

		<option value="2010">2010</option>
	</select>

	<button type="submit">Edit data</button>
</form>

I don't think I have 37 elements. As far as understand, I only have
one element, that one <person> element. But maybe I don't understand
what you mean with "elements"?

And I'm not sure what you mean with "my basis for enumerating" the
elements. I just want to find a way to get the proper amount of
<option> tags (31 for the days menu, 12 for the month menu, 110 for
the year menu). David says that if I "control the source you could add
more elements". But what is the source? My database? My XML file? What
should I add more elements to? And how?


>If you want to
> make a menu, simply write it out ... why do you need an algorithmic
> approach?
>
> Again, the answer to that would help volunteers understand *why* you think
> you need an array, rather than just coming up with an analogous approach.

I think I need an algorithmic approach since I don't only want to
output the names of the month: I want to compare each of these names
to the content of my $month variable, and output one thing (an option
tag containing "selected='selected'" ) if they are the same and
another thing (an option tag without the "selected" attribute) if
they4re not the same.

Earlier, when I was generating html straight from php I used an array
to do this, so I guess that's why this seems the way to do it that
came to mind.

This is how that looked, if it's of any interest to you:

//create montharray
$monthName = array(1=> "January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December");

//create month selector
for($currentmonth = 1; $currentmonth <= 12; $currentmonth++)
{
$monthstring = $monthstring . "<option value='" .
$monthName[$currentmonth] . "'";
if($monthName[$currentmonth]==$month)
{
$monthstring = $monthstring . " SELECTED";
}
$monthstring = $monthstring . ">" . $monthName[$currentmonth] . "</option>";
}


Regards,
Martin Jackson

Current Thread