-1

I created xquery function which returns a table:

declare function local:table($collection as xs:string*, $interface as xs:string?, $date as xs:string?) as node() {
<table border="1">
    <thead>
        <tr>
            <th>Inteface Name</th>
            <th>Test Date</th>
            <th>Test Result</th>
            <th>Report Link</th>
        </tr>
    </thead>
    <tbody>
    {   
        for $child in xmldb:get-child-resources($collection)
        let $doc := fn:doc(fn:concat($collection, '/', $child))
        where (fn:ends-with($child, '.xml')) 
                and (($doc//*:interfaceName/text() eq $interface) or empty($interface))
                and (($doc//*:reportDate/text() eq $date) or empty($date))
        order by $doc//*:reportDate/text() descending      
        return
        <tr>
            <td>
                {$doc//*:interfaceName/text()}   
            </td>
            <td>
                {$doc//*:reportDate/text()}  
            </td>
            <td>
                {$doc//*:testResult/text()}  
            </td>
            <td>
                <li>
                <!--<a href="{document-uri(fn:doc(fn:concat($collection, '/', $child)))}">{$child}</a> -->
                <a href="http://localhost:8080/exist/rest/db/junitReports/Report1.xml">  {$child}</a> 
                </li>
            </td>
        </tr>
    }
    </tbody>
</table>

I also added a few input controls on the page. One of them looks like:

  <InterfaceName constraint="true" readonly="false" required="false" relevant="true">
       <value>test</value>
  </InterfaceName>


<xf:bind nodeset="InterfaceName">
<xf:bind nodeset="value" type="string"/>

<xf:input id="InterfaceName" ref="InterfaceName/value" incremental="true">
        <xf:label></xf:label>
        <xf:hint>xxxxxYYYZZZ</xf:hint>
        <xf:help>Enter interface name</xf:help>
        <xf:alert>Enter interface name</xf:alert>
</xf:input>

I also added a button to the webpage:

<trigger1 constraint="true" readonly="false" required="false" relevant="true">
    <value></value>
</trigger1>

<xf:submission id="s-send"
        replace="instance"
        resource="echo:test"
        method="get">
 </xf:submission>

<div>
    <xf:trigger id="trigger1" ref="trigger1/value" incremental="true">
        <xf:label>Filter output</xf:label>
        <xf:hint>a Hint for this control</xf:hint>
        <xf:help>help for trigger1</xf:help>
        <xf:send submission="s-send"/>
    </xf:trigger>
</div>

On this button click I need to somehow pass parameters form those input controls to xquery function and return the table to the webpage. Entire webpage is of type xQuery (it builds html) and run with eXist-db. Could you help me with this, please?

Thank you.

user6419217
  • 59
  • 10

1 Answers1

1

You'll need four elements to achieve your goal:

  1. An <xf:instance id="result" > to store the result of calling your xquery. Make sure to add an id attribute to identify the instance further.

  2. An <xf:submission> to call your xquery an store the result in the instance. This is the submission you'll call in the <xf:send> and may look like this:

  <xf:submission id="s-send" method="get" replace="instance" instance="result">
   <xf:resource value="concat('myxquery.xq?interface=',InterfaceName/value)"/>
  </xf:submission>

Note that the concat function is used to build the xquery url, including parameters.

  1. An <xf:output value="instance('result')" mediatype="application/xhtml+xml"> to show the contents of the result instance. The mediatype="application/xhtml+xml" attribute is needed to display the html table.

In the server side, you can't call an xquery function directly, you need to write an xquery (myquery.xq) that calls the function and extracts the parameters from the URL.

Take a look to this sample https://en.wikibooks.org/wiki/XQuery/Getting_URL_Parameters

Bill Velasquez
  • 875
  • 4
  • 9
  • Hi Bill. Thanks a lot for your answer. I already "invented bicycle" by myself and it works, but your solution looks better and is very similar to what I wanted to get in the beginning, so, I tried it. Result: the function returns filled table, but on the form I get all output as one concatenated string: "Inteface Name Test Date Test Result Report Link SlenovNINEIN 2016-06-01 failure Report2.xml RwillgEIFKFX 2016-06-01 success Report1.xml". – user6419217 Jun 07 '16 at 16:40
  • Do you know why it looks like this? I thought it would "replace" my response instance and paste filled table there so that it would be shown as a table on the form. Thanks a lot for showing me how to call a query with parameters from input control. – user6419217 Jun 07 '16 at 16:43
  • Maybe your query returns a content-type different from application/xhtml+xml. Use the developer tools of your browser (F12) to check the network request that loads the table. – Bill Velasquez Jun 07 '16 at 21:20
  • Now I have an issue passing parameters from input controls to url. I am doing concatenation like this in submission: action="{concat('/exist/rest/db/xquery/returnTable.xq?interface=',InterfaceName)}". I also changed my input controls- removed element everywhere (it did not work with IntefanceName/value in concatenation neither) – user6419217 Jun 08 '16 at 09:36
  • I tried to replace action attribute in submission with extra element: . But I still get the same uri: "resource-uri":"http://localhost:8080/exist/rest/db/xquery/returnTable.xq?interface=" – user6419217 Jun 08 '16 at 11:28
  • XxPath evaluation with { } doesn't work in the submission element (with xsltforms processor). The solution is to use the xf:resource element with an XPath expression in the value attribute. Maybe the XPath InterfaceName is returning an empty element, try using absolute XPath like /InterfaceName/Value – Bill Velasquez Jun 09 '16 at 15:09
  • Hi Bill. Do you know how can I change content-type in response message? My xquery returns plain text instead of xml in response message. And I think that changing content-type from text/javascript to application/xhtml+xml should solve the issue. – user6419217 Jun 27 '16 at 19:41
  • declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes"; Take a look to the full XQuery Crib Sheet for more options: https://en.wikibooks.org/wiki/XQuery/eXist_Crib_sheet#output_XHTML_document – Bill Velasquez Jun 29 '16 at 03:15