RE: [xsl] xinclude, temp trees, and keys

Subject: RE: [xsl] xinclude, temp trees, and keys
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 20 Nov 2004 17:37:20 -0000
> However, I've exposed another problem which I can't quite wrapped my 
> head around how to solve.  I have this key at the top of the main 
> stylesheet:
> 
>    <xsl:key name="citekey" match="db:biblioref/@linkend" 
> use="'all'" />
> 
> My problem (I think) is that I need this to apply this to the content 
> that I am xincluding, while my guess is the key is constructed before 
> it gets included in the temporary tree.

The key is applicable to any document (but only one at a time). The key()
function searches the document containing the context node, and you can
reasonably assume that the index will be constructed for a particular
document the first time it is used to search that document.

I don't really see what a key with a constant "use" expression achieves.
Apart from the fact that it applies to whichever document is current at the
time, you could just as well (and probably better) use a global variable 

<xsl:variable name="citekey" select="//db:biblioref/@linkend"/>

What's more, I would probably replace:

<xsl:for-each select="distinct-values(key('citekey', 'all'))">
           <xsl:if test="position() &gt; 1">,%20</xsl:if>
           <xsl:text>&apos;</xsl:text>
           <xsl:value-of select="."/>
           <xsl:text>&apos;</xsl:text>
         </xsl:for-each>

with

<xsl:for-each-group select="//db:biblioref/@linkend" group-by=".">
           <xsl:if test="position() &gt; 1">,%20</xsl:if>
           <xsl:text>&apos;</xsl:text>
           <xsl:value-of select="."/>
           <xsl:text>&apos;</xsl:text>
         </xsl:for-each-group>

One reason is that for-each-group gives you more control over ordering in
the result than distinct-values (the order of the result of distinct-values
is implementation-defined).

Incidentally, the &apos; could just as well be written '.

However, I don't understand the problem well enough to know which document
you are trying to search at this point.

Michael Kay
http://www.saxonica.com/

