Re: Rephrasing the problem [was Re: [xsl] Second try: Search and replace many strings that may not be present in target

Subject: Re: Rephrasing the problem [was Re: [xsl] Second try: Search and replace many strings that may not be present in target
From: Greg Faron <gfaron@xxxxxxxxxxxxxxxxxx>
Date: Mon, 20 May 2002 13:05:49 -0600
At 11:51 AM 5/20/2002, you wrote:
                            Sample Input
<kc>
  <quotes>
    <quote who="Peter P. Peters"/>
    <quote who="Bernadette Peters"/>
    <quote who="Seth David Schoen"/>
  </quotes>
  <section>
    <p>This paragraph refers once to Peter P. Peters, once to Bernadette
    Peters, and then again to Peter P. Peters.</p>
  </section>
  <section>
    <p>This paragraph refers to Harry Smith and Seth David Schoen. Also
    Bernadette Peters.</p>
  </section>
</kc>

                          Desired Output
<kc>
  <quotes>
    <quote who="Peter P. Peters"/>
    <quote who="Bernadette Peters"/>
    <quote who="Seth David Schoen"/>
  </quotes>
  <section>
    <p>This paragraph refers once to Peter P. Peters [<a
    href="people/Peter_P._Peters.html">*</a>], once to Bernadette Peters
    [<a href="people/Bernadette_Peters.html">*</a>], and then again to Peter
    P. Peters.</p>
  </section>
  <section>
    <p>This paragraph refers to Harry Smith and Seth David Schoen [<a
    href="people/Seth_David_Schoen.html">*</a>]. Also Bernadette Peters
    [<a href="people/Bernadette_Peters.html">*</a>]</p>
  </section>
</kc>


I don't know why the first <a> tag is stripped out of the output, but I'll leave it to the others to solve. I've got to get back to work... Here you go:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="kc"/>
</xsl:template>
<xsl:template match="kc">
<kc>
<xsl:copy-of select="quotes"/>
<xsl:apply-templates select="section"/>
</kc>
</xsl:template>
<xsl:template match="section">
<section>
<!--
I assume each <section/> contains only one <p/>, as indicated in your sample input.
If not, you need to create a for-each loop here.
-->
<p>
<xsl:call-template name="linkify">
<xsl:with-param name="contents" select="p"/>
<xsl:with-param name="quote-names" select="/kc/quotes/quote"/>
</xsl:call-template>
</p>
</section>
</xsl:template>
<xsl:template name="linkify">
<xsl:param name="contents" select="/.."/>
<xsl:param name="quote-names" select="/.."/>
<xsl:choose>
<xsl:when test="not($quote-names)">
<xsl:copy-of select="$contents"/>
</xsl:when>
<xsl:when test="contains($contents, $quote-names[1]/@who)">
<xsl:call-template name="linkify">
<xsl:with-param name="contents">
<xsl:copy-of select="substring-before($contents, $quote-names[1]/@who)"/>
<xsl:value-of select="$quote-names[1]/@who"/>
<xsl:text> [</xsl:text>
<a>
<xsl:attribute name="href">
<xsl:text>people/</xsl:text>
<xsl:value-of select="translate($quote-names[1]/@who, ' ', '_')"/>
<xsl:text>.html</xsl:text>
</xsl:attribute>
<xsl:text>*</xsl:text>
</a>
<xsl:text>]</xsl:text>
<xsl:copy-of select="substring-after($contents, $quote-names[1]/@who)"/>
</xsl:with-param>
<xsl:with-param name="quote-names" select="$quote-names[position() > 1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="linkify">
<xsl:with-param name="contents" select="$contents"/>
<xsl:with-param name="quote-names" select="$quote-names[position() > 1]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>





Greg Faron Integre Technical Publishing Co.



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread