Skip to content

Commit c4cef09

Browse files
committed
Replacing QueryBuilder::filter with the new ldh:add-regex-filter query transform mode
1 parent e5cb941 commit c4cef09

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/client.xsl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ WHERE
13021302
<!-- trigger typeahead in the search bar -->
13031303

13041304
<xsl:template match="input[@id = 'uri']" mode="ixsl:onkeyup" priority="1">
1305+
<xsl:param name="var-name" select="'label'" as="xs:string"/>
13051306
<xsl:param name="text" select="ixsl:get(., 'value')" as="xs:string?"/>
13061307
<xsl:param name="menu" select="following-sibling::ul" as="element()"/>
13071308
<xsl:param name="delay" select="400" as="xs:integer"/>
@@ -1353,14 +1354,40 @@ WHERE
13531354
<xsl:with-param name="menu" select="$menu"/>
13541355
</xsl:call-template>
13551356
</xsl:when>
1356-
<!-- if the input is not a URI, execute a keyword search with SPARQL regex() -->
1357+
<!-- if the input is not a URI, execute a keyword search with SPARQL regex() -->
13571358
<xsl:when test="not(starts-with(ixsl:get(., 'value'), 'http://')) and not(starts-with(ixsl:get(., 'value'), 'https://'))">
1358-
<!-- TO-DO: refactor query building using XSLT -->
13591359
<xsl:variable name="select-builder" select="ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromString', [ $select-string ])"/>
1360-
<!-- pseudo JS code: SPARQLBuilder.SelectBuilder.fromString(select-builder).wherePattern(SPARQLBuilder.QueryBuilder.filter(SPARQLBuilder.QueryBuilder.regex(QueryBuilder.var("label"), QueryBuilder.term(QueryBuilder.str($text))))) -->
1360+
<xsl:variable name="select-json-string" select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'stringify', [ ixsl:call($select-builder, 'build', []) ])" as="xs:string"/>
1361+
<xsl:variable name="select-xml" select="json-to-xml($select-json-string)" as="document-node()"/>
1362+
<!-- append FILTER(regex()) -->
1363+
<xsl:variable name="select-xml" as="document-node()">
1364+
<xsl:document>
1365+
<xsl:apply-templates select="$select-xml" mode="ldh:add-regex-filter">
1366+
<xsl:with-param name="var-name" select="$var-name" tunnel="yes"/>
1367+
<xsl:with-param name="pattern" select="$text" tunnel="yes"/>
1368+
<xsl:with-param name="flags" select="'iq'" tunnel="yes"/> <!-- case insensitive, ignore meta-characters: https://www.w3.org/TR/xpath-functions-31/#flags -->
1369+
</xsl:apply-templates>
1370+
</xsl:document>
1371+
</xsl:variable>
1372+
<!-- set LIMIT -->
1373+
<xsl:variable name="select-xml" as="document-node()">
1374+
<xsl:document>
1375+
<xsl:apply-templates select="$select-xml" mode="ldh:replace-limit"/>
1376+
</xsl:document>
1377+
</xsl:variable>
1378+
<!-- wrap SELECT into a DESCRIBE -->
1379+
<xsl:variable name="query-xml" as="element()">
1380+
<xsl:apply-templates select="$select-xml" mode="ldh:wrap-describe"/>
1381+
</xsl:variable>
1382+
<xsl:variable name="query-json-string" select="xml-to-json($query-xml)" as="xs:string"/>
1383+
<xsl:variable name="query-json" select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'parse', [ $query-json-string ])"/>
1384+
<xsl:variable name="query-string" select="ixsl:call(ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromQuery', [ $query-json ]), 'toString', [])" as="xs:string"/>
1385+
<!--
1386+
<xsl:variable name="select-builder" select="ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromString', [ $select-string ])"/>
13611387
<xsl:variable name="select-builder" select="ixsl:call($select-builder, 'wherePattern', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'filter', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'regex', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'str', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'var', [ 'label' ]) ]), ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'term', [ ac:escape-regex($text) ]), true() ] ) ] ) ])"/>
13621388
<xsl:variable name="select-string" select="ixsl:call($select-builder, 'toString', [])" as="xs:string"/>
13631389
<xsl:variable name="query-string" select="ac:build-describe($select-string, $limit, (), (), true())" as="xs:string"/>
1390+
-->
13641391
<xsl:variable name="service-uri" select="xs:anyURI(ixsl:get(id('search-service'), 'value'))" as="xs:anyURI?"/>
13651392
<xsl:variable name="service" select="key('resources', $service-uri, ixsl:get(ixsl:window(), 'LinkedDataHub.apps'))" as="element()?"/>
13661393
<xsl:variable name="endpoint" select="($service/sd:endpoint/@rdf:resource/xs:anyURI(.), resolve-uri('sparql', $ldt:base))[1]" as="xs:anyURI"/>

src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/query-transforms.xsl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,4 +696,40 @@ extension-element-prefixes="ixsl"
696696
</xsl:copy>
697697
</xsl:template>
698698

699+
<!-- FILTER(regex()) -->
700+
701+
<!-- identity transform -->
702+
<xsl:template match="@* | node()" mode="ldh:add-regex-filter">
703+
<xsl:copy>
704+
<xsl:apply-templates select="@* | node()" mode="#current"/>
705+
</xsl:copy>
706+
</xsl:template>
707+
708+
<!-- append FILTER(regex()) to WHERE -->
709+
<xsl:template match="json:array[@key = 'where']" mode="ldh:add-regex-filter" priority="1">
710+
<xsl:param name="var-name" as="xs:string" tunnel="yes"/>
711+
<xsl:param name="pattern" as="xs:string" tunnel="yes"/>
712+
<xsl:param name="flags" as="xs:string?" tunnel="yes"/>
713+
714+
<xsl:copy>
715+
<xsl:apply-templates select="@* | node()" mode="#current"/>
716+
717+
<json:map>
718+
<json:string key="type">filter</json:string>
719+
<json:map key="expression">
720+
<json:string key="type">operation</json:string>
721+
<json:string key="operator">regex</json:string>
722+
<json:array key="args">
723+
<json:string>?<xsl:value-of select="$var-name"/></json:string>
724+
<json:string>"<xsl:value-of select="$pattern"/>"</json:string>
725+
726+
<xsl:if test="$flags">
727+
<json:string>"<xsl:value-of select="$flags"/>"</json:string>
728+
</xsl:if>
729+
</json:array>
730+
</json:map>
731+
</json:map>
732+
</xsl:copy>
733+
</xsl:template>
734+
699735
</xsl:stylesheet>

0 commit comments

Comments
 (0)