+<func:function name='f:matching-child'>
+ <xsl:param name='child' select='./copyright-holder' />
+ <xsl:param name='node' select='.' />
+ <xsl:param name='nodeset' select='$node/../*[name()=name($node)]' />
+
+ <func:result select='$nodeset[*[name()=name($child)]=$child]' />
+</func:function>
+
+<func:function name='f:attribution-order'>
+ <xsl:param name='a' />
+ <xsl:param name='b' />
+
+ <xsl:variable name='docmatch'
+ select='number($a/copyright-holder = /document/copyright-holder)
+ - number($b/copyright-holder = /document/copyright-holder)' />
+
+ <xsl:variable name='authmatch'
+ select='count(f:matching-child($a/copyright-holder, $a))
+ - count(f:matching-child($b/copyright-holder, $b))' />
+
+ <xsl:variable name='licmatch'
+ select='count(f:matching-child($a/license, $a))
+ - count(f:matching-child($b/license, $b))' />
+
+ <xsl:choose>
+ <xsl:when test='$docmatch'><func:result select='$docmatch' /></xsl:when>
+ <xsl:when test='$authmatch'><func:result select='$authmatch' /></xsl:when>
+ <xsl:when test='$licmatch'><func:result select='$licmatch' /></xsl:when>
+ <xsl:otherwise><func:result select='"nope"' /></xsl:otherwise>
+ </xsl:choose>
+</func:function>
+
+<xsl:template match='image/license'>
+ <xsl:text>, </xsl:text>
+ <a href='{uri}' rel='license'><xsl:value-of select='shortname' /></a>
+</xsl:template>
+
+<xsl:template match='image'>
+ <xsl:choose>
+ <xsl:when test='position() = 1'>, except </xsl:when>
+ <xsl:when test='position() = last()'> and </xsl:when>
+ <xsl:otherwise>, </xsl:otherwise>
+ </xsl:choose>
+ <a href='{uri}'><xsl:value-of select='title' /></a>
+ <xsl:text> © </xsl:text>
+ <xsl:value-of select='copyright' />
+ <xsl:apply-templates select='license' />
+</xsl:template>
+
+<xsl:template name='image-attribution'>
+ <xsl:variable name='images-fragment'>
+ <xsl:for-each select='/document/image'>
+ <xsl:sort select='number(copyright-holder = /document/copyright-holder)'
+ data-type='number' order='descending' />
+ <xsl:sort select='count(f:matching-child(copyright-holder))'
+ data-type='number' order='descending' />
+ <xsl:sort select='copyright-holder' order='descending' />
+ <xsl:sort select='count(f:matching-child(license))'
+ data-type='number' order='descending' />
+ <xsl:sort select='license/identifier' order='descending' />
+
+ <xsl:call-template name='notransform' />
+ </xsl:for-each>
+ </xsl:variable>
+ <xsl:variable name='images' select='exslt:node-set($images-fragment)/*' />
+
+ <xsl:variable name='abbrev-split'
+ select='count($images[copyright-holder = $images[1]/copyright-holder
+ and license/identifier = $images[1]/license/identifier])' />
+
+ <xsl:variable name='abbrev-years-fragment'>
+ <xsl:for-each select='$images[$abbrev-split >= position()]/copyright-year'>
+ <xsl:sort data-type='number' />
+ <copyright-year><xsl:value-of select='.' /></copyright-year>
+ </xsl:for-each>
+ </xsl:variable>
+ <xsl:variable name='abbrev-years'
+ select='exslt:node-set($abbrev-years-fragment)/*' />
+
+ <p>
+ <xsl:text>Images © </xsl:text>
+ <xsl:value-of select='$abbrev-years[1]' />
+ <xsl:if test='$abbrev-years[last()] != $abbrev-years[1]'>
+ <xsl:value-of select='concat("–", $abbrev-years[last()])' />
+ </xsl:if>
+ <xsl:value-of select='concat(" ", $images[1]/copyright-holder)' />
+ <xsl:apply-templates select='$images[1]/license' />
+ <xsl:apply-templates select='$images[position() > $abbrev-split]' />
+ <xsl:text>.</xsl:text>
+ </p>
+</xsl:template>
+