xsl-list
[Top] [All Lists]

Re: [xsl] enumerating combination

2007-06-28 02:23:16
Hi Florent,

Thanks a lot for your solution !

Yes sorry for the mistakes in the xml sample I gave, it was late ...
(and sorry too for having reply to an another thread with another new subject)

I also forgot to precise that I wanted to do this in XSLT 1.0 but I adpat your 
solution to 1.0 and it goes well :-)

This is my final xsl, I generate some xml finaly (I'll put it in a variable to 
iterate on it and make what I need) : 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
version="1.0">
        
        <xsl:output method="xml"/>
        
        <xsl:template match="spellchecker">
                <all-combinations>
                        <xsl:apply-templates select="word[1]"/>
                </all-combinations>
        </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[not(*)]">
                <xsl:param name="line" select="/"/>
                <xsl:variable name="next-word" 
select="self::suggest/parent::word/following-sibling::word[1] | 
self::word/following-sibling::word[1]"/>
                <xsl:variable name="new-line">
                        <xsl:copy-of select="$line"/>
                        <word>
                                <xsl:attribute name="suggest">
                                        <xsl:choose>
                                                <xsl:when test="name() = 
'word'">false</xsl:when>
                                                
<xsl:otherwise>true</xsl:otherwise>
                                        </xsl:choose>
                                </xsl:attribute>
                                <xsl:value-of select="@value"/>
                        </word>
                </xsl:variable>
                <xsl:choose>
                        <xsl:when test="not($next-word)"> <!--last word : 
next-word empty, it 
doesn't exist -->
                                <combination>
                                        <xsl:copy-of select="$new-line"/>
                                </combination>
                                <xsl:text>&#10;</xsl:text>
                        </xsl:when>
                        <xsl:otherwise> <!--not last word, then continue-->
                                <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>

which gave as result (apply on the same xml) :

<?xml version="1.0" encoding="utf-8"?>
<all-combinations>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c1</word>
                <word suggest="true">d1</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c1</word>
                <word suggest="true">d2</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c1</word>
                <word suggest="true">d3</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c2</word>
                <word suggest="true">d1</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c2</word>
                <word suggest="true">d2</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c2</word>
                <word suggest="true">d3</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c3</word>
                <word suggest="true">d1</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c3</word>
                <word suggest="true">d2</word>
        </combination>
        <combination>
                <word suggest="true">a1</word>
                <word suggest="false">b</word>
                <word suggest="true">c3</word>
                <word suggest="true">d3</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c1</word>
                <word suggest="true">d1</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c1</word>
                <word suggest="true">d2</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c1</word>
                <word suggest="true">d3</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c2</word>
                <word suggest="true">d1</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c2</word>
                <word suggest="true">d2</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c2</word>
                <word suggest="true">d3</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c3</word>
                <word suggest="true">d1</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c3</word>
                <word suggest="true">d2</word>
        </combination>
        <combination>
                <word suggest="true">a2</word>
                <word suggest="false">b</word>
                <word suggest="true">c3</word>
                <word suggest="true">d3</word>
        </combination>
</all-combinations>

Thanks again,

Regards, 

Matthieu.


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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>