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 May 14th, 2007, 11:22 AM
Registered User
 
Join Date: May 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT For Each - Field Names

Hi,

I have some data being returned like this :

[u]Column1 | Column2 | Column 3 | Column 4</u>
 ABC | DEF | GHI | JKL
 123 | 456 | 789 | 101

When i write out the information i want to loop through each column and write out the column name. Is this possible ?

Thanks
 
Old May 14th, 2007, 11:41 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

What does your input look like (in XML) and what do you want the output to look like?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 14th, 2007, 11:49 AM
Registered User
 
Join Date: May 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It looks like this :
<row>
  <PartnerCode>Partner1</PartnerCode>
  <Column1>50%</Column1>
  <Column2>20%</Column2>
  <Column3>30%</Column3>
</row>
<row>
  <PartnerCode>Partner2</PartnerCode>
  <Column1>25%</Column1>
  <Column2>30%</Column2>
  <Column3>45%</Column3>
</row>

and i want write it out like this :

        Partner1 Partner2
Column1 50% 25%
Column2 20% 30%
Column3 30% 45%

Thanks

 
Old May 14th, 2007, 12:02 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm assuming you want HTML output though you still haven't actually said...

Something like this:

<table>
  <tr>
    <td/>
    <xsl:for-each select="row">
     <td><xsl:value-of select="PartnerCode"/></td>
    </xsl:for-each>
  </tr>

  <xsl:for-each select="row[1]/*[not(self::PartnerCode)]">
  <tr>
    <xsl:variable name="p" select="position()"/>
    <td><xsl:value-of select="name()"/></td>
    <xsl:for-each select="../../row/*[position()=$p]">
      <td><xsl:value-of select="."/></td>
    </xsl:for-each>
  </tr>
  </xsl:for-each>
</table>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 15th, 2007, 03:58 AM
Registered User
 
Join Date: May 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much Micheal!

That work 99% perfectly :)

The only question i have is I see the code at the top which skips PartnerCode when writing out the first column, but it still writes out the data for partner code so it appears like this

        Partner1 Partner2
Column1 Partner1 Partner1
Column2 50% 25%
Column3 20% 30%

I presume I need to exclude that field from this for each ;

    <xsl:for-each select="../../row/*[position()=$p]">
      <td><xsl:value-of select="."/></td>
    </xsl:for-each>

can you help me out with the syntax ?

thanks

 
Old May 15th, 2007, 04:36 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry about that. Change to

<xsl:variable name="p" select="position()+1"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 15th, 2007, 06:33 AM
Registered User
 
Join Date: May 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks, works like a charm!

you the man!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Error getting field names from query vbark Access VBA 1 January 23rd, 2007 09:04 PM
Replacing field names in datasheet philiprodley Access VBA 1 August 10th, 2006 12:47 PM
Get Field Names fizzerchris Classic ASP Databases 1 February 25th, 2006 05:06 AM
Retrieving Field Names RayRedSox Classic ASP Basics 0 October 12th, 2005 02:16 PM
Retrieving Dynamic Field Names tdaustin Classic ASP Basics 4 January 12th, 2004 11:10 AM





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