Search code examples
orbeon

How to count a dataset in Orbeon Forms?


I have a HTTP Service that returns an JSON response that Orbeon converts to XML like this:

<json type="object">
    <data type="object">
        <timeSlots type="array">
            <_ type="object">
                <label>9:00AM to 9:30AM</label>
                <value>1</value>
            </_>
            <_ type="object">
                <label>10:00AM to 10:30AM</label>
                <value>2</value>
            </_>
        </timeSlots>
    </data>
    <is_success type="boolean">true</is_success>
</json>

I've set up an Orbeon "action" that uses Save to Dataset named "time-slots":

Screen shot showing Save to Dataset

But, I can't figure out how to count the dataset in a control. I've tried adding the following to a control's calculated value:

count(fr:dataset('time-slots')/json/data/timeSlots/_)

and

count(fr:dataset('time-slots')/json/data/timeSlots)

I can't figure out the syntax. Any hints or ideas would be greatly appreciated!


Solution

  • The fr:dataset() function returns the root element instead of the document node, just like doc(), so you'll want to skip the json in your XPath. If you want to count the number of time slots, based on the XML you quoted, the following should do the trick:

    count(fr:dataset('time-slots')/data/timeSlots/_)