xsl-list
[Top] [All Lists]

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

2004-08-13 09:15:21
On Aug 12, 2004, at 6:13 PM, Michael Kay wrote:

How am I generating this "copy", in other words?  Should I, for
example, be using the new temporary tree functionality? Within the
context of a variable?  Something else?

Basically:

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

So you're doing a two-phase transformation, with $temp holding the output of the first phase, which acts as the input to the second phase. You don't have
to use different modes, but it makes things clearer.

Since the bibliography is only part of the document, and in a separate namespace, I decided to create a virtual copy of the mods:modsCollection element in the phase-1 mode, and then to apply-templates on that in the default mode; like so:

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

<xsl:template match="mods:modsCollection">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="phase-1"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="mods:mods" mode="phase-1">
  <xsl:copy>
    <xsl:copy-of select="*|@*"/>
    <xsl:variable name="bibref" select="mods:mods" />
<xsl:for-each-group select="$bibref" group-by="mods:grouping-key(.)">
      <xsl:sort select="current-grouping-key()"/>
      <xsl:for-each-group select="current-group()"
group-by="xs:integer(substring(mods:originInfo/mods:dateIssued,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()">
          <xsl:attribute name="id">
            <xsl:value-of select="@ID"/>
          </xsl:attribute>
          <key type="creator">
            <xsl:value-of select="$creator-before"/>
            <xsl:choose>
              <xsl:when test="$first and position()=1">
                <xsl:apply-templates select="." mode="names"/>
                <xsl:text> </xsl:text>
              </xsl:when>
              <xsl:otherwise>———.</xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="$creator-after"/>
          </key>
          <key type="year">
            <xsl:value-of select="$year" />
            <xsl:if test="last() > 1">
              <xsl:number value="position()" format="a"/>
            </xsl:if>
          </key>
        </xsl:for-each>
      </xsl:for-each-group>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

My problem is I can't access the generated year, which should be of the form:

        <mods:key type="year">1999a</mods:key>.

I have a feeling this means something's gone wrong with this intermediate step, as if I apply-templates to "mods:key" within the default mode of mods:modsCollection, I get no output. Everything but the year gets rendered correctly.

Is there something obviously wrong with the above? Maybe the way I've generated the copy? I thought it might be a namespace problem, but that's not it.

Bruce



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