Re: [xsl] Re: String Combination

Subject: Re: [xsl] Re: String Combination
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Wed, 13 Mar 2002 22:44:32 -0000
Hi,

This takes a node, makes a node-set using each of its letters and then
passes it to Dimitre's nice permutation code.  This does of course, require
the msxml node-set extension function.  Hopefully it will help anyway...

==xml==
<root>
<node>abcd</node>
</root>
('hellow' produced too many results for a post!)

==output==
<?xml version="1.0" encoding="UTF-16">
abcd;abdc;acbd;acdb;adbc;adcb;bacd;badc;bcad;bcda;bdac;bdca;cabd;cadb;cbad;c
bda;cdab;cdba;dabc;dacb;dbac;dbca;dcab;dcba;

cheers
Andrew

==xsl==
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<xsl:variable name="letterNodeSet">
  <root>
  <xsl:call-template name="nodeSetMaker">
    <xsl:with-param name="theString" select="root/node"/>
  </xsl:call-template>
  </root>
</xsl:variable>

<xsl:template match="/">
  <xsl:call-template name="permutations">
    <xsl:with-param name="nodes"
select="msxsl:node-set($letterNodeSet)/root/letter"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="nodeSetMaker">
  <xsl:param name="theString"/>
  <xsl:choose>
    <xsl:when test="string-length($theString)">
      <xsl:element name="letter">
        <xsl:value-of select="substring($theString,1,1)"/>
      </xsl:element>
      <xsl:call-template name="nodeSetMaker">
        <xsl:with-param name="theString"
         select="substring($theString,2)"/>
      </xsl:call-template>
    </xsl:when>
  </xsl:choose>
</xsl:template>


<xsl:template name="permutations">
  <xsl:param name="nodes" select="/.."/>
  <xsl:param name="current"/>
  <xsl:param name="delimiter" select="';'"/>

  <xsl:choose>
    <xsl:when test="not($nodes)">
      <xsl:value-of select="concat($current, $delimiter)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="$nodes">
        <xsl:variable name="thisPosition" select="position()"/>
        <xsl:call-template name="permutations">
          <xsl:with-param name="nodes"
            select="$nodes[position() != $thisPosition]"/>
          <xsl:with-param name="current"
           select="concat($current, .)"/>
       </xsl:call-template>
     </xsl:for-each>
   </xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread