Karthik Karthik - 5 months ago 11
HTML Question

Dynamically assign values to select tag using Xquery

I tried drop down using

<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>


It is working well. Instead of hard-coded values , I would like to dynamically assign values from another
xquery/xpath


Example

/leader/country/text()


So I will have list of countries from the above code that should go to select tag of HTML

Answer

Try an XQuery FLWOR expression like:

<select>{
  for $country in /leader/country/string(.)
  return <option value="{$country}">{$country}</option>
}</select>

While XPaths over the database are useful for getting started, it's good to learn the Search API or cts:search() to query with good performance over large datasets.

Hoping that helps,