Skip to content

Instantly share code, notes, and snippets.

View emchateau's full-sized avatar

Emmanuel Château-Dutier emchateau

View GitHub Profile
@emchateau
emchateau / filter01.xq
Created February 19, 2021 20:50
Filter map by key value
map:keys($map)[exists($map(.)[?prop2 = 17])]
@emchateau
emchateau / sequenceComparison.xq
Last active November 27, 2020 02:26
XQuery sequence comparison
for $a in (1,2,3,5)
where $a = (1,3,2,3)
group by $k := $a
return $a
@emchateau
emchateau / compareOrderOfSequences.xq
Created July 26, 2020 13:28
Compare order of two sequences
(: https://codereview.stackexchange.com/questions/214571/xquery-compare-order-of-two-sequences :)
declare function local:testOrder2($baseSequence, $testSequence) {
let $positions := $testSequence ! index-of($baseSequence, .)
return min(for $pos at $index in tail($positions) return $pos - $positions[$index]) ge 0
};
@emchateau
emchateau / getDistinctMaps.xq
Created July 12, 2020 20:53
Deep comparison of maps in XQuery
(: @see https://stackoverflow.com/questions/62844910/how-to-deep-compare-maps-to-filter-distinct-items-in-xquery :)
declare function local:getDistinctMaps($maps as map(*)*) as map(*)* {
fold-left(
$maps,
(),
function($distinct-maps, $new-map) {
if (some $map in $distinct-maps satisfies deep-equal($map, $new-map))
then $distinct-maps else ($distinct-maps, $new-map)
}
)
<xsl:template match="Person">
<xsl:for-each-group select="Student/Info" group-by="@Country">
<country name="{current-grouping-key()}">
</country>
</xsl:for-each-group>
</xsl:template>
@emchateau
emchateau / xpathInJavascript
Created July 1, 2020 04:45
XPath in Javascript
var element = document.evaluate( '//body//form/p/textarea' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
if (element != null) {
element.value = '...';
}
@emchateau
emchateau / books.xml
Last active July 1, 2020 00:54
Remove duplicates
<books>
<book>
<title>XML in 24 hours</title>
<author>Some Guy</author>
</book>
<book>
<title>Food in Seattle</title>
<author>Some Guy2</author>
</book>
<book>
@emchateau
emchateau / modernXML.md
Last active February 7, 2021 03:25
Modern XML usefull resources

title: Modern XML usefull resources

author: @emchateau, @sardinecan

since: 2020-06-24

description: Selected XML resources from the ANR Experts projet for the NA+DAH Getty advanced workshop.

@emchateau
emchateau / getExpertsCollaborations.xqm
Created June 16, 2020 14:26
Network function to get parisian experts collaborations (ANR Experts)
xquery version "3.1";
declare namespace xpr = "xpr";
declare default element namespace "eac";
declare function local:pairsCombinations($seq) {
if (fn:count($seq) = 1) then
map{
'source' : $seq
}
else for $i at $pos in $seq
@emchateau
emchateau / pairsCombinations.xq
Last active June 12, 2020 21:46
pairs combinations in a sequence with XQuery
xquery version "3.1" ;
(:~
: Pairs combinations in a sequence
: Unique pairs in a sequence (where order is irrelevant)
:)
let $seq := ("a", "b", "b", "d")
for $i at $pos in $seq
for $j in fn:subsequence($seq, $pos+1, fn:count($seq))
return <result>{$i, $j}</result>