DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Need help in exslt str:tokenize function

  1. #1
    Join Date
    Feb 2008
    Posts
    7

    Need help in exslt str:tokenize function

    Hi all,

    I am trying to tokenize a string that has empty space in between the delimiters(comma is used as delimiter).

    I have the XSL code as shown below:

    Code:
     <xsl:variable name="Test" select="',BBBB,,DDDD,EEEE'"/>
          <xsl:variable name="TestToken" select="str:tokenize($Test,',')"/>
    
    <xsl:message>Value of first is ....<xsl:value-of select="$TestToken[1]"/></xsl:message>
    <xsl:message>Value of second  is ....<xsl:value-of select="$TestToken[2]"/></xsl:message>
    <xsl:message>Value of Third is ....<xsl:value-of select="$TestToken[3]"/></xsl:message>
    <xsl:message>Value of Fourth is ....<xsl:value-of select="$TestToken[4]"/></xsl:message>
    <xsl:message>Value of Fifth is ....<xsl:value-of select="$TestToken[5]"/></xsl:message>
    Upon executing this code, I get the output as follows:

    Value of first is ....BBBB
    Value of second is ....DDDD
    Value of Third is ....EEEE
    Value of Fourth is ....
    Value of Fifth is ....


    But my required output is as below:

    Value of first is ....
    Value of second is ....BBBB
    Value of Third is ....
    Value of Fourth is ....EEEE
    Value of Fifth is ....DDDD

    I would like to know how to get the required output using str:tokenize.

    Any help appreciated.

    Thanks in advance
    Priby

  2. #2
    Join Date
    May 2009
    Posts
    60
    xml to testing
    Code:
    <root>
    	
    </root>


    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    	<xsl:template match="/">
    		<xsl:variable name="Test" select="',BBBB,,DDDD,EEEE'"/>
    		<xsl:call-template name="cvs">
    			<xsl:with-param name="str" select="$Test"/>
    		</xsl:call-template>
    	</xsl:template>
    	<xsl:template name="cvs">
    		<xsl:param name="str"/>
    		<xsl:param name="cn" select="1"/>
    		<xsl:if test="string-length($str) != 0">
    			<xsl:choose>
    				<xsl:when test="contains($str,',')">
    					<xsl:message>
    						<xsl:value-of select="concat($cn ,'.  ',substring-before($str,','))"/>
    					</xsl:message>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:message>
    						<xsl:value-of select="concat($cn ,'.  ',$str)"/>
    					</xsl:message>
    				</xsl:otherwise>
    			</xsl:choose>
    			<xsl:call-template name="cvs">
    				<xsl:with-param name="str" select="substring-after($str,',')"/>
    				<xsl:with-param name="cn" select="$cn +  1"/>
    			</xsl:call-template>
    		</xsl:if>
    	</xsl:template>
    </xsl:stylesheet>

    result


    Code:
    1.  
    2.  BBBB
    3.  
    4.  DDDD
    5.  EEEE

  3. #3
    Join Date
    Feb 2008
    Posts
    7
    Hi,

    Thanks for your reply.

    Actually I want to take the tokenized values into a variable and access them as an array like TestToken[1].

    Is there any way to achieve the above functionality?

    Thanks in advance
    Priby

  4. #4
    Join Date
    May 2009
    Posts
    60
    work only with sax 6.5.5
    i don't know why(leraning by doing)
    test with saxon 9.1.0.1 ther was problem with saxonutput

    with saxon greater than 6.5.5 use <xsl:result-document href="file:///C:/xml_daten/erg.xml"> and not

    <saxonutput href="C:\xml_daten\erg.xml">


    Code:
    
    <root>
    	<b>
    		<a></a>
    		<a>BBBB</a>
    		<a></a>
    		<a>DDDD</a>
    		<a>EEEE</a>
    	</b>
    </root>

    xsl with extension saxon

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon" exclude-result-prefixes="saxon" version="1.0">
    	<xsl:output indent="yes" method="xml"/>
    	<xsl:template name="cvs">
    		<xsl:param name="str"/>
    		<xsl:param name="cn" select="1"/>
    		<xsl:if test="string-length($str) != 0">
    			<xsl:choose>
    				<xsl:when test="contains($str,',')">
    					<a>
    						<xsl:value-of select="substring-before($str,',')"/>
    					</a>
    				</xsl:when>
    				<xsl:otherwise>
    					<a>
    						<xsl:value-of select="$str"/>
    					</a>
    				</xsl:otherwise>
    			</xsl:choose>
    			<xsl:call-template name="cvs">
    				<xsl:with-param name="str" select="substring-after($str,',')"/>
    				<xsl:with-param name="cn" select="$cn +  1"/>
    			</xsl:call-template>
    		</xsl:if>
    	</xsl:template>
    	<xsl:template match="/">
    		<result>
    			<!--
    				<xsl:call-template name="cvs">
    					<xsl:with-param name="str" select="',BBBB,,DDDD,EEEE'"/>
    				</xsl:call-template>
    			-->
    			<xsl:variable name="foo" select="/root/b"/>
    			<xsl:copy-of select="$foo"/>
    			<!--
    		<xsl:value-of select="count($foo/a)"/>
    		-->
    			<xsl:variable name="TestToken">
    				<b>
    					<xsl:call-template name="cvs">
    						<xsl:with-param name="str" select="',BBBB,,DDDD,EEEE'"/>
    					</xsl:call-template>
    				</b>
    			</xsl:variable>
    			<saxon:output href="C:\xml_daten\erg.xml">
    				<xsl:copy-of select="$TestToken"/>
    			</saxon:output>
    
    
    			<xsl:variable name="test" select="document('C:\xml_daten\erg.xml')/b/a"/>
    			<!--<xsl:value-of select="count($test)"/> -->
    			<a1>
    				<xsl:value-of select="$test[1]"/>
    			</a1>
    			<a2>
    				<xsl:value-of select="$test[2]"/>
    			</a2>
    			<a3>
    				<xsl:value-of select="$test[3]"/>
    			</a3>
    			<a4>
    				<xsl:value-of select="$test[4]"/>
    			</a4>
    			<a5>
    				<xsl:value-of select="$test[5]"/>
    			</a5>
    			<xsl:for-each select="$test">
    				<a>
    					<xsl:value-of select="."/>
    				</a>
    			</xsl:for-each>
    		</result>
    	</xsl:template>
    </xsl:stylesheet>

    result
    Code:
    <?xml version='1.0' ?>
    <result>
      <b>
    		
        <a/>
    		
        <a>BBBB</a>
    		
        <a/>
    		
        <a>DDDD</a>
    		
        <a>EEEE</a>
    	
      </b>
      <a1/>
      <a2>BBBB</a2>
      <a3/>
      <a4>DDDD</a4>
      <a5>EEEE</a5>
      <a/>
      <a>BBBB</a>
      <a/>
      <a>DDDD</a>
      <a>EEEE</a>
    </result>
    Last edited by xml-looser; 09-01-2009 at 10:27 AM.

  5. #5
    Join Date
    Feb 2008
    Posts
    7
    Hi,

    Thanks a lot for your reply.

    I am sorry to say that I want the output of tokenization in a variable and access the variable as an array.

    For example:

    Code:
    <xsl:variable name="Test" select="',BBBB,,DDDD,EEEE'"/>
    <xsl:variable name="TestToken" select="str:tokenize($Test,',')"/>
    <xsl:message>First value is ....<xsl:value-of select="$TestToken[1]"/>
    <xsl:message>Second value is ....<xsl:value-of select="$TestToken[2]"/>
    .....
    Is it possible to achieve the above, without loosing the empty strings between the delimiters.

    Thanks in advance
    Priby

  6. #6
    Join Date
    May 2009
    Posts
    60
    xsl is not programming language


    there is no Array
    but you can store content in a xml file an so you can with
    position access the information

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/"
     extension-element-prefixes="saxon" exclude-result-prefixes="saxon" version="2.0">
    	<xsl:output indent="yes" method="xml"/>
    	<xsl:template name="cvs">
    		<xsl:param name="str"/>
    		<xsl:param name="cn" select="1"/>
    		<xsl:if test="string-length($str) != 0">
    			<xsl:choose>
    				<xsl:when test="contains($str,',')">
    					<xsl:call-template name="nocontent">
    						<xsl:with-param name="str" select="substring-before($str,',')"/>
    					</xsl:call-template>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:call-template name="nocontent">
    						<xsl:with-param name="str" select="$str"/>
    					</xsl:call-template>
    					</xsl:otherwise>
    			</xsl:choose>
    			<xsl:call-template name="cvs">
    				<xsl:with-param name="str" select="substring-after($str,',')"/>
    				<xsl:with-param name="cn" select="$cn +  1"/>
    			</xsl:call-template>
    		</xsl:if>
    	</xsl:template>
    	<xsl:template name="nocontent">
    	<xsl:param name="str"/>
    	<xsl:if test="string-length($str)!=0">
    	<a>
    	<xsl:value-of select="$str"/>
    	</a>
    	</xsl:if>
    	</xsl:template>
    	<xsl:template match="/">
    		<result>
    			
    			<xsl:variable name="Test">
    				<b>
    					<xsl:call-template name="cvs">
    						<xsl:with-param name="str" select="',BBBB,,DDDD,EEEE,,HHHH,1111,,,1111'"/>
    					</xsl:call-template>
    				</b>
    			</xsl:variable>
    			<xsl:result-document href="file:///C:/xml_daten/erg.xml">
    				<xsl:copy-of select="$Test"/>
    			</xsl:result-document>
    
    
    			<xsl:variable name="TestToken" select="document('C:/xml_daten/erg.xml')/b/a"/>
    			<count>
    			<xsl:value-of select="count($TestToken)"/>
    			</count>
    			<a1>
    				<xsl:value-of select="$TestToken[1]"/>
    			</a1>
    			<a2>
    				<xsl:value-of select="$TestToken[2]"/>
    			</a2>
    			<a3>
    				<xsl:value-of select="$TestToken[3]"/>
    			</a3>
    			<a4>
    				<xsl:value-of select="$TestToken[4]"/>
    			</a4>
    			<a5>
    				<xsl:value-of select="$TestToken[5]"/>
    			</a5>
    			<xsl:for-each select="$TestToken">
    				<a>
    					<xsl:value-of select="."/>
    				</a>
    			</xsl:for-each>
    		</result>
    	</xsl:template>
    </xsl:stylesheet>

    result

    Code:
    <?xml version='1.0' ?>
    <result>6
      <a1>BBBB</a1>
      <a2>DDDD</a2>
      <a3>EEEE</a3>
      <a4>HHHH</a4>
      <a5>1111</a5>
      <!--here with for-each display-->
      <a>BBBB</a>
      <a>DDDD</a>
      <a>EEEE</a>
      <a>HHHH</a>
      <a>1111</a>
      <a>1111</a>
    </result>

  7. #7
    Join Date
    May 2009
    Posts
    60
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/"
     extension-element-prefixes="saxon" exclude-result-prefixes="saxon" version="2.0">
    	<xsl:output indent="yes" method="xml"/>
    	<xsl:template name="cvs">
    		<xsl:param name="str"/>
    		<xsl:param name="cn" select="1"/>
    		<xsl:if test="string-length($str) != 0">
    			<xsl:choose>
    				<xsl:when test="contains($str,',')">
    					<xsl:call-template name="nocontent">
    						<xsl:with-param name="str" select="substring-before($str,',')"/>
    					</xsl:call-template>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:call-template name="nocontent">
    						<xsl:with-param name="str" select="$str"/>
    					</xsl:call-template>
    					</xsl:otherwise>
    			</xsl:choose>
    			<xsl:call-template name="cvs">
    				<xsl:with-param name="str" select="substring-after($str,',')"/>
    				<xsl:with-param name="cn" select="$cn +  1"/>
    			</xsl:call-template>
    		</xsl:if>
    	</xsl:template>
    	<xsl:template name="nocontent">
    	<xsl:param name="str"/>
    	<xsl:if test="string-length($str)!=0">
    	<a>
    	<xsl:value-of select="$str"/>
    	</a>
    	</xsl:if>
    	</xsl:template>
    	<xsl:template match="/">
    		<result>
    			<xsl:variable name="data" select="',BBBB,,DDDD,EEEE,,HHHH,1111,,,1111'"/>
    			<xsl:variable name="Test">
    				<b>
    					<xsl:call-template name="cvs">
    						<xsl:with-param name="str" select="$data"/>
    					</xsl:call-template>
    				</b>
    			</xsl:variable>
    			<xsl:result-document href="file:///C:/xml_daten/erg.xml">
    				<xsl:copy-of select="$Test"/>
    			</xsl:result-document>
    
    
    			<xsl:variable name="TestToken" select="document('C:/xml_daten/erg.xml')/b/a"/>
    			<count>
    			<xsl:value-of select="count($TestToken)"/>
    			</count>
    			<a1>
    				<xsl:value-of select="$TestToken[1]"/>
    			</a1>
    			<a2>
    				<xsl:value-of select="$TestToken[2]"/>
    			</a2>
    			<a3>
    				<xsl:value-of select="$TestToken[3]"/>
    			</a3>
    			<a4>
    				<xsl:value-of select="$TestToken[4]"/>
    			</a4>
    			<a5>
    				<xsl:value-of select="$TestToken[5]"/>
    			</a5>
    			<xsl:for-each select="$TestToken">
    				<a>
    					<xsl:value-of select="."/>
    				</a>
    			</xsl:for-each>
    		</result>
    	</xsl:template>
    </xsl:stylesheet>

  8. #8
    Join Date
    Feb 2008
    Posts
    7
    Hi,

    Actually using str:tokenize funtion, I am able to store the value in a variable and access it as an array.

    But the problem is that the empty strings between the delimiters are getting ignored. I just want to avoid that.

    Is there any way to acheive the above functionality.

    Thanks in advance
    Priby

  9. #9
    Join Date
    May 2009
    Posts
    60
    ther is a problem with the first ,
    i don't to modify this

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" extension-element-prefixes="saxon" exclude-result-prefixes="saxon" version="2.0">
    	<xsl:output indent="yes" method="xml"/>
    
    	<xsl:template match="/">
    		<result>
    			<xsl:variable name="data" select="',BBBB,,DDDD,EEEE,,HHHH,1111,,,1111'"/>
    			<xsl:variable name="LineInfo" select="tokenize($data, ',{1,}')"/>
    			<text1>
    				<xsl:value-of select="$LineInfo[1]"/>
    			</text1>
    			<text2>
    				<xsl:value-of select="$LineInfo[2]"/>
    			</text2>
    			<text3>
    				<xsl:value-of select="$LineInfo[3]"/>
    			</text3>
    			<text4>
    				<xsl:value-of select="$LineInfo[4]"/>
    			</text4>
    			<text5>
    				<xsl:value-of select="$LineInfo[5]"/>
    			</text5>
    			<text6>
    				<xsl:value-of select="$LineInfo[6]"/>
    			</text6>
    			<text7>
    				<xsl:value-of select="$LineInfo[7]"/>
    			</text7>
    		</result>
    	</xsl:template>
    </xsl:stylesheet>


    Code:
    <?xml version='1.0' ?>
    <result>
      <text1/>
      <text2>BBBB</text2>
      <text3>DDDD</text3>
      <text4>EEEE</text4>
      <text5>HHHH</text5>
      <text6>1111</text6>
      <text7>1111</text7>
    </result>

  10. #10
    Join Date
    Feb 2008
    Posts
    7
    Hi,

    Thanks for your reply.

    Actually I don't want to loose the empty strings between the delimiters.

    That is in the below code:

    Code:
    <xsl:variable name="data" select="',BBBB,,DDDD,EEEE,,HHHH,1111,,,1111'"/>
    <xsl:variable name="LineInfo" select="str:tokenize($data, ',{1,}')"/>
    
    <xsl:message>First ....<xsl:value-of select="$LineInfo[1]"/></xsl:message>
    <xsl:message>Second ....<xsl:value-of select="$LineInfo[2]"/></xsl:message>	
    <xsl:message>Third ....<xsl:value-of select="$LineInfo[3]"/></xsl:message>
    <xsl:message>Fourth ....<xsl:value-of select="$LineInfo[4]"/></xsl:message>
    <xsl:message>Fifth....<xsl:value-of select="$LineInfo[5]"/></xsl:message>
    <xsl:message>Sixth ....<xsl:value-of select="$LineInfo[6]"/></xsl:message>
    <xsl:message>Seventh ....<xsl:value-of select="$LineInfo[7]"/> </xsl:message>
    <xsl:message>Eighth ....<xsl:value-of select="$LineInfo[8]"/></xsl:message>
    <xsl:message>Nineth ....<xsl:value-of select="$LineInfo[9]"/></xsl:message>
    <xsl:message>Tenth ....<xsl:value-of select="$LineInfo[10]"/></xsl:message>
    <xsl:message>Eleventh ....<xsl:value-of select="$LineInfo[11]"/> </xsl:message>
    I want the output in which the empty strings betweeen the delimiters are NOT ignored.
    That is I want the following output:

    First ....
    Second .....BBBB
    Third ....
    Fourth ....DDDD
    Fifth ....EEEE
    Sixth....
    Seventh....HHHH
    Eigth....1111
    Nineth....
    Tenth....
    Eleventh....1111


    Could you please suggest a way to achieve the above.

    Thanks in advance,
    Priby

  11. #11
    Join Date
    May 2009
    Posts
    60
    Code:
    <xsl:variable name="LineInfo" select="tokenize($data, ',')"/>

  12. #12
    Join Date
    Feb 2008
    Posts
    7
    Hi,

    If I use the following code as you suggested, the empty strings between the delimiters are ignored

    Code:
    <xsl:variable name="data" select="',BBBB,,DDDD,EEEE,,HHHH,1111,,,1111'"/>
    <xsl:variable name="LineInfo" select="str:tokenize($data, ',')"/>
    
    <xsl:message>First ....<xsl:value-of select="$LineInfo[1]"/> </xsl:message>
    <xsl:message>Second ....<xsl:value-of select="$LineInfo[2]"/></xsl:message>	
    <xsl:message>Third ....<xsl:value-of select="$LineInfo[3]"/></xsl:message>
    <xsl:message>Fourth ....<xsl:value-of select="$LineInfo[4]"/></xsl:message>
    <xsl:message>Fifth ....<xsl:value-of select="$LineInfo[5]"/></xsl:message>
    <xsl:message>Sixth ....<xsl:value-of select="$LineInfo[6]"/></xsl:message>
    <xsl:message>Seventh ....<xsl:value-of select="$LineInfo[7]"/> </xsl:message>
    <xsl:message>Eighth ....<xsl:value-of select="$LineInfo[8]"/></xsl:message>
    <xsl:message>Nineth ....<xsl:value-of select="$LineInfo[9]"/></xsl:message>
    <xsl:message>Tenth ....<xsl:value-of select="$LineInfo[10]"/></xsl:message>
    <xsl:message>Eleventh ....<xsl:value-of select="$LineInfo[11]"/> </xsl:message>
    The ouput for the above code will be as

    First ....BBBB
    Second ....DDDD
    Third....EEEE
    Fourth....HHHH
    Fifth....1111
    Sixth....1111
    Seventh....
    Eighth....
    Ninth....
    Tenth....
    Eleventh.....

    I do not want the empty strings between the delimiters(For example the empty string between BBBB and DDDD) to be ignored.

    Is there any way to acheive it?

    Thanks in advance
    Priby

Similar Threads

  1. Replies: 8
    Last Post: 03-08-2003, 07:25 PM
  2. Replies: 2
    Last Post: 06-05-2001, 01:55 PM
  3. calling ping thru vb
    By Michael Shutt in forum VB Classic
    Replies: 6
    Last Post: 04-05-2001, 02:25 AM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center