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

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

September 10th, 2007, 05:02 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
<xsl:for-each select="t[position() mod 4 = 1]">
<iiiii>
<xsl:for-each
select=".|following-sibling::item[position() < 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
|
|

September 10th, 2007, 06:06 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

September 10th, 2007, 06:11 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
try fields/t instead then...
/- Sam Judson : Wrox Technical Editor -/
|
|

September 10th, 2007, 06:20 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

September 10th, 2007, 06:35 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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() < 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
|
|

September 10th, 2007, 06:43 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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() < 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>
|
|

September 10th, 2007, 06:55 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

September 10th, 2007, 07:35 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

September 10th, 2007, 07:44 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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() < 4]">
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |