XSLT: look for a word and it's context

Posted by farhad on Stack Overflow See other posts from Stack Overflow or by farhad
Published on 2010-05-19T11:49:00Z Indexed on 2010/05/19 11:50 UTC
Read the original article Hit count: 479

Filed under:
|

Hello! I need to search a word and it's context into an xml. For example

<line>hello world, my name is farhad and i'm having trouble with xslt</line>

looking for 'and', context of 3 words:

<line>hello world, my <span class="context">name is farhad <span class="word">and</span> i'm having</span> trouble with xslt</line>

How can i do? I wrote some xslt to find the word, but i can't go back 3 words to set span. This is my xslt:

<xsl:variable name="delimiters">[,.;!?\s"()]+</xsl:variable>

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

<xsl:template match="line">
    <line>
    <xsl:for-each select="tokenize(.,'\s')">
           <xsl:choose>
               <!-- se l'ultimo carattere è di punteggiatura, prendo la sottostringa senza la punteggiatura -->
               <xsl:when test="compare(replace(.,$delimiters,'$1'),'red') = 0">
                    <span class="word">
                        <xsl:value-of select="."/>
                    </span> 
               </xsl:when>
               <xsl:otherwise>
                       <xsl:value-of select="."/>
                        <xsl:choose>
                            <xsl:when test="position()=last()">
                                <xsl:text></xsl:text>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text> </xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
               </xsl:otherwise>
           </xsl:choose>
    </xsl:for-each>
    </line><xsl:text>
    </xsl:text>

</xsl:template>

This is an example xml: http://pastebin.com/eAVM9CDQ .

I have to search for context also on preceding tags, for example:

  <line>hello world,</line>
<line>my name</line>
<line>is farhad </line>
<line>and i'm having</line>
<line>trouble with xslt</line>

so, looking for 'and', context of 3 words:

    <line>hello world,</line>
<line>my <span class="context">name</line>
<line>is farhad </line>
<line><span class="word">and</span> i'm having</span></line>
<line>trouble with xslt</line>

with problems of overlapping, but now it's not a problem (i think i know how manage it). How can i search a word and its context? Thank you very much.

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xslt-2.0