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 September 10th, 2007, 04:43 AM
Authorized User
 
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default concatenation string of an input file

hello

I have an input file with the string below. I would like to get it to one long string but in a group of 4, meaning, if it is longer then 4, write a new string.
for example:

my input file is:
    <fields>
        <t id="110" class="statistic">Bytes Read1</t>
        <t id="120" class="statistic">Bytes Write2</t>
        <t id="130" class="statistic">Bytes Read3</t>
        <t id="140" class="statistic">Bytes Write4</t>
        <t id="150" class="statistic">Bytes Read5</t>
        <t id="160" class="statistic">Bytes Write6</t>
    </fields>

out put file required:
<iI31000000>Bytes Read1, Bytes Write2, Bytes Read3, Bytes Write4</iI31000000>
<iI31000001>Bytes Read5, Bytes Write6</iI3100000>

Thanks for your help
Kfir

 
Old September 10th, 2007, 05:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:for-each select="t[position() mod 4 = 1]">
<iiiii>
  <xsl:for-each
     select=".|following-sibling::item[position() &lt; 4]">
     <xsl:value-of select="."/>
     <xsl:if test="position() != last()">, </xsl:if>
  </xsl:for-each>
</iiiii>
<xsl:for-each>

Packing a sequence number into your output element names seems really bad XML design so I haven't done that but, but you can do it if you really must by using <xsl:element name="iiii{position()}"> in place of <iiiii>.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 10th, 2007, 06:06 AM
Authorized User
 
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michel

thanks for your help but the solution that you gave me does not seem to work.
<xsl:for-each select="t[position() mod 4 = 1]"> - does not run on anything probably and that why it does not return an answer.
if I delete this for-each, I will recieve just one string each time for each iii (Bytes Read1) and not concatering.


 
Old September 10th, 2007, 06:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

try fields/t instead then...

/- Sam Judson : Wrox Technical Editor -/
 
Old September 10th, 2007, 06:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the code doesn't work then you probably integrated it incorrectly. Please show your complete code.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 10th, 2007, 06:35 AM
Authorized User
 
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

according to the code you gave me, it prints the first and every 4th (meaning it will print
<iiiii>Service</iiiii>
<iiiii>Bytes Write4</iiiii>

it seems like it enter the inner for-each, but it doesnt make there anything
              <xsl:for-each select=".|following-sibling::item[position() &lt; 4]">
                 <xsl:value-of select="."/>
                 <xsl:if test="position() != last()">, </xsl:if>
              </xsl:for-each>

I am not really familiar with .|following-sibling... command, but it seems not to do there any thing. it passes after the first one, and doesnt print it as a big string from the 1st group of 4 class=statistic and so on.

thanks

 
Old September 10th, 2007, 06:43 AM
Authorized User
 
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

input file:
<titles-en>

    <fields>
        <t id="110" class="statistic">Bytes Read1</t>
        <t id="120" class="statistic">Bytes Write2</t>
        <t id="110" class="statistic">Bytes Read3</t>
        <t id="120" class="statistic">Bytes Write4</t>
        <t id="110" class="statistic">Bytes Read5</t>
        <t id="120" class="statistic">Bytes Write6</t>
    </fields>
</titles-en>

xsl file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
    <xsl:if test = "/titles-en/fields/t/@class = 'statistic'">
        <xsl:for-each select="/titles-en/fields/t[position() mod 4 = 1]">
            <iiiii>
              <xsl:for-each select=".|following-sibling::item[position() &lt; 4]">
                 <xsl:value-of select="."/>
                 <xsl:if test="position() != last()">, </xsl:if>
              </xsl:for-each>
            </iiiii>
        </xsl:for-each>
      </xsl:if>
</xsl:template>
</xsl:stylesheet>


output:
<iiiii>Service</iiiii>
<iiiii>Bytes Write4</iiiii>


 
Old September 10th, 2007, 06:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

following-sibling::item should be following-sibling::t

Cut and paste error.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 10th, 2007, 07:35 AM
Authorized User
 
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks, but still it just print it in a different way.
out put:
<iiii1>Service, </iiii1><iiii2>Bytes Write4, </iiii2>

could it be that
<xsl:for-each select="/titles-en/fields/t[position() mod 4 = 1]">

takes only those that are mod 4, and doesnt run on the ones in between?

 
Old September 10th, 2007, 07:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

could it be that
<xsl:for-each select="/titles-en/fields/t[position() mod 4 = 1]">

takes only those that are mod 4, and doesnt run on the ones in between?

Sure, that's what it's supposed to do. The others are processed by the inner for-each:

<xsl:for-each select=".|following-sibling::t[position() &lt; 4]">

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
String concatenation query. AjayLuthria XSLT 2 March 14th, 2007 11:55 AM
string concatenation nulogix PHP How-To 1 June 24th, 2004 06:17 AM
string concatenation matt_99 Access VBA 2 January 17th, 2004 09:55 AM
T-SQL String Concatenation jaucourt SQL Language 2 January 13th, 2004 10:54 AM
Reading string data from an input file Omega_st1 Visual C++ 0 October 15th, 2003 05:10 PM





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