Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0
This is the forum to discuss the Wrox book XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition by Michael Kay; ISBN: 9780470192740
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 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 March 2nd, 2012, 08:37 PM
Registered User
Points: 29, Level: 1
Points: 29, Level: 1 Points: 29, Level: 1 Points: 29, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do you repeat rows with certain values but group on others

I am new to XSLT and have read/researched both the XSLT 2.0 and 1.0 versions and could use some assistance if figuring out how to get something accomplished.

I have the following XML file that has has months of repeating data 2 sets per month. I am needing to break the rows up so that each row only contains one pair of months data (Gross/Net sales values) but repeats for every set of month's data that is expressed.

Code:
<?xml version="1.0"?>
<?xml-stylesheet type="txt/xsl" href="transform.xsl"?>
<Report>
<ReportData>
<DataSet>
<Row rowNum="1">
<Column name="Title 1"><Val xsi:type="xsd:string">Sample 1</Val></Column>
<Column name="Title 2"><Val xsi:type="xsd:string">Sample 2</Val></Column>
<Column name="Title 3"><Val xsi:type="xsd:string">Sample 3</Val></Column>
<Column name="Title 4"><Val xsi:type="xsd:string">Sample 4</Val></Column>
<Column name="Title 5"><Val xsi:type="xsd:string">160X600</Val></Column>
<Column name="Title 6"><Val xsi:type="xsd:string" xsi:nil="true"></Val></Column>
<Column name="Title 7"><Val xsi:type="xsd:integer">1325600</Val></Column>
<Column name="MAR_Month_Total"><Val xsi:type="xsd:float">19.00</Val></Column>
<Column name="MAR_Month_Total_B"><Val xsi:type="xsd:float">22.00</Val></Column>
<Column name="APR_Month_Total"><Val xsi:type="xsd:float">18.00</Val></Column>
<Column name="APR_Month_Total_B"><Val xsi:type="xsd:float">35.00</Val></Column>
<Column name="MAY_Month_Total"><Val xsi:type="xsd:float">25.00</Val></Column>
<Column name="MAY_Month_Total_B"><Val xsi:type="xsd:float">15.00</Val></Column>
</Row>
</DataSet>
</ReportData>
</Report>
Sample of how the results need to turn out. New fields with Month ID is based on the month being represented and Year is always the current year.

Code:
<?xml version="1.0"?>
<?xml-stylesheet type="txt/xsl" href="transform.xsl"?>
<Report>
<ReportData>
<DataSet>
<Row rowNum="1">
<Column name="Title 1"><Val xsi:type="xsd:string">Sample 1</Val></Column>
<Column name="Title 2"><Val xsi:type="xsd:string">Sample 2</Val></Column>
<Column name="Title 3"><Val xsi:type="xsd:string">Sample 3</Val></Column>
<Column name="Title 4"><Val xsi:type="xsd:string">Sample 4</Val></Column>
<Column name="Title 5"><Val xsi:type="xsd:string">160X600</Val></Column>
<Column name="Title 6"><Val xsi:type="xsd:string" xsi:nil="true"></Val></Column>
<Column name="Title 7"><Val xsi:type="xsd:integer">1325600</Val></Column>
<Column name="MAR_Month_ID"><Val xsi:type="xsd:integer">3</Val></Column>
<Column name="YEAR"><Val xsi:type="xsd:integer">2012</Val></Column>
<Column name="MAR_Month_Total"><Val xsi:type="xsd:float">19.00</Val></Column>
<Column name="MAR_Month_Total_B"><Val xsi:type="xsd:float">22.00</Val></Column>
</Row>
<Row rowNum="2">
<Column name="Title 1"><Val xsi:type="xsd:string">Sample 1</Val></Column>
<Column name="Title 2"><Val xsi:type="xsd:string">Sample 2</Val></Column>
<Column name="Title 3"><Val xsi:type="xsd:string">Sample 3</Val></Column>
<Column name="Title 4"><Val xsi:type="xsd:string">Sample 4</Val></Column>
<Column name="Title 5"><Val xsi:type="xsd:string">160X600</Val></Column>
<Column name="Title 6"><Val xsi:type="xsd:string" xsi:nil="true"></Val></Column>
<Column name="Title 7"><Val xsi:type="xsd:integer">1325600</Val></Column>
<Column name="APR_Month_ID"><Val xsi:type="xsd:integer">4</Val></Column>
<Column name="YEAR"><Val xsi:type="xsd:integer">2012</Val></Column>
<Column name="APR_Month_Total"><Val xsi:type="xsd:float">18.00</Val></Column>
<Column name="APR_Month_Total_B"><Val xsi:type="xsd:float">35.00</Val></Column>
</Row>
<Row rowNum="3">
<Column name="Title 1"><Val xsi:type="xsd:string">Sample 1</Val></Column>
<Column name="Title 2"><Val xsi:type="xsd:string">Sample 2</Val></Column>
<Column name="Title 3"><Val xsi:type="xsd:string">Sample 3</Val></Column>
<Column name="Title 4"><Val xsi:type="xsd:string">Sample 4</Val></Column>
<Column name="Title 5"><Val xsi:type="xsd:string">160X600</Val></Column>
<Column name="Title 6"><Val xsi:type="xsd:string" xsi:nil="true"></Val></Column>
<Column name="Title 7"><Val xsi:type="xsd:integer">1325600</Val></Column>
<Column name="MAY_Month_ID"><Val xsi:type="xsd:integer">5</Val></Column>
<Column name="YEAR"><Val xsi:type="xsd:integer">2012</Val></Column>
<Column name="MAY_Month_Total"><Val xsi:type="xsd:float">25.00</Val></Column>
<Column name="MAY_Month_Total_B"><Val xsi:type="xsd:float">15.00</Val></Column>
</Row>
</DataSet>
</ReportData>
</Report>
Any Thoughts/Pointers would be totally appreciative.
 
Old March 4th, 2012, 06:46 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think I would tackle this with something like

Code:
<xsl:variable name="rows" select="//Row"/>
<xsl:for-each select="distinct-values(for $n in $rows/Column/@name return substring-before($n, '_Month_Total')[. != '']">
  <xsl:variable name="month"
  <Row>
     ... create row for month $month ...
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 4th, 2012, 12:44 PM
Registered User
Points: 29, Level: 1
Points: 29, Level: 1 Points: 29, Level: 1 Points: 29, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so very much for the example. Gave me a much needed direction on where to go.




Similar Threads
Thread Thread Starter Forum Replies Last Post
for-each-group and differing attributes values Kenneth.Dougherty XSLT 3 March 11th, 2011 01:30 PM
select the group of rows based on the three XML fields and with these conditions. CCPuser XSLT 3 October 22nd, 2010 09:11 AM
select the group of rows based on the three XML fields and with conditions. CCPuser BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 1 October 22nd, 2010 09:06 AM
Can I in SQL group by week & sum the values (i.e. callagga SQL Language 2 May 16th, 2008 07:25 AM
Group XML by Attribute values? NotesSensei XSLT 4 July 14th, 2004 11:53 AM





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