[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

indexing and string replacement : position() problem i

Subject: indexing and string replacement : position() problem in document()
From: "f_Parlant" <fxp.xsl@xxxxxxxxxxx>
Date: Mon, 23 Jun 2003 00:06:04 +0200
string pos
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 &lt;= 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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.