xsl-list
[Top] [All Lists]

Re: [xsl] Split and Found unique values

2006-12-07 15:42:26
In XSLT1 it works good. It is joining all values but I have problems now with 
split and find unique values.
This is my function. $seen variable should contain all processed values to find 
unique:

<xsl:template name="Split">
  <xsl:param name="strInput"/>
  <xsl:param name="strDelimiter" select="'-'"/>
  <xsl:param name="processed"/>
  <xsl:variable name="strNextItem" 
select="substring-before($strInput,$strDelimiter)"/>
  <xsl:variable name="strOutput" 
select="substring-after($strInput,$strDelimiter)"/>

  <xsl:variable name="seen" select="concat($strNextItem,'-',$processed)"/>

  <xsl:choose>
    <xsl:when test='contains($strInput,$strDelimiter) and 
(not(contains($strNextItem,$seen)))'>
      <xsl:element name='OPTION'>
       <xsl:value-of select="$strNextItem"/>
      </xsl:element>

      <xsl:call-template name="Split">
       <xsl:with-param name="strInput" select="$strOutput"/>
       <xsl:with-param name="strDelimiter" select="$strDelimiter"/>
       <xsl:with-param name="processed" select="$seen"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:if test="not(contains($strInput,$seen))">
        <xsl:element name='OPTION'>
         <xsl:value-of select="$strInput"/>
        </xsl:element>
       </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>





---------- Initial Header -----------

From      : "David Carlisle" davidc(_at_)nag(_dot_)co(_dot_)uk
To          : xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Cc          : 
Date      : Thu, 7 Dec 2006 14:42:01 GMT
Subject : Re: [xsl] Split and Found unique values








your posted input doesn't match your code,  <target:row is in a target
namespace but
match='xml/target' use=
woul match an element target in no namespace.

I don't think you want to do this:
                  <xsl:for-each select='$Rowset[generate-id() = 
generate-id(key("Category", @Category))]'>
as that handles each complete category separately, you want to stick
them all togeter first and then split/sort. so

so something like

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:t="t" >

 <xsl:output indent="yes"/>

<xsl:key name='Category' match='t:row' use='@Category'/>
<xsl:variable name='seen' select="''"/>
 
  <xsl:template match="x">
              <select name="x" multiple="multiple">
                     <xsl:call-template name="Split">
                         <xsl:with-param name="strInput">
<xsl:for-each select="t:row/@Category">
  <xsl:value-of select="."/>
  <xsl:if test="position()!=last()">-</xsl:if>
</xsl:for-each>
                       </xsl:with-param>
                     </xsl:call-template>
              </select>
</xsl:template>

if x is the parent of your target:row elements.


In XSLT2 it's just

<x xmlns:target="t">

       <target:row Category='A-B-C-D'/>
       <target:row Category='A-B'/>
       <target:row Category='A-C-D'/>
       <target:row Category='C'/>
       <target:row Category='B-C-D'/>
       <target:row Category='A-t'/>
</x>




<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:target="t" >

<xsl:output indent="yes"/>

<xsl:template match="x">
  <xsl:for-each select="distinct-values(target:row/tokenize(@Category,'-'))">
   <option><xsl:value-of select="."/></option>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

$ saxon8 spl.xml spl.xsl
<?xml version="1.0" encoding="UTF-8"?>
<option xmlns:target="t">A</option>
<option xmlns:target="t">B</option>
<option xmlns:target="t">C</option>
<option xmlns:target="t">D</option>
<option xmlns:target="t">t</option>

--~------------------------------------------------------------------
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>
--~--




------------------------------------------------------
Passa a Infostrada. ADSL e Telefono senza limiti e senza canone Telecom
http://click.libero.it/infostrada07dic06



--~------------------------------------------------------------------
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>