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

Subject: Re: [xsl] grouping + global variable (?) (was re: regexs,
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 13 Aug 2004 18:46:27 -0400
Bruce,

Namespace issues are tricky. A good rule of thumb is to make all namespaces explicit in your stylesheet, and not switch them off or around as you go. (You can often get away with doing this in your source, as you have it, once you've learned to "decouple" the name given from the actual name as you think about it.)

The reason your div is coming out in the mods namespace is that that is the namespace assigned by the stylesheet to the null namespace, which is the namespace the div is in. If you want it in another namespace, or none, change or remove that null namespace declaration at the top of the stylesheet. (And/or make the namespace explicit in the literal result element.)

If this isn't sufficient, someone who knows XSLT 2.0 better will have to give you advice on the nice features the draft gives for namespace fixup, to have your namespaces not only behave well, but look good too.

As for modes, this looks like it'll work, but you might want to consider making phase-1 and phase-2 apply all the way down their own template hierarchies, to prevent them from stepping on each other inadvertently.

I hope this helps,
Wendell

At 06:30 PM 8/13/2004, you wrote:

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[@type = 'family'],
                   mods:namePart[@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="{@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="{@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>


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread