Use of different parameters in a select box with xquery

Viewed 46

I'm new to xQuery (with existdb), trying to fill in select box options with values from 1 node (Full) of an xml file and setting another node (corto) to be the parameter sent for further queries, unfortunately unsuccessful till now...

The structure of the xml source to the select control is:

<?xml version="1.0" encoding="UTF-8"?>
<canales>
<Canal>
    <corto>24H</corto>
    <Full>24H</Full>
</Canal>
<Canal>
<corto>ALJZ</corto>
<Full>AL JAZEERA</Full>
</Canal>
....

No problems when sharing just 1 parameter for both presentation on screen and to pass it to other functions:

declare function app:Lista_canales($node as node()*, $model as map(*)){
let $nombre_canal := (doc(concat($config:data-root, '/','canales.xml')))
let $control :=
    <select name="n_canal" class="form-control">
        <option value ="">Select channel</option>
        {for $canall in $nombre_canal//corto
        return <option value="{$canall}">{$canall}</option>
        }

    </select>
return
    templates:form-control($control, $model)

};

Snapshot shows the values of select box and the table showing the subsequent query done with the DISN tag

Problems appear if I try to use the (more meaningful) Full name for the select list while keeping the other to pass it to other queries. This code does not do what I want:

declare function app:Lista_canales($node as node()*, $model as map(*)){
let $nombre_canal := (doc(concat($config:data-root, '/','canales.xml')))
let $control :=
    <select name="n_canal" class="form-control">
        <option value ="">Select channel</option>
        {for $canall in $nombre_canal
        return <option value="{$canall//corto}">{$canall//Full}</option>
        }
    </select>
return
    templates:form-control($control, $model)

};

Any advice/hints will be welcomed.

1 Answers

Your problem emerges from the change you made in your 2nd sample's FLWOR expression - to the for clause.

In the 1st sample, you iterated over $nombre_canal//corto - meaning that you would return one <option> element for every <corto> element in the source.

In the 2nd sample, you switched to iterating over just $nombre_canal - meaning that you do not really iterate at all, since $nombre_canal is the full canales.xml document.

To fix the 2nd sample, you should restore the for clause from the 1st sample. Here's a minimal example demonstrating the approach:

xquery version "3.1";

let $nombre_canal := 
    <canales>
        <Canal>
            <corto>24H</corto>
            <Full>24H</Full>
        </Canal>
        <Canal>
            <corto>ALJZ</corto>
            <Full>AL JAZEERA</Full>
        </Canal>
    </canales>
for $canall in $nombre_canal/Canal
return 
    <option value="{$canall/corto}">{$canall/Full/string()}</option>

This returns two <option> elements (one per <Canal> from the source):

<option value="24H">24H</option>
<option value="ALJZ">AL JAZEERA</option>
Related