[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Markup a paragraph of text based on keywords


Subject: Re: [xsl] Markup a paragraph of text based on keywords
From: "andrew welch" <andrew.j.welch@xxxxxxxxx>
Date: Fri, 25 Aug 2006 15:23:39 +0100

On 8/25/06, David Carlisle <davidc@xxxxxxxxx> wrote:

probably not terribly efficient but


$ saxon8 words1.xml wordlist.xsl
<?xml version="1.0" encoding="UTF-8"?><p>words <a>apple</a> words <a>juice, orange</a> words apple</p>


David


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="p">
  <p>
    <xsl:apply-templates select="doc('wordlist.xml')/list/word[1]">
      <xsl:with-param name="p" select="."/>
    </xsl:apply-templates>
  </p>
</xsl:template>

<xsl:template match="word">
  <xsl:param name="p"/>
  <xsl:choose>
    <xsl:when test="following-sibling::word">
      <xsl:apply-templates select="following-sibling::word[1]">
        <xsl:with-param name="p" select="replace($p,.,concat('[',.,']'))"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="x">
        <xsl:analyze-string select="replace($p,.,concat('[',.,']'))" regex="\[(.*?)\]">
          <xsl:matching-substring>
            <a><xsl:value-of select="regex-group(1)"/></a>
          </xsl:matching-substring>
          <xsl:non-matching-substring>
            <xsl:value-of select="."/>
          </xsl:non-matching-substring>
        </xsl:analyze-string>
      </xsl:variable>
      <xsl:apply-templates select="$x/node()"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="a[not(preceding-sibling::a=.)]">
  <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

Thanks, David. From your answer I know I'm not missing out on any tricks, so I'm happy.


Current Thread