Skip to content

Instantly share code, notes, and snippets.

@pwin
pwin / text-to-lines.xquery
Last active December 14, 2015 17:19
tokenise lines on newline
declare function core:text-to-lines($text) {
let $lines := tokenize($text, '\n')
return $lines
};
@pwin
pwin / get-filename.xquery
Last active December 14, 2015 17:19
return filename part of filepath
declare function core:get-filename($uri as xs:string) {
fn:tokenize($uri, "/")[last()]
};
@pwin
pwin / gist:5142371
Created March 12, 2013 12:03
in an XForm submission - adding a variable value to a querystring to overcome caching etc
<xf:submission id="s2" method="get" replace="instance" targetref="instance('list')">
<xf:resource value="concat('/exist/rest/db/apps/stats21/views/report-view.xquery?t=',substring(string(digest( string( random( true ) ), 'MD5', 'hex' )), 1, 8))"/>
</xf:submission>
@pwin
pwin / select-box unique.html
Created March 12, 2013 12:06
In an XForms select1 itemset, ensure that the nodeset only contains unique values
<xf:select1 ref="instance('policeforce')/selectedforce" selection="closed" >
<xf:itemset nodeset="instance('report-transformed')/body//tr[not(td[3] = preceding-sibling::tr/td[3])]">
<xf:label ref="td[3]" />
<xf:value ref="td[3]" />
</xf:itemset>
</xf:select1>
@pwin
pwin / IATI-Activities work in progress.xquery
Last active December 17, 2015 00:59
Work in progress on IATI Activities XForm
xquery version "3.0";
declare option exist:serialize "method=xhtml media-type=text/xml indent=yes process-xsl-pi=no";
let $attribute := request:set-attribute("betterform.filter.ignoreResponseBody", "true")
(: look at https://github.com/orbeon/eXist-1.4.x/blob/master/webapp/xforms/tasks/todo-list.xml :)
let $form := <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
@pwin
pwin / scraper.xquery
Last active December 20, 2015 14:19
XQuery site scraper. Using BaseX
xquery version "3.0";
declare namespace xhtml="http://www.w3.org/1999/xhtml";
declare function local:get-page($url){
http:send-request(<http:request method='get' headers='false' status-only='false'/>, $url)
};
declare function local:get-details($n as node()){
for $h in $n/xhtml:div[@class="hit detailed"]
let $title := $h//xhtml:p[@class="summary"]
@pwin
pwin / IATI-select-1.xquery
Last active December 26, 2015 08:49
some selections from IATI files - suitable for creating dashboards etc
xquery version "3.0";
declare namespace functx = "http://www.functx.com";
declare function functx:sort
( $seq as item()* ) as item()* {
for $item in $seq
order by $item
return $item
} ;
@pwin
pwin / SG_scraper.py
Last active December 29, 2015 07:39
Scrapy spider for scotland.gov.uk
import uuid
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import Rule, CrawlSpider
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from SG_2013_10_29.items import Sg20131029Item
from pybloom import BloomFilter
bloom = BloomFilter(capacity=500000, error_rate=0.000001)
@pwin
pwin / utilityValueByMonth
Last active December 31, 2015 00:49
SPARQL query to get the total utility consumption per month for a building
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix gol: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix qb: <http://purl.org/linked-data/cube#>
prefix buildingCode: <http://cofog01.data.scotland.gov.uk/id/facility/>
prefix fn: <http://www.w3.org/2005/xpath-functions#>
prefix util: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
select ?month (sum(?o) as ?val )
@pwin
pwin / getSites.sparql
Last active December 31, 2015 02:19
SPARQL to get sites
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX v: <http://www.w3.org/2006/vcard/ns#>
SELECT * WHERE {
SERVICE <http://cofog01.data.scotland.gov.uk/sparql> {
select distinct ?fn ?code {
graph ?g {