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 October 12th, 2012, 06:22 PM
Registered User
 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Smile String Concate problem

Hi,

I am new to XSLT. Need some assitance in generating a text output using xslt

input XML:
Code:
<data>
<sers>
<ser>sr1</ser>
<ser>sr2</ser>
<ser>sr3</ser>
<ser>sr4</ser>
<ser>sr5</ser>
<ser>sr6</ser>
<ser>sr7</ser>
<ser>sr8</ser>
<ser>sr9</ser>
<ser>sr10</ser>
<ser>sr11</ser>
<ser>sr12</ser>
</sers>
</data>
output needed : (To generate text for every 5 "ser" tag data)

ser: sr1,sr2,sr3,sr4,sr5
ser: sr6,sr7,sr8,sr9,sr10
ser: sr11,sr12

Tried several options to generate this..but not successful. Any help is appreciated!
 
Old October 13th, 2012, 05:01 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an example using XSLT 2.0
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
<xsl:param name="size" select="5"/>
  
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each-group select="data/sers/ser" group-adjacent="(position() - 1) idiv $size">
    <xsl:text>ser: </xsl:text>
    <xsl:value-of select="current-group()" separator=","/>
    <xsl:text> </xsl:text>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>
XSLT 2.0 can be used with XSLT 2.0 processors like Saxon 9 or AltovaXML.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; October 13th, 2012 at 05:49 AM.. Reason: adding links to XSLT 2.0 processors
 
Old October 13th, 2012, 12:32 PM
Registered User
 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks much Martin! That was awesome. Looks like I need to work on my xslt 2.0 knowledge!

We dont use xslt 2.0 version and for-each-group is not working with msxml6.0.dll version.

Is this can be achieved in xslt 1.0 version?

Last edited by rmani; October 13th, 2012 at 01:36 PM..
 
Old October 15th, 2012, 12:51 PM
Registered User
 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can this be achieved using XSLT 1.0 version?
Any advice on how to go about it?
 
Old October 15th, 2012, 01:03 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 1.0 sample:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
<xsl:param name="size" select="5"/>
  
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="data/sers/ser[(position() - 1) mod $size = 0]">
    <xsl:if test="position() > 1">
      <xsl:text>
</xsl:text>
    </xsl:if>
    <xsl:text>ser: </xsl:text>
    <xsl:for-each select=". | following-sibling::ser[position() &lt; $size]">
      <xsl:if test="position() > 1">
        <xsl:text>, </xsl:text>
      </xsl:if>
      <xsl:value-of select="."/>
    </xsl:for-each>
    <xsl:text> </xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 15th, 2012, 02:24 PM
Registered User
 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin. Thats was quick.
This solution worked perfect!





Similar Threads
Thread Thread Starter Forum Replies Last Post
string-length problem alapati.sasi XSLT 3 July 4th, 2007 10:08 AM
string manipulation problem JAMIE AVERY Excel VBA 1 September 21st, 2006 09:01 AM
String Problem seran128 ASP.NET 2.0 Basics 1 April 13th, 2006 11:11 AM
String Problem muki XSLT 0 October 12th, 2005 10:19 PM
string problem shoakat Classic ASP Databases 3 October 20th, 2004 08:53 AM





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