Charlie Ou Yang
Charlie Ou Yang

Reputation: 641

how to pass parameters into SPARQL Web Pages query

I am very new to SWP and I'm very confused as to passing parameters that I get from the URL into a SPARQL query to make it more dynamic. This is the SWP file I'm working with:

<!-- Navigate to http://localhost:8083/tbl/tutorial.swp?test=Mate in a web browser -->
<ui:setContext
        xmlns:kennedys="http://topbraid.org/examples/kennedys#"
        ui:queryGraph="&lt;http://topbraid.org/examples/kennedys&gt;"
        let:query = "{= ui:param('test', xsd:string)}"
        let:helloThere = "{= xsd:string('hello')}"
    >
    <h1>G'day, {= ?helloThere}!</h1>


    <table>
        <thead>
            <tr>
                <th data-field="propertyID">
                    Property ID
                </th>
                <th data-field="property">
                    Property
                </th>
                <th data-field="valueID">
                    Value ID
                </th>
                <th data-field="value">
                    Value
                </th>
            </tr>
        </thead>

        <ui:forEach ui:resultSet="{#
                SELECT * 
                WHERE {
                    &lt;http://topbraid.org/examples/kennedys#AlfredTucker&gt; ?property ?value .
                }
                }">
            <tr>
                <td>{= xsd:string(?property) }</td>
                <td>{= ui:label(?property) }</td>
                <td>{= xsd:string(?value) }</td>
                <td>{= ui:label(?value) }</td>
            </tr>

        </ui:forEach>

    </table>


</ui:setContext>

This all works fine and dandy, but I'm trying to pass in a variable into the SPARQL query. I tried something like this:

<ui:forEach ui:resultSet="{#
            SELECT * 
            WHERE {
                &lt;http://topbraid.org/examples/kennedys#" + {= ?query} + "&gt; ?property ?value .
            }
            }">
        <tr>
            <td>{= xsd:string(?property) }</td>
            <td>{= ui:label(?property) }</td>
            <td>{= xsd:string(?value) }</td>
            <td>{= ui:label(?value) }</td>
        </tr>

    </ui:forEach>

Unfortunately, that doesn't work. I would like to know how to pass variables into the SPARQL query.

Upvotes: 3

Views: 695

Answers (1)

Charlie Ou Yang
Charlie Ou Yang

Reputation: 641

I contacted TopBraid support and they solved my problem! Here's the solution that worked:

<!-- Navigate to http://localhost:8083/tbl/dummy.swp?name=AlfredTucker in a web browser -->
<ui:setContext
        xmlns:kennedys="http://topbraid.org/examples/kennedys#"
        ui:queryGraph="&lt;http://topbraid.org/examples/kennedys&gt;"
        let:name="{= ui:param('name', xsd:string) }">

    <data let:personURI="{= IRI(CONCAT('http://topbraid.org/examples/kennedys#', ?name))}">
        <ui:forEach ui:resultSet="{#
                SELECT * 
                WHERE {
                    ?personURI ?property ?value .
                } ORDER BY ui:label(?person) }">
            <entry>
                <property>{= ui:label(?property) }</property>
                <value>{= ui:label(?value)}</value>
            </entry>
        </ui:forEach>
    </data>

</ui:setContext>

Upvotes: 3

Related Questions