Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 19th, 2010, 11:27 AM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default split string based on Character count

Hello,
Is there a way to split a string based on Character count using xslt 2.0? I believe the tokenize function allows for splitting string based on delimiter, but I need to be able to split string based on character count. I'm hoping there is a way to do this without having to use template and do a loop.

Thanks in advance!
 
Old August 19th, 2010, 11:28 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Do you mean like the substring function?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 19th, 2010, 11:42 AM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

substring would allow for cutting string before or after a specified character or pattern right? I am looking for something more like the tokenize function where you can split the string giving you an array of strings, but instead of splitting based on delimiter I want to split based on character count or string length.

Thanks for taking the time to read my post.
 
Old August 19th, 2010, 11:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

No, substring returns a string based on a two numbers, the start index and length.

You could always just use analyse-string using a pattern such as ".{n}" where n is the length you require.

The following example would split a string into 7 character length bits, with the remainder in the last element.

Code:
<xsl:analyze-string regex=".{{7}}" select=".">
	<xsl:matching-substring><item><xsl:value-of select="."/></item></xsl:matching-substring>
	<xsl:non-matching-substring><item><xsl:value-of select="."/></item></xsl:non-matching-substring>
</xsl:analyze-string>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
ptn77 (August 19th, 2010)
 
Old August 19th, 2010, 12:04 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

As an alternative to using xsl:analyze-pattern here is an attempt to implement a tokenize function taking a string and substring-cout:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/2010/mf"
  exclude-result-prefixes="xs mf">
  
  <xsl:function name="mf:tokenize" as="xs:string*">
    <xsl:param name="str" as="xs:string"/>
    <xsl:param name="count" as="xs:integer"/>
    <xsl:for-each-group select="string-to-codepoints($str)" group-by="(position() - 1) idiv $count">
      <xsl:sequence select="codepoints-to-string(current-group())"/>
    </xsl:for-each-group>
  </xsl:function>
  
  <xsl:template name="main">
    <xsl:value-of select="mf:tokenize('1234567890', 3)" separator="|"/>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 19th, 2010, 12:11 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Furthermore, you could split 10JAN2008 into parts of length 2,3,4 like this:

Code:
<xsl:analyze-string regex="(.{{2}})(.{{3}})(.{{4}})" select=".">
  <xsl:matching-substring>
    <item day="{regex-group(1)" month="{regex-group(2)}" year="{regex-group(3)}">
  </xsl:matching-substring>
</xsl:analyze-string>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 19th, 2010, 12:17 PM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thank you everyone for your replies, I will try your suggestions to see which method works best for me.
 
Old August 19th, 2010, 12:29 PM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
No, substring returns a string based on a two numbers, the start index and length.

You could always just use analyse-string using a pattern such as ".{n}" where n is the length you require.

The following example would split a string into 7 character length bits, with the remainder in the last element.

Code:
<xsl:analyze-string regex=".{{7}}" select=".">
	<xsl:matching-substring><item><xsl:value-of select="."/></item></xsl:matching-substring>
	<xsl:non-matching-substring><item><xsl:value-of select="."/></item></xsl:non-matching-substring>
</xsl:analyze-string>
This method works great for what I'm doing. Thanks!!
 
Old August 20th, 2010, 04:23 PM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default maximum number allowed for ".{n}"

Quote:
Originally Posted by samjudson View Post
No, substring returns a string based on a two numbers, the start index and length.

You could always just use analyse-string using a pattern such as ".{n}" where n is the length you require.

The following example would split a string into 7 character length bits, with the remainder in the last element.

Code:
<xsl:analyze-string regex=".{{7}}" select=".">
	<xsl:matching-substring><item><xsl:value-of select="."/></item></xsl:matching-substring>
	<xsl:non-matching-substring><item><xsl:value-of select="."/></item></xsl:non-matching-substring>
</xsl:analyze-string>

Is there a maximum number allowed for ".{n}" when using
analyse-string with character count regex?

thanks,
 
Old August 21st, 2010, 05:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If there is I shouldn't think it would be one that would affect you - i.e. it would be something like Maximum Integer (2,147,483,647 in 32 bit architecture).

When people ask questions like this however I always worry. Are you saying you have found a value that doesn't work? Are you saying you have written the whole XSLT template but don't have 2 minutes to try it out yourself? Or are you just being curious for the sake of it?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
count occurrence of a character in a string surya Javascript How-To 6 May 7th, 2012 04:23 PM
Chapter 4 - Split View-Based App johnoeffinger BOOK: Beginning iPad Application Development 4 May 21st, 2010 05:43 PM
Count all occurences of a character in a string thomaz C# 2 February 9th, 2007 01:52 PM
Split field at designated character but at " " neo_jakey Classic ASP Professional 0 March 14th, 2005 11:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.