Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
Reply With Quote
  #2 (permalink)  
Old March 4th, 2012, 06:46 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #3 (permalink)  
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:40 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.