开发者

Look for a word and its context

I need to search a word and its 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.


It can be solved using XSLT 2.0 and with a suitable regular expression:

<?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 the overlapping trees you would like to have are impossible in XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