xsl-list
[Top] [All Lists]

Re: grouping + global variable (?) (was re: regexs,

2004-08-13 15:30:12

On Aug 13, 2004, at 2:58 PM, Wendell Piez wrote:

Okay, but why are both these templates matching the same elements with the same priority, in no mode? They are in conflict and your processor will only use one of them (or throw an error).

Because I was totally confused!

I think my confusion was a combination of dealing with multiple namespaces, multiple modes, and multiple levels of processing.

The following examples work, but I'm still a bit shakey on how to handle all these namespace and mode issues when I expand this. For example, the div that wraps the output here is placed in the mods namespace, which clearly is not right.

=== input ===

<?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>Some citations: <citation><biblioref linkend="one"/><biblioref
    linkend="two"/><biblioref linkend="three"/></citation></para>
  </section>
  <bibliography>
    <modsCollection xmlns="http://www.loc.gov/mods/v3";>
      <mods ID="one">
        <name type="personal">
          <namePart type="given">John</namePart>
          <namePart type="family">Doe</namePart>
          <role>
            <roleTerm type="text">author</roleTerm>
          </role>
        </name>
        <titleInfo>
          <title>Some Title</title>
        </titleInfo>
        <originInfo>
          <dateIssued>1999</dateIssued>
        </originInfo>
      </mods>
      <mods ID="two">
        <name type="personal">
          <namePart type="given">John</namePart>
          <namePart type="family">Doe</namePart>
          <role>
            <roleTerm type="text">author</roleTerm>
          </role>
        </name>
        <titleInfo>
          <title>Another Title</title>
        </titleInfo>
        <originInfo>
          <dateIssued>1999</dateIssued>
        </originInfo>
      </mods>
      <mods ID="three">
        <name type="personal">
          <namePart type="given">John</namePart>
          <namePart type="family">Doe</namePart>
          <role>
            <roleTerm type="text">author</roleTerm>
          </role>
        </name>
        <name type="personal">
          <namePart type="given">Jane</namePart>
          <namePart type="family">Jones</namePart>
          <role>
            <roleTerm type="text">author</roleTerm>
          </role>
        </name>
        <titleInfo>
          <title>Some Title</title>
        </titleInfo>
        <originInfo>
          <dateIssued>1999</dateIssued>
        </originInfo>
      </mods>
    </modsCollection>
  </bibliography>
</article>

=== xslt ===

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xs="http://www.w3.org/2001/XMLSchema";
                xmlns:xdt="http://www.w3.org/2003/11/xpath-datatypes";
                xmlns:str="http://example.org/xslt/functions";
                xmlns:db="http://docbook.org/docbook-ng";
                xmlns:mods="http://www.loc.gov/mods/v3";
                xmlns:bib="http://www.example.org/bib";
                xmlns="http://www.loc.gov/mods/v3";
                exclude-result-prefixes="xs xdt mods str db bib"
                version="2.0">

<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
  <xsl:apply-templates select="//mods:modsCollection"/>
</xsl:template>

<xsl:function name="bib:grouping-key" as="xs:string">
  <xsl:param name="bibref" as="element(mods:mods)" />
  <xsl:value-of separator=";">
    <xsl:for-each select="$bibref/mods:name">
      <xsl:value-of
        select="string-join((mods:namePart[(_at_)type = 'family'],
                   mods:namePart[(_at_)type = 'given']), ',')" />
    </xsl:for-each>
  </xsl:value-of>
</xsl:function>

<xsl:template match="mods:modsCollection">
  <xsl:variable name="temp">
    <xsl:apply-templates select="." mode="phase-1"/>
  </xsl:variable>
  <xsl:apply-templates select="$temp" mode="phase-2"/>
</xsl:template>

<xsl:template match="mods:modsCollection" mode="phase-1">
  <xsl:copy>
    <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="xs:integer(substring(mods:originInfo/mods:dateIssued|
                mods:relatedItem/mods:part/mods:date,1,4))">
        <xsl:sort select="current-grouping-key()" />
        <xsl:variable name="year" as="xs:integer"
                select="current-grouping-key()"/>
        <xsl:variable name="first" as="xs:boolean" select="position() = 1" />
        <xsl:for-each select="current-group()">
          <mods ID="{(_at_)ID}">
            <xsl:if test="last() > 1">
              <key type="year-suffix">
                <xsl:number value="position()" format="a"/>
              </key>
            </xsl:if>
            <xsl:copy-of select="*"/>
          </mods>
        </xsl:for-each>
      </xsl:for-each-group>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="mods:modsCollection" mode="phase-2">
  <div class="bibliography">
    <xsl:apply-templates select="mods:mods"/>
  </div>
</xsl:template>

<xsl:template match="mods:mods">
  <div class="record" id="{(_at_)ID}">
    <xsl:apply-templates select="mods:titleInfo[not(@type)]"/>
    <xsl:apply-templates select="mods:key"/>
  </div>
</xsl:template>

<xsl:template match="mods:titleInfo[not(@type)]">
  <h3>
    <xsl:apply-templates select="mods:title"/>
    <xsl:apply-templates select="mods:subTitle"/>
  </h3>
</xsl:template>

<xsl:template match="mods:title">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="mods:subTitle">
  <xsl:text>: </xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="mods:key">
  <p>
    <xsl:text>has suffix of </xsl:text>
    <xsl:apply-templates/>
  </p>
</xsl:template>

</xsl:stylesheet>



<Prev in Thread] Current Thread [Next in Thread>