sort-order

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

sort-order

Bruce D'Arcus
I bought a license of Oxygen in the hopes it makes writing stylesheets  
a bit easier.  Anyway, I managed to figure out how to handle numbered  
citations.  Here's a start.

First, a test doc:

<?xml version="1.0" encoding="utf-8"?>
<article xmlns="http://docbook.org/docbook-ng">
   <info>
     <title>Test</title>
   </info>
   <section>
     <info>
       <title>Introduction</title>
     </info>
     <para>A citation with page number detail: <citation>
         <biblioref linkend="Veer1996a" units="page" begin="23"  
end="24"/>
       </citation>.</para>
     <para>Some more citations: <citation>
         <biblioref linkend="Tilly2000a"/>
         <biblioref linkend="Thrift1990a"/>
         <biblioref linkend="TimesP2001a"/>
       </citation>. A citation <footnote>
         <para>... in a footnote <citation>
             <biblioref linkend="Tilly2000a" begin="234"/>
           </citation>
         </para>
       </footnote>.</para>
   </section>
   <bibliography/>
</article>

... and the stylesheet:

===
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
version="2.0"
   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://www.example.org/bib"
   xmlns:exist="http://exist.sourceforge.net/NS/exist"  
exclude-result-prefixes="db mods exist">
   <xsl:output method="xhtml" encoding="utf-8" indent="yes"/>
   <xsl:strip-space elements="*"/>
   <!--
This demonstrates basic ideas to rework the code. I think
we additionally need top-level parameters for:

        - output format (xhtml, WordML, OOo, etc.)
        - input format?
        - whether to save bibcollection out to a file (and use as fallback)
-->
   <xsl:param name="citeclass">note</xsl:param>
   <xsl:param name="sort_order-bib">cited</xsl:param>
   <xsl:param name="biboutfile"/>
   <!-- grab all the citation pointers -->
   <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>
   <!-- construct a list of unique references to pass to a query -->
   <xsl:variable name="citekeys">
     <xsl:text>(</xsl:text>
     <xsl:for-each-group select="$citerefs" group-by=".">
       <xsl:if test="position() &gt; 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>
   <!--
take list of references, and grab them from a database over
http; in this case it's an XQuery to eXist,but it should be
parameterized for flexibility
-->
   <xsl:variable name="bibrecord"  
select="doc(concat(&quot;http://localhost:8080/exist/servlet/db/biblio?
&quot;,    
&quot;_query=declare%20namespace%20mods=%22http://www.loc.gov/mods/
v3%22;&quot;,   &quot;%20for%20$citekey%20in%20&quot;,       $citekeys,  
   
&quot;%20return%20/mods:modsCollection/mods:
mods[@ID=$citekey]&quot;))"/>
   <!--
create raw bib collection; in a real implementation, this might be where
enhancement (grouping, sorting, adding content for processing, etc.)  
would
take place
-->
   <xsl:variable name="raw-biblist">
     <modsCollection xmlns="http://www.loc.gov/mods/v3">
       <xsl:copy-of select="$bibrecord/exist:result/mods:mods"/>
     </modsCollection>
   </xsl:variable>
   <!-- format raw biblist -->
   <xsl:variable name="formatted-biblist">
     <xsl:choose>
       <xsl:when test="$citeclass='note'">
         <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:choose>
             <xsl:when test="$sort_order-bib='cited'">
               <xsl:perform-sort>
                 <xsl:sort select="$number" />
                 <p id="{@ID}">
                   <xsl:value-of select="$number" />
                   <xsl:text>. </xsl:text>
                   <xsl:apply-templates select="mods:titleInfo"/>
                   <xsl:apply-templates select="mods:name"/>
                 </p>
               </xsl:perform-sort>
             </xsl:when>
             <xsl:otherwise>
               <p id="{@ID}">
                 <xsl:apply-templates select="mods:titleInfo"/>
                 <xsl:apply-templates select="mods:name"/>
               </p>
             </xsl:otherwise>
           </xsl:choose>
         </xsl:for-each>
       </xsl:when>
     </xsl:choose>
   </xsl:variable>
   <xsl:template match="/">
     <html>
       <head>
         <title>Testing</title>
       </head>
       <body>
         <div id="content">
           <div id="main-content">
             <xsl:apply-templates/>
           </div>
           <div id="bibliography">
             <xsl:copy-of select="$formatted-biblist"/>
           </div>
         </div>
       </body>
     </html>
     <xsl:if test="$biboutfile">
       <xsl:result-document href="{$biboutfile}">
         <xsl:copy-of select="$raw-biblist"/>
       </xsl:result-document>
     </xsl:if>
   </xsl:template>
   <xsl:template match="mods:titleInfo">
     <span class="title">
       <xsl:apply-templates select="mods:title"/>
       <xsl:apply-templates select="mods:subTitle"/>
     </span>
   </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>
     <span class="creator">
       <xsl:apply-templates select="mods:namePart[@type='family']"/>
     </span>
   </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">
     <h2>
       <xsl:apply-templates/>
     </h2>
   </xsl:template>
   <xsl:template match="db:para">
     <p>
       <xsl:apply-templates/>
     </p>
   </xsl:template>
   <!--+ ==============================================================
     | custom 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:value-of  
select="$cite-position/bib:refs/bib:unique/bib:ref[@key=$linkend]/
@position"/>
   </xsl:template>
</xsl:stylesheet>
===

Result:

<?xml version="1.0" encoding="utf-8"?><html  
xmlns="http://www.w3.org/1999/xhtml"  
xmlns:bib="http://www.example.org/bib">
    <head>
       <meta http-equiv="Content-Type" content="text/html;  
charset=utf-8"/>

       <title>Testing</title>
    </head>
    <body>
       <div id="content">
          <div id="main-content">
             <h2>Introduction</h2>
             <p>A citation with page number detail: [1].</p>
             <p>Some more citations: [2, 3, 4]. A citation
                <p>... in a footnote [2]</p>.
             </p>
          </div>
          <div id="bibliography">
             <p id="Veer1996a">1. <span class="title">Riots and Rituals:  
The Construction of Violence and Public Space in Hindu  
Nationalism</span>, <span class="creator">van der Veer</span></p>
             <p id="Tilly2000a">2. <span class="title">Review of Moral  
Economy and Popular Protest</span>, <span  
class="creator">Tilly</span></p>
             <p id="Thrift1990a">3. <span class="title">For a New  
Regional Geography 1</span>, <span class="creator">Thrift</span></p>
             <p id="TimesP2001a">4. <span class="title">Senators Stamp  
OK on Anti-Terrorism Laws</span></p>
          </div>
       </div>
    </body>
</html>

This still leaves some questions, though:

1)  what should the output actually look like?  Should the above be  
just an <ol> element and allow the browser to number the bib list?  
That has advantages, but may not be as reliable (??). (in any case, the  
citations should be linked to the bib entries)

2)  I think this suggests the stylesheets need a "sort_order-bib"  
top-level parameter -- as I have here -- with options of "as cited" and  
"author-year" (and maybe "citekey"; am not sure if some bibs are sorted  
on that or not).

3)  I don't know if my solution to the above problem is generalizable  
to footnote/endnote and its first/subsequent problem.

4)  I don't know how to collapse the number ranges.  So, (perhaps with  
a parameter option) "[2, 3, 4]" would become "[2-4]".

Bruce