hi,
I'm trying index a document from a glossary (dictionnary).
I'm using the code from Dave Pawson's **indexing with xslt** and Jeni
Tennison's **multiple string replacements**, but can't get through it
properly.
My first issue is that the position() with a document() fonction doesn't
work: there are tricky context problems that I couldn't understand. Besides,
it makes the loops totally irrelevant and end-up into an infinite loop (well
I guess that's why xalan prefers to crash.)
__mydoc.xml__
<?xml version="1.0" ?>
<doc>
This doc uses a tag based language.
It tests the string replacement problems.
</doc>
__mygloss.xml__
<?xml version="1.0" ?>
<dic>
<item><mot>string</mot><classe>chaine</classe></item>
<item><mot>tag</mot><classe>balise</classe></item>
<item><mot>duplicate</mot><classe>doublon</classe></item>
<item><mot>parsing</mot><classe>no translation yet</classe></item>
</dic>
To begin, I'm just trying to check if my document can be parsed for each
word in the gloss and put them into a link element. Later, I'll try to make
more complex, and substitute th "mot" with the "classe" part.
OUPTUT EXPECTED (SIMPLE)
<?xml version="1.0" ?>
<doc>
This doc uses a <a href="tag">tag</a> based language.
It tests the <a href="string">string</a> replacement problems.
</doc>
OUTPUT EXPECTED (for later)
<?xml version="1.0" ?>
<doc>
This doc uses a <a href="balise">tag</a> based language.
It tests the <a href="chaine">string</a> replacement problems.
</doc>
*************** Version based on Jeni Tennison
<xsl:template match="text()">
<xsl:variable name="input_text" select="."/>
<xsl:variable name="replaced_text">
<xsl:call-template name="gloss">
<xsl:with-param name="input_text" select="$input_text" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$replaced_text" />
</xsl:template>
<xsl:template name="gloss">
<xsl:param name="input_text"/>
<xsl:param name="pos">1</xsl:param>
<xsl:variable name="addlinks">
<xsl:call-template name="links">
<xsl:with-param name="input_text" select="$input_text" />
<xsl:with-param name="word" select="document('mongloss.xml')//mot[$pos]"
/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$pos <= count(document('mongloss.xml')//mot)">
<xsl:call-template name="gloss">
<xsl:with-param name="input_text" select="$addlinks"/>
<xsl:with-param name="pos" select="$pos + 1" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise> <xsl:value-of select="$addlinks"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="links">
<xsl:param name="input_text"/>
<xsl:param name="word"/>
<xsl:choose>
<xsl:when test="contains($input_text, $word)">
<xsl:value-of select="substring-before($input_text, $word)"/>
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of
select="$word"/></xsl:attribute>
<xsl:value-of select="$word"/></xsl:element>
<xsl:value-of select="$word"/>
<xsl:call-template name="links">
<xsl:with-param name="input_text"
select="substring-after($input_text, $word)"/>
<xsl:with-param name="word" select="$word"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input_text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
**************** For-each version (based on dpawson indexing)
<xsl:template match="text()">
<xsl:variable name="input_text"><xsl:value-of select="." /></xsl:variable>
<xsl:variable name="replaced_text">
<xsl:call-template name="gloss">
<xsl:with-param name="input_text" select="$input_text" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$replaced_text" />
</xsl:template>
<xsl:template name="gloss">
<xsl:param name="input_text"/>
<xsl:param name="pos">1</xsl:param>
<xsl:choose>
<xsl:when test="$pos = count(document('mongloss.xml')//mot)">
<xsl:value-of select="$input_text"/>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="document('mongloss.xml')//mot">
<xsl:variable name="word" select="."/>
<xsl:call-template name="gloss">
<xsl:with-param name="input_text">
<xsl:variable name="addlinks">
<xsl:call-template name="links">
<xsl:with-param name="input_text" select="$input_text" />
<xsl:with-param name="word" select="$word" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$addlinks"/>
</xsl:with-param>
<xsl:with-param name="pos" select="$pos + 1" />
</xsl:call-template>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="links">
<xsl:param name="input_text"/>
<xsl:param name="word"/>
<xsl:choose>
<xsl:when test="contains($input_text, $word)">
<xsl:value-of select="substring-before($input_text, $word)"/>
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of
select="$word"/></xsl:attribute>
<xsl:value-of select="$word"/></xsl:element>
<xsl:value-of select="$word"/>
<xsl:call-template name="links">
<xsl:with-param name="input_text"
select="substring-after($input_text, $word)"/>
<xsl:with-param name="word" select="$word"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input_text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
I know some of you have worked on this kind of script, thanks for any
advice.
François Parlant
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list