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 January 5th, 2013, 11:59 AM
Registered User
 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Output from a for-each loop

Im having trouble getting the output Im looking for from a loop
I am exporting to excel

here's xml snippet
<Brown1>
<Transaction>
<Acct_Id>7801177</Acct_Id>
<Acct_Nm>Name1</Acct_Nm>
<Trans_Num>116902</Trans_Num>
<Trans_Desc>Buy</Trans_Desc>
<Trans_Result>Closed</Trans_Result>
</Transaction>
<Transaction>
<Acct_Id>7801177</Acct_Id>
<Acct_Nm>Name1</Acct_Nm>
<Trans_Num>116903</Trans_Num>
<Trans_Desc>Sell</Trans_Desc>
<Trans_Result>Closed</Trans_Result>
</Transaction>
<Transaction>
<Acct_Id>7801177</Acct_Id>
<Acct_Nm>Name1</Acct_Nm>
<Trans_Num>116904</Trans_Num>
<Trans_Desc>Buy</Trans_Desc>
<Trans_Result>Open</Trans_Result>
</Transaction>
<Transaction>
<Acct_Id>8692031</Acct_Id>
<Acct_Nm>Name2</Acct_Nm>
<Trans_Num>116932</Trans_Num>
<Trans_Desc>Buy</Trans_Desc>
<Trans_Result>Open</Trans_Result>
</Transaction>
<Transaction>
<Acct_Id>8692031</Acct_Id>
<Acct_Nm>Name2</Acct_Nm>
<Trans_Num>116999</Trans_Num>
<Trans_Desc>Sell</Trans_Desc>
<Trans_Result>Closed</Trans_Result>
</Transaction>
</Brown1>

This is what Im trying to get out

AccountID 7801177
AccountName Name1

TransNum TransDesc TransResult
116902 Buy Closed
116903 Sell Closed
116904 Buy Open


AccountId 8692031
AccountName Name2

TransNum TransDesc TransResult
116932 Buy Open
116999 Sell Closed

Here's a snippet from xsl

<xsl:template name="header_data">
<xsl:for-each select="/*/Brown1">
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s21"><Data ss:Type="String">AccountId</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String"><xsl:value-of select="Acct_Id"/></Data></Cell>
</Row>
<Row>
<Cell ss:StyleID="s21"><Data ss:Type="String">AccountName</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String"><xsl:value-of select="Acct_Nm"/></Data></Cell>
</Row>
<Row></Row>
<Row>
<Cell ss:StyleID="s21"><Data ss:Type="String">TransNum</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">TransDesc</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">TransResult</Data></Cell>
</Row>
<xsl:call-template name="report_data"/>

</xsl:for-each>
</xsl:template>


<xsl:template name="report_data">
<xsl:for-each select="/*/Brown1">

<Row ss:AutoFitHeight="0" ss:Height="12">
<Cell ss:StyleID="s23"><Data ss:Type="String"><xsl:value-of select="Trans_Num"/></Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="Number"><xsl:value-of select="Trans_Desc"/></Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="Number"><xsl:value-of select="Trans_Result"/></Data></Cell>
</Row>
</xsl:for-each>
</xsl:template>

Im pretty new at this
I just want to print out the AccountId & AcctName once
then the data for that account

Then the accountId & Name followed by data

I just cant get the looping correct
 
Old February 14th, 2013, 11:25 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Im no pro at this either, but i would probably try something as follows.
<xsl:template match = Brown1>
<xsl:for-each select="Transaction">
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s21"><Data ss:Type="String">AccountId</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String"><xsl:value-of select="Acct_Id"/></Data></Cell>
</Row>

all this does it sets your root at the Brown1 level, then says for each select grab transaction, then inside transaction output the Acct_Id.
Im sure someone will post a better way to do this, but give it a try and see where you can go with it
 
Old February 14th, 2013, 11:29 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

you can name and match a template in the same tag i.e
<xsl:temple match=Brown1 name=Header>
There is various fields that can fall in here, Priority, Mode, As.
 
Old February 14th, 2013, 06:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is very easy in XSLT 2.0 using "group-adjacent":

Code:
<xsl:for-each-group select="Transaction" group-adjacent="Account-ID">
   --- output account details ---
  <xsl:for-each select="current-group()">
     --- output transaction details
It's much harder in XSLT 1.0; you will need to use "sibling recursion" rather than a for-each loop.

Both techniques should be well covered in your favourite Wrox textbook.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
For loop with Floating Point Loop Counter - pp. 150 shivesh BOOK: Ivor Horton's Beginning Visual C++ 2010 1 March 28th, 2012 09:31 AM
Array Loop skipping loop in VBA jeskit Access VBA 5 March 20th, 2012 10:03 AM
Loop through recordset to output XML shakshakshak Classic ASP Databases 2 April 25th, 2011 11:04 AM
Loop twice, then inside loop select nodes?? JohnBampton XSLT 2 March 9th, 2009 05:21 AM
Output loop data to a string iloveoatmeal Classic ASP Basics 2 December 22nd, 2005 08:28 PM





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