XQuery HTML array input

337 views Asked by At

I am new to XQuery 3.0 and trying to write a project.

I am sending a post request containing song[] input element.

However, when I try to use request:get-parameter('song[]', ''), it gives me a result as below:

Value 1 Value 2 Value 3

Anyone knows how to get these variables separately?

Edit: Actual contents of the request parameter being POSTed to eXist is provided.

HTML Form:

<form action="post.xq" method="POST">
    <input name="song[]" type="text" value="Song 1" />
    <input name="song[]" type="text" value="Song 2" />
    <input name="song[]" type="text" value="Song 3" />
</form>

post.xq:

let $result := request:get-parameter('song[]', '')
return
    <result>
        {$result}
    </result>

result:

<result>Song 1 Song 2 Song 3</result>
1

There are 1 answers

1
Joe Wicentowski On BEST ANSWER

Yours songs are all there, as a sequence of 3 strings. The $result variable is a sequence of 3 strings returned by the request:get-parameter() function. To access them individually, you can use a positional predicate, e.g.:

<result>{$result[1]}</result>

To output them all, you could use a FLWOR expression, e.g.:

for $song at $n in $result
return
    <song n="{$n}">{$song}</song>

Or for simpler uses (assuming XQuery 3.0 is available), the simple map operator, e.g.:

$result ! <song>{.}</song>

Or for returning the values as a string, separated by, say, a semi-colon, e.g.:

string-join($result, "; ")

The reason you're seeing them as a single space-delimited string (Song 1 Song 2 Song 3) is that the 3 items in the sequence are being normalized, and the default item separator is a single space. For more on sequence normalization and the item separator, see https://www.w3.org/TR/xslt-xquery-serialization-31/#serdm.