> 
> Below is my entire driver file. Help would, as always, be much 
> appreciated.
> 
> Bruce
> 
> <?xml version="1.0" encoding="utf-8"?>
> <xsl:stylesheet version="2.0"
> 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>    	xmlns:xs="http://www.w3.org/2001/XMLSchema";
> 	xmlns:db="http://docbook.org/docbook-ng";
>    	xmlns:mods="http://www.loc.gov/mods/v3";
> 	xmlns:bib="http://xbiblio.sourceforge.net";
>    	xmlns="http://docbook.org/docbook-ng";
> 	xmlns:xi="http://www.w3.org/2001/XInclude";
> 	xmlns:exist="http://exist.sourceforge.net/NS/exist";
> 	exclude-result-prefixes="mods bib db xs">
> 
> <!--
> This driver file creates a temporary tree of the document for
> subsequent processing. In the case of footnote class
> citations, this means wrapping all citations in a footnote
> element.
> -->
> 
>    <!--+ 
> ==============================================================
>      | first, create a temporary tree that adds raw bib data 
> to document
>      | based on unique citation keys, and any xincluded docs
>      +-->
> 
>    <xsl:key name="citekey" match="db:biblioref/@linkend" 
> use="'all'" />
> 
>    <xsl:template match="/">
>      <xsl:variable name="temp">
>        <xsl:apply-templates mode="resolve-linked-docs" />
>      </xsl:variable>
>      <xsl:apply-templates select="$temp" mode="step-2" />
>    </xsl:template>
> 
>    <xsl:template match="db:article" mode="resolve-linked-docs">
>      <article>
>        <xsl:apply-templates mode="resolve-linked-docs" />
>      </article>
>    </xsl:template>
> 
>    <xsl:template match="db:book" mode="resolve-linked-docs">
>      <book>
>        <xsl:apply-templates mode="resolve-linked-docs" />
>      </book>
>    </xsl:template>
> 
>    <xsl:template match="db:chapter" mode="resolve-linked-docs">
>      <xsl:copy-of select="."/>
>    </xsl:template>
> 
>    <xsl:template match="db:info" mode="resolve-linked-docs">
>      <xsl:copy-of select="."/>
>    </xsl:template>
> 
>    <xsl:template match="db:section" mode="resolve-linked-docs">
>      <xsl:copy-of select="."/>
>    </xsl:template>
> 
>    <!--+ 
> ==============================================================
>      | resolve xincludes
>      +-->
>    <xsl:template match="xi:include" mode="resolve-linked-docs">
>      <xsl:copy-of select="document(@href)" />
>    </xsl:template>
> 
>    <xsl:template match="db:bibliography" mode="resolve-linked-docs">
>       <xsl:variable name="citekeys">
>         <xsl:text>(</xsl:text>
>         <xsl:for-each select="distinct-values(key('citekey', 'all'))">
>           <xsl:if test="position() &gt; 1">,%20</xsl:if>
>           <xsl:text>&apos;</xsl:text>
>           <xsl:value-of select="."/>
>           <xsl:text>&apos;</xsl:text>
>         </xsl:for-each>
>         <xsl:text>)</xsl:text>
>       </xsl:variable>
>    <!--+ 
> ==============================================================
>      | bibrecord variable identifies where to locate the bib 
> records; in
>      | this case, we access them via http from an eXist XML DB
>      +-->
>       <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]"))' />
>       <bibliography>
>         <modsCollection xmlns="http://www.loc.gov/mods/v3";>
>            <xsl:copy-of select="$bibrecord/exist:result/mods:mods" />
>        </modsCollection>
>      </bibliography>
>    </xsl:template>
> 
>    <!--+ 
> ==============================================================
>      | next, take that temporary tree and enhance it for subsequent 
> processing
>      +-->
> 
>    <xsl:template match="/" mode="step-2">
>      <xsl:variable name="temp">
>        <xsl:apply-templates mode="enhanced-bib" />
>      </xsl:variable>
>      <xsl:apply-templates select="$temp" mode="modified" />
>    </xsl:template>
> 
>    <xsl:template match="db:article" mode="enhanced-bib">
>      <article>
>        <xsl:apply-templates mode="enhanced-bib" />
>      </article>
>    </xsl:template>
> 
>    <xsl:template match="db:book" mode="enhanced-bib">
>      <book>
>        <xsl:apply-templates mode="enhanced-bib" />
>      </book>
>    </xsl:template>
> 
>    <xsl:template match="db:chapter" mode="enhanced-bib">
>      <chapter>
>        <xsl:apply-templates mode="enhanced-bib" />
>      </chapter>
>    </xsl:template>
> 
>    <xsl:template match="db:info" mode="enhanced-bib">
>      <xsl:copy-of select="."/>
>    </xsl:template>
> 
>    <xsl:template match="db:section[$citation-class='author-year']" 
> mode="enhanced-bib">
>      <xsl:copy-of select="."/>
>    </xsl:template>
> 
>    <xsl:template match="db:bibliography" mode="enhanced-bib">
>      <bibliography>
>        <xsl:apply-templates select="mods:modsCollection" 
> mode="enhanced-bib"/>
>      </bibliography>
>    </xsl:template>
> 
>    <xsl:template match="db:section[$citation-class='note']" 
> mode="enhanced-bib">
>      <section>
>        <xsl:apply-templates mode="enhanced-bib"/>
>      </section>
>    </xsl:template>
> 
>    <xsl:template match="db:section[$citation-class='note']/db:info" 
> mode="enhanced-bib">
>      <xsl:copy-of select="."/>
>    </xsl:template>
> 
>    <xsl:template match="db:footnote[$citation-class='note']" 
> mode="enhanced-bib">
>      <footnote>
>        <xsl:apply-templates mode="enhanced-bib"/>
>      </footnote>
>    </xsl:template>
> 
>    <xsl:template match="db:section/db:para[$citation-class='note']" 
> mode="enhanced-bib">
>      <para>
>        <xsl:apply-templates mode="enhanced-bib"/>
>      </para>
>    </xsl:template>
> 
>    <xsl:template match="db:footnote/db:para[$citation-class='note']" 
> mode="enhanced-bib">
>      <para>
>        <xsl:apply-templates mode="enhanced-bib"/>
>      </para>
>    </xsl:template>
> 
>    <xsl:template match="db:citation[$citation-class='note']" 
> mode="enhanced-bib">
>    <!--+ 
> ==============================================================
>      | with footnote class citations, wrap all citations in a 
> footnote, 
> unless
>      | already in a footnote
>      +-->
>      <xsl:choose>
>        <xsl:when test="ancestor::db:footnote">
>          <xsl:copy-of select="."/>
>        </xsl:when>
>        <xsl:otherwise>
>          <footnote>
>            <xsl:copy-of select="."/>
>          </footnote>
>        </xsl:otherwise>
>      </xsl:choose>
>    </xsl:template>
> 
> </xsl:stylesheet>

Current Thread