Mojtaba Nabizada
Mojtaba Nabizada

Reputation: 13

concatenation of one value with multiple values from another xquery

Hej. I am new at XML-query and have some problem finding a way to combine the result of two xquery into one.

Consider the XML:

<programs>
    <program id="488">
        <editor>Anna</editor>
        <channel>132</channel>
        <category>5</category>
    </program>
    <program id="178">
        <editor>Olle</editor>
        <channel>132</channel>
        <category>68</category>
    </program>
    <program id="179">
        <editor>Olle</editor>
        <channel>132</channel>
        <category>10</category>
    </program>
</programs>

I want to extract list of editors along with the categories they have worked on which would be like this:

    <li>Anna 5 </li>
    <li>Olle 68 10</li>

Here is the xquery code I am using

                    let $editors :=
                        for $d in $sr/sr/programs/program
                        where $d/channel = "132"
                        return $d/editor
                    let $cat :=
                        for $a in $sr/sr/programs/program
                        where $a/editor = data($editors)
                        return concat($a/editor ,' ', $a/category)
                    for $result in distinct-values($cat)
                    return <li>{string($result)}</li>

Appreciate all the helps!

Upvotes: 1

Views: 259

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66783

I would iterate over the distinct list of editors, obtained from an XPath with a predicate to restrict to those with channel equal to 132, and then retrieve a distinct list of categories for those editors with a similar XPath with predicate filter, and then return the HTML with those values:

for $editor in distinct-values($sr/sr/programs/program[channel eq 132]/editor)
let $categories := 
  distinct-values($sr/sr/programs/program[channel eq 132 and editor eq $editor]/category)
return <li>{$editor, $categories}</li>

Upvotes: 0

Related Questions