Re: [xsl] preceding/following character?

Subject: Re: [xsl] preceding/following character?
From: Bruce D'Arcus <bdarcus@xxxxxxxxx>
Date: Fri, 17 Jun 2005 19:15:26 -0400
OK, Andrew's solution worked for my simple case, but when I add some
content that more closely modelled the real world problem, it broke
down.  Specifically, where there's more than "cite" element in a "p",
I get duplicate cite elements for each one in the output.

So (new) input:

<p>Some text and more text <cite/>. Some <em>more</em> text. Some text
and more text <cite/>.</p>

Stylsheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:bib="http://www.example.org"; version="2.0"
exclude-result-prefixes="bib">

  <xsl:strip-space elements="*"/>

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

  <xsl:template match="cite" mode="clean">[XXX]</xsl:template>

  <xsl:template match="em">
    <EM>
      <xsl:apply-templates/>
    </EM>
  </xsl:template>

  <xsl:template match="text()[preceding-sibling::node()[1][self::cite]]">
    <xsl:choose>
      <xsl:when test="substring(.,1,1) = '.'">
        <xsl:text>.</xsl:text>
        <xsl:apply-templates select="../cite" mode="clean"/>
        <xsl:copy-of select="substring(.,2)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

output:

<div>Some text and more text .[XXX][XXX] Some <EM>more</EM> text. Some
text and more text .[XXX][XXX]</div>

Note: before I stumbled on this, I was trying to solve another related
problem: the now extra whitespace ahead of the cite element.

If I try to adapt Mike's suggestion of using grouping to solve it, I
find myself a bit lost, and end up with this:

  <xsl:template match="p">
    <xsl:for-each-group select="node()"
      group-starting-with="cite[starts-with(following-sibling::node()[1],
'.')]">
      <xsl:text>. </xsl:text>
      <cite_out/>
      <xsl:value-of select="substring-after(current-group()[1], '.')"/>
      <xsl:copy-of select="current-group()[position() gt 1]"/>
    </xsl:for-each-group>
  </xsl:template>

... with this output:

<div>. <cite_out/>. <cite_out/>. Some <em>more</em> text. Some text
and more text . <cite_out/>.</div>

Converting the element into a string and using regular expressions (as
Mike also suggested) seems like it'd be difficult to do with these
stylesheets, in part because the paragraph template are handled in the
importing (document) stylesheet.

I feel like this should be easier than it seems to be.

Bruce

Current Thread