Look for the word and its context

I need to find a word and its context in xml. for instance

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

      

searches for "and", a three-word context:

<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 this? I wrote some xslt to find a word, but I can't go back to 3 words to set the range. 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 need to search for context also in previous 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>

      

therefore, looking for "and", a three-word context:

    <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 overlap issues, but not a problem now (I think I know how it works). How can I find a word and its context? Thank you very much.

+2


a source to share


1 answer


It can be solved with XSLT 2.0 and with a suitable regex:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="pattern" select="'never saw'"/>
<!-- globale Variable (statt Tunnel-Parameter) -->
<xsl:variable name="rex" select="concat(
        '((\w+\W+){0,3})',      (: leading context :)
        '(', $pattern, ')',     (: matched pattern :)
        '((\W+\w+){0,3})'       (: trailing context :)
    )"/>
<xsl:output indent="yes"/>

<xsl:template match="/">
    <xsl:apply-templates select="*" mode="set-context"/>
</xsl:template>

<xsl:template match="*" mode="set-context">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="set-context"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()" mode="set-context">
    <xsl:analyze-string select="." regex="{ $rex }">
        <xsl:matching-substring>
            <span class="context">
                <xsl:value-of select="regex-group(1)"/>
                <span class="word">
                    <xsl:value-of select="regex-group(3)"/>
                </span>
                <xsl:value-of select="regex-group(4)"/>
            </span>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:copy-of select="."/>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

<xsl:template match="@*|node()"><!-- identity template -->
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

      



Note that overlapping trees you would like to have are not possible in XML.

+1


a source







All Articles