Re: [xsl] enumerating combination

Subject: Re: [xsl] enumerating combination
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Thu, 28 Jun 2007 00:24:48 +0200 (CEST)
Matthieu Ricaud wrote:

  Hi

> It's about a spellchecker. When searching for an experssion
> with say N words, I want to list every combinations of
> suggested words.

  I guess you made an error in your input file (besides the typo
s/valeur/value/), and there should be only 2 As and only 3 Ds.

  So you want to walk from words to words starting at the first word,
in the document order.  For each, either you use its @value or apply
each of its suggest, in the document order.  For suggest, you want to
use its @value and then apply the next word.  Doing so, you construct
the string from step to step.  Where there is no more 'next word', you
output the string and you backtrack:

(drkm)[4] ~/xslt/tests$ cat spell-check.xml
<spellchecker date="Wed Jun 27 17:13:12 CEST 2007">

  <word value="a" exist="false">
    <suggest value="a1"/>
    <suggest value="a2"/>
  </word>

  <word value="b" exist="true"></word>

  <word value="c" exist="false">
    <suggest value="c1"/>
    <suggest value="c2"/>
    <suggest value="c3"/>
  </word>

  <word value="d" exist="false">
    <suggest value="d1"/>
    <suggest value="d2"/>
    <suggest value="d3"/>
  </word>

</spellchecker>

(drkm)[5] ~/xslt/tests$ cat spell-check.xsl
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="spellchecker">
    <xsl:apply-templates select="word[1]"/>
  </xsl:template>

  <xsl:template match="word">
    <xsl:param name="line" select="''"/>
    <xsl:apply-templates select="suggest">
      <xsl:with-param name="line" select="$line"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="suggest | word[empty(*)]">
    <xsl:param name="line" select="''"/>
    <xsl:variable name="next-word" select="
        ../following-sibling::word[1]
        | following-sibling::word[1]"/>
    <xsl:variable name="new-line" select="
        concat($line, ' ', @value)"/>
    <xsl:choose>
      <xsl:when test="empty($next-word)">
        <xsl:value-of select="$new-line"/>
        <xsl:text>&#10;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="$next-word">
          <xsl:with-param name="line" select="$new-line"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

(drkm)[6] ~/xslt/tests$ saxon spell-check.xml spell-check.xsl
 a1 b c1 d1
 a1 b c1 d2
 a1 b c1 d3
 a1 b c2 d1
 a1 b c2 d2
 a1 b c2 d3
 a1 b c3 d1
 a1 b c3 d2
 a1 b c3 d3
 a2 b c1 d1
 a2 b c1 d2
 a2 b c1 d3
 a2 b c2 d1
 a2 b c2 d2
 a2 b c2 d3
 a2 b c3 d1
 a2 b c3 d2
 a2 b c3 d3

(drkm)[7] ~/xslt/tests$

  I think that would help you, if I understood corectly your problem.

  Regards,

--drkm





















      _____________________________________________________________________________ 
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail 

Current Thread