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

August 19th, 2010, 11:27 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
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!
|
|

August 19th, 2010, 11:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Do you mean like the substring function?
|
|

August 19th, 2010, 11:42 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
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.
|
|

August 19th, 2010, 11:57 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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>
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
ptn77 (August 19th, 2010)
|
|

August 19th, 2010, 12:04 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

August 19th, 2010, 12:11 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

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

August 19th, 2010, 12:29 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by samjudson
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!!
|
|

August 20th, 2010, 04:23 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
maximum number allowed for ".{n}"
Quote:
Originally Posted by samjudson
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,
|
|

August 21st, 2010, 05:18 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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?
|
|
 |