another round

I’ve started to move the logic from the earlier stylesheets into the
new approach. Where possible, I’ve moved logic into functions, though
there’s a bug in one. Ultimately, it’ll need to be moved back into
separate files, I think.

David, you there? Are you going to be able to work on this?===

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mods="http://www.loc.gov/mods/v3"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/docbook-ng"
xmlns:bib="http://xbiblio.sourceforge.net"
xmlns:exist="http://exist.sourceforge.net/NS/exist"
exclude-result-prefixes=“db mods xs exist bib”>

<xsl:output method=“xhtml” encoding=“utf-8” indent=“yes”/>
<xsl:strip-space elements="*"/>
<xsl:param name=“citeclass”>number</xsl:param>
<xsl:param name=“sort_order-bib”>cited</xsl:param>
<xsl:param name=“biboutfile”/>

<xsl:variable name=“citerefs” select="//db:biblioref/@linkend"/>

<xsl:variable name=“cite-position”>
bib:refs
bib:all
<xsl:for-each select="$citerefs">
<bib:ref position="{position()}" id="{generate-id()}“
key=”{.}"/>
</xsl:for-each>
</bib:all>
bib:unique
<xsl:for-each-group select="$citerefs" group-by=".">
<bib:ref position="{position()}" id="{generate-id()}“
key=”{.}"/>
</xsl:for-each-group>
</bib:unique>
</bib:refs>
</xsl:variable>

<xsl:variable name=“citekeys”>
xsl:text(</xsl:text>
<xsl:for-each-group select="$citerefs" group-by=".">
<xsl:if test=“position() > 1”>,%20</xsl:if>
xsl:text’</xsl:text>
<xsl:value-of select="."/>
xsl:text’</xsl:text>
</xsl:for-each-group>
xsl:text)</xsl:text>
</xsl:variable>

<xsl:variable name=“bibrecord"
select=“doc(concat(“http://localhost:8080/exist/servlet/db/biblio?
”,
”_query=declare%20namespace%20mods=%22http://www.loc.gov/mods/
v3%22;”, “%20for%20$citekey%20in%20”, $citekeys,

“%20return%20/mods:modsCollection/mods:
mods[@ID=$citekey]”))"/>

<xsl:variable name=“raw-biblist”>

<xsl:copy-of select="$bibrecord/exist:result/mods:mods"/>

</xsl:variable>

<xsl:variable name=“enhanced-biblist”>

xsl:choose
<xsl:when test="$sort_order-bib=‘citekey’">
<xsl:for-each
select="$raw-biblist/mods:modsCollection/mods:mods">
<xsl:sort select="@ID"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:when>
<xsl:when test="$sort_order-bib=‘cited’">
<xsl:for-each
select="$raw-biblist/mods:modsCollection/mods:mods">
<xsl:variable name=“id” select="@ID"/>
<xsl:variable name=“number”>
<xsl:value-of
select="$cite-position/bib:refs/bib:unique/bib:ref[@key=$id]/
@position"/>
</xsl:variable>
xsl:perform-sort
<xsl:sort select="$number"/>
</xsl:perform-sort>
<mods:mods ID="{$id}" position="{position()}">
<xsl:copy-of select=“mods:"/>
</mods:mods>
</xsl:for-each>
</xsl:when>
<xsl:when test="$sort_order-bib=‘author-year’">
<xsl:for-each select="$raw-biblist/mods:modsCollection">
<xsl:variable name=“bibref” select=“mods:mods”/>
<xsl:for-each-group select="$bibref"
group-by=“bib:grouping-key(.)”>
<xsl:sort select=“current-grouping-key()”/>
<xsl:for-each-group select=“current-group()“
group-by=“substring((mods:originInfo/mods:dateIssued,
mods:relatedItem/mods:originInfo/mods:dateIssued,
mods:relatedItem/mods:part/mods:date)[1],1,4)”>
<xsl:sort select=“current-grouping-key()”/>
<xsl:variable name=“year"
select=“current-grouping-key()”/>
<xsl:variable name=“first” as=“xs:boolean"
select=“position() = 1”/>
<xsl:variable name=“noname-substitute”>
xsl:choose
<xsl:when test=“not(mods:name) and
bib:refclass(.)=‘part-inSerial’”>
<xsl:value-of
select=“mods:relatedItem[@type=‘host’]/mods:titleInfo[not(@type)]/mods:
title”/>
</xsl:when>
<xsl:when test=“not(mods:name) and
not(bib:refclass(.)=‘part-inSerial’)”>
xsl:textAnonymous</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:for-each select=“current-group()”>
<mods:mods ID=”{@ID}”>

bib:refclass
<xsl:value-of select=“bib:refclass(.)”/>
</bib:refclass>
bib:reftype
<xsl:value-of select=“bib:reftype(.)”/>
</bib:reftype>

bib:position
<xsl:value-of select=“position()”/>
</bib:position>
bib:year
<xsl:value-of select=“current-grouping-key()”/>
</bib:year>

bib:noname-substitute
<xsl:value-of select=”$noname-substitute”/>
</bib:noname-substitute>
<xsl:if test=“last() > 1”>
<bib:key type=“year-suffix”>
<xsl:number value=“position()” format=“a”/>
</bib:key>
</xsl:if>
<xsl:copy-of select="mods:
”/>
</mods:mods>
</xsl:for-each>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:for-each>
</xsl:when>
</xsl:choose>

</xsl:variable>

<xsl:variable name=“formatted-biblist”>
<xsl:for-each
select="$enhanced-biblist/mods:modsCollection/mods:mods">
<xsl:variable name=“id” select="@ID"/>
<xsl:variable name=“number”>
<xsl:value-of
select="$cite-position/bib:refs/bib:unique/bib:ref[@key=$id]/
@position"/>
</xsl:variable>
xsl:choose
<xsl:when test="$citeclass=‘note’">


<xsl:value-of select="$number"/>
xsl:text. </xsl:text>
<xsl:apply-templates select=“mods:titleInfo”/>
<xsl:apply-templates select=“mods:name”/>


</xsl:when>
<xsl:when test="$citeclass=‘author-year’">


<xsl:apply-templates select=“mods:titleInfo”/>
<xsl:apply-templates select=“mods:name”/>


</xsl:when>
<xsl:when test="$citeclass=‘number’">


<xsl:value-of select="$number"/>
xsl:text. </xsl:text>
<xsl:apply-templates select=“mods:titleInfo”/>
<xsl:apply-templates select=“mods:name”/>


</xsl:when>
<xsl:when test="$citeclass=‘citekey’">


xsl:text[</xsl:text>
<xsl:value-of select="@ID"/>
xsl:text] </xsl:text>
<xsl:apply-templates select=“mods:titleInfo”/>
<xsl:apply-templates select=“mods:name”/>


</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:variable>

<xsl:template match="/">


Testing




xsl:apply-templates/


<xsl:copy-of select="$formatted-biblist"/>




<!–+ ==============================================================

write out raw MODS file if parameter is specified
±->
<xsl:if test="$biboutfile">
<xsl:result-document href="{$biboutfile}">
<xsl:copy-of select="$raw-biblist"/>
</xsl:result-document>
</xsl:if>

 <xsl:result-document href="enhanced-mods.xml">
   <xsl:copy-of select="$enhanced-biblist"/>
 </xsl:result-document>

</xsl:template>

<xsl:template match=“mods:titleInfo”>

<xsl:apply-templates select=“mods:title”/>
<xsl:apply-templates select=“mods:subTitle”/>

</xsl:template>
<xsl:template match=“mods:title”>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match=“mods:subTitle”>
xsl:text: </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match=“mods:name”>
xsl:text, </xsl:text>

<xsl:apply-templates select=“mods:namePart[@type=‘family’]”/>

</xsl:template>
<xsl:template match=“mods:namePart”>
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match=“db:article”>
<xsl:apply-templates select=“db:info”/>
<xsl:apply-templates select=“db:section”/>
</xsl:template>
<xsl:template match=“db:section”>
xsl:apply-templates/
</xsl:template>
<xsl:template match=“db:article/db:info”/>
<xsl:template match=“db:info/db:title”>


xsl:apply-templates/


</xsl:template>
<xsl:template match=“db:para”>


xsl:apply-templates/


</xsl:template>

<xsl:template match=“db:citation”>
xsl:text[</xsl:text>
<xsl:for-each select=“db:biblioref”>
<xsl:apply-templates select="."/>
<xsl:if test=“not(position() = last())”>
xsl:text, </xsl:text>
</xsl:if>
</xsl:for-each>
xsl:text]</xsl:text>
</xsl:template>
<xsl:template match=“db:biblioref”>
<xsl:variable name=“linkend” select="@linkend"/>
<xsl:variable name=“number”>
xsl:choose
<xsl:when test="$sort_order-bib=‘cited’">
<xsl:value-of
select="$cite-position/bib:refs/bib:unique/bib:ref[@key=$linkend]/
@position"/>
</xsl:when>
<xsl:when test="$sort_order-bib=‘citekey’">
<xsl:value-of select="$linkend"/>
</xsl:when>
xsl:otherwise
<xsl:value-of
select="$enhanced-biblist/mods:modsCollection/mods:mods[@ID=$linkend]/
@position"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:value-of select="$number"/>

</xsl:template>

<xsl:function name=“bib:number-condense”>
<xsl:param name=“begin”/>
<xsl:param name=“end”/>
xsl:choose
<xsl:when test="$begin castable as xs:integer">
<xsl:variable name=“begin” select="$begin" as=“xs:integer”/>
xsl:choose
<xsl:when test="$begin gt 100 and $begin mod 100 and $begin
idiv 100 eq $end idiv 100">
<xsl:value-of select="$end mod 100"/>
</xsl:when>
xsl:otherwise
<xsl:value-of select="$end"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
xsl:otherwise
<xsl:value-of select="$end"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

<xsl:function name=“bib:grouping-key” as=“xs:string”>
<xsl:param name=“bibref” as=“element(mods:mods)”/>
xsl:choose
<xsl:when test="$bibref/mods:name">
<xsl:value-of separator=";">
<xsl:for-each select="$bibref/mods:name">
<xsl:value-of select=“string-join((mods:namePart[@type =
‘family’]| mods:namePart[not(@type)],
mods:namePart[@type = ‘given’]), ‘,’)”/>
</xsl:for-each>
</xsl:value-of>
</xsl:when>
xsl:otherwise
xsl:choose
<xsl:when
test="$bibref/mods:relatedItem[@type=‘host’]/mods:originInfo/mods:
issuance = ‘continuing’">
<xsl:for-each select="$bibref">
<xsl:value-of
select=“mods:relatedItem[@type=‘host’]/mods:titleInfo[not(@type)]/mods:
title”/>
</xsl:for-each>
</xsl:when>
xsl:otherwise
xsl:textAnonymous</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

<xsl:function name=“bib:refclass” as=“xs:string”>
<xsl:param name=“bibref” as=“element(mods:mods)?”/>
xsl:choose
<xsl:when test="$bibref/mods:relatedItem[@type=‘host’]">
<xsl:variable name=“issuance"
select=”$bibref/mods:relatedItem/mods:originInfo/mods:issuance"/>
xsl:choose
<xsl:when
test="$issuance=‘monographic’">part-inMonograph</xsl:when>
xsl:otherwisepart-inSerial</xsl:otherwise>
</xsl:choose>
</xsl:when>
xsl:otherwisemonograph</xsl:otherwise>
</xsl:choose>
</xsl:function>

<xsl:function name=“bib:reftype” as=“xs:string”>
<xsl:param name=“bibref” as=“element(mods:mods)?”/>
xsl:choose
<xsl:when test="$bibref/mods:relatedItem/@type = ‘host’">
<xsl:variable name=“issuance"
select=”$bibref/mods:relatedItem/mods:originInfo/mods:issuance"/>
xsl:choose
<xsl:when test=“bib:refclass($bibref) = ‘part-inSerial’”>
xsl:textarticle</xsl:text>

</xsl:when>
<xsl:when test=“bib:refclass($bibref) = ‘part-inMonograph’”>
xsl:choose
<xsl:when test="$bibref/mods:genre">
<xsl:value-of select="$bibref/mods:genre"/>
</xsl:when>
xsl:otherwise
xsl:textchapter</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
xsl:otherwise
<xsl:value-of select="$bibref/mods:genre"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
xsl:otherwisebook</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>

Bruce